Skip to content

Commit

Permalink
Merge pull request #4 from yileicn/feature/aspnetcore3.0
Browse files Browse the repository at this point in the history
Feature/aspnetcore3.0
  • Loading branch information
yileicn authored Mar 29, 2020
2 parents 00762d7 + 94247f7 commit 4382238
Show file tree
Hide file tree
Showing 104 changed files with 2,466 additions and 856 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,7 @@ nuget/tools/*
nuget/*.nupkg
nuget/*.unitypackage
.vs/
/examples/HttpGateway
*.bat
/nupkgs
*.nuspec
48 changes: 32 additions & 16 deletions Grpc.Extensions.sln
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2019
# Visual Studio Version 16
VisualStudioVersion = 16.0.29911.84
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.Extension", "src\Grpc.Extension\Grpc.Extension.csproj", "{3340E08A-22AA-4024-8D77-03B7CB5DC341}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{4C928E46-8040-4B94-B73F-4D4412741F83}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Greeter", "examples\Greeter\Greeter\Greeter.csproj", "{724DFC8C-4B57-4C3F-811C-0463BE2A2829}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreeterClient", "examples\Greeter\GreeterClient\GreeterClient.csproj", "{ACCF4597-3748-4117-8633-1CB767F8CCC3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreeterServer", "examples\Greeter\GreeterServer\GreeterServer.csproj", "{A7706C84-92D2-4B7A-B779-76B64D2950EC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FM.GrpcDashboard", "FM.GrpcDashboard", "{0EF7BF4A-D275-4AF3-9C06-13AB154CED76}"
Expand All @@ -35,6 +31,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.Extension.Discovery",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.Extension.Abstract", "src\Grpc.Extension.Abstract\Grpc.Extension.Abstract.csproj", "{ABCD8408-7BED-431F-BBC2-E880DC597EB5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.Extension.AspNetCore", "src\Grpc.Extension.AspNetCore\Grpc.Extension.AspNetCore.csproj", "{5EC20A87-DF9C-4A79-B847-EB7DD5C1553F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MathServer.AspNetCore", "examples\CodeFirst\MathServer.AspNetCore\MathServer.AspNetCore.csproj", "{CF0443B5-DC32-4C80-B80E-A3F8D6E1792B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreeterClientTest", "examples\Greeter\GreeterClientTest\GreeterClientTest.csproj", "{ACCF4597-3748-4117-8633-1CB767F8CCC3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreeterClient", "examples\Greeter\GreeterClient\GreeterClient.csproj", "{724DFC8C-4B57-4C3F-811C-0463BE2A2829}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AspNetCore3.0", "AspNetCore3.0", "{291086F1-5608-46C9-9976-BD969B8C8256}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -45,14 +51,6 @@ Global
{3340E08A-22AA-4024-8D77-03B7CB5DC341}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3340E08A-22AA-4024-8D77-03B7CB5DC341}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3340E08A-22AA-4024-8D77-03B7CB5DC341}.Release|Any CPU.Build.0 = Release|Any CPU
{724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Debug|Any CPU.Build.0 = Debug|Any CPU
{724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Release|Any CPU.ActiveCfg = Release|Any CPU
{724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Release|Any CPU.Build.0 = Release|Any CPU
{ACCF4597-3748-4117-8633-1CB767F8CCC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACCF4597-3748-4117-8633-1CB767F8CCC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACCF4597-3748-4117-8633-1CB767F8CCC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACCF4597-3748-4117-8633-1CB767F8CCC3}.Release|Any CPU.Build.0 = Release|Any CPU
{A7706C84-92D2-4B7A-B779-76B64D2950EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7706C84-92D2-4B7A-B779-76B64D2950EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7706C84-92D2-4B7A-B779-76B64D2950EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -89,20 +87,38 @@ Global
{ABCD8408-7BED-431F-BBC2-E880DC597EB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABCD8408-7BED-431F-BBC2-E880DC597EB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABCD8408-7BED-431F-BBC2-E880DC597EB5}.Release|Any CPU.Build.0 = Release|Any CPU
{5EC20A87-DF9C-4A79-B847-EB7DD5C1553F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EC20A87-DF9C-4A79-B847-EB7DD5C1553F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EC20A87-DF9C-4A79-B847-EB7DD5C1553F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EC20A87-DF9C-4A79-B847-EB7DD5C1553F}.Release|Any CPU.Build.0 = Release|Any CPU
{CF0443B5-DC32-4C80-B80E-A3F8D6E1792B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF0443B5-DC32-4C80-B80E-A3F8D6E1792B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF0443B5-DC32-4C80-B80E-A3F8D6E1792B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF0443B5-DC32-4C80-B80E-A3F8D6E1792B}.Release|Any CPU.Build.0 = Release|Any CPU
{ACCF4597-3748-4117-8633-1CB767F8CCC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACCF4597-3748-4117-8633-1CB767F8CCC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACCF4597-3748-4117-8633-1CB767F8CCC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACCF4597-3748-4117-8633-1CB767F8CCC3}.Release|Any CPU.Build.0 = Release|Any CPU
{724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Debug|Any CPU.Build.0 = Debug|Any CPU
{724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Release|Any CPU.ActiveCfg = Release|Any CPU
{724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{724DFC8C-4B57-4C3F-811C-0463BE2A2829} = {94B12F76-D786-4D94-A9F4-8DD7DD73685F}
{ACCF4597-3748-4117-8633-1CB767F8CCC3} = {94B12F76-D786-4D94-A9F4-8DD7DD73685F}
{A7706C84-92D2-4B7A-B779-76B64D2950EC} = {94B12F76-D786-4D94-A9F4-8DD7DD73685F}
{2A959582-34C8-4556-AD13-093ED5917FCF} = {0EF7BF4A-D275-4AF3-9C06-13AB154CED76}
{94B12F76-D786-4D94-A9F4-8DD7DD73685F} = {4C928E46-8040-4B94-B73F-4D4412741F83}
{BE41EB1C-CB4F-4EDD-9E47-17D54308B014} = {4C928E46-8040-4B94-B73F-4D4412741F83}
{D8942E3B-2EE2-454E-85D6-A87ED1BE3907} = {BE41EB1C-CB4F-4EDD-9E47-17D54308B014}
{4BDF0944-F493-4D6B-BC30-A7C349E7080E} = {BE41EB1C-CB4F-4EDD-9E47-17D54308B014}
{EE3555D8-A6BA-4340-9F56-121B24C48E0C} = {BE41EB1C-CB4F-4EDD-9E47-17D54308B014}
{5EC20A87-DF9C-4A79-B847-EB7DD5C1553F} = {291086F1-5608-46C9-9976-BD969B8C8256}
{CF0443B5-DC32-4C80-B80E-A3F8D6E1792B} = {BE41EB1C-CB4F-4EDD-9E47-17D54308B014}
{ACCF4597-3748-4117-8633-1CB767F8CCC3} = {94B12F76-D786-4D94-A9F4-8DD7DD73685F}
{724DFC8C-4B57-4C3F-811C-0463BE2A2829} = {94B12F76-D786-4D94-A9F4-8DD7DD73685F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {89FBBEF6-DF3C-43D4-BD32-075D5E3D14F2}
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build:clean
-dotnet pack ./src/Grpc.Extension.Common/Grpc.Extension.Common.csproj -c Release -o ./nupkgs
-dotnet pack ./src/Grpc.Extension.Client/Grpc.Extension.Client.csproj -c Release -o ./nupkgs
-dotnet pack ./src/Grpc.Extension/Grpc.Extension.csproj -c Release -o ./nupkgs
-dotnet pack ./src/Grpc.Extension.AspNetCore/Grpc.Extension.AspNetCore.csproj -c Release -o ./nupkgs

push-dev:build
#method one
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@
- Grpc ProtoFirst
- 通过Proto生成代码和注释
- Grpc CodeFirst
- 通过代码生成proto和注释(GrpcMethod自动注册)
- 通过代码生成proto和注释给第三方语言使用(GrpcMethod自动注册)
- 代码更干净且方便扩展,例如可以在ProtoMessage上打验证特性来统一处理验证逻辑等
- 更方便拆分GrpcService到多个类,而不是使用partial class

### NuGet Package
- [Grpc.Extensions](https://www.nuget.org/packages/FM.Grpc.Extensions/)
- [Grpc.Extensions.Client](https://www.nuget.org/packages/FM.Grpc.Extensions.Client/)
- [Grpc.Extensions.Discovery](https://www.nuget.org/packages/FM.Grpc.Extensions.Discovery/)
支持NetFramework4.6,NetCore2.1
- [Grpc.Extensions 服务端](https://www.nuget.org/packages/FM.Grpc.Extensions/)
- [Grpc.Extensions.Client 客户端](https://www.nuget.org/packages/FM.Grpc.Extensions.Client/)
- [Grpc.Extensions.Discovery 服务注册和发现](https://www.nuget.org/packages/FM.Grpc.Extensions.Discovery/)

支持asp.netcore3.0
- [Grpc.Extensions.AspNetCore 服务端](https://www.nuget.org/packages/FM.Grpc.Extensions.AspNetCore/)
- [Grpc.Extensions.Client 客户端](https://www.nuget.org/packages/FM.Grpc.Extensions.Client/)
- [Grpc.Extensions.Discovery 服务注册和发现](https://www.nuget.org/packages/FM.Grpc.Extensions.Discovery/)

### Documentation
- [Grpc ProtoFirst Demo](https://github.com/yileicn/Grpc.Extensions/tree/master/examples/Greeter)
Expand All @@ -40,6 +46,8 @@
## 依赖的技术栈
- [dotnet standard 2.0]()
- [gRPC - An RPC library and framework](https://github.com/grpc/grpc)
- [gRPC-dotnet - gRPC for .NET](https://github.com/grpc/grpc-dotnet)
- [Protobuf-net - Protocol Buffers library for idiomatic .NET](https://github.com/protobuf-net/protobuf-net)
- [Consul - Service Discovery and Configuration Made Easy](https://consul.io)
- [Polly - Polly is a .NET resilience and transient-fault-handling library](https://github.com/App-vNext/Polly)
- [OpenTracing(Jaeger) - a Distributed Tracing System](https://github.com/jaegertracing/jaeger)
Expand Down
2 changes: 1 addition & 1 deletion examples/CodeFirst/MathClient/MathClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
<PackageReference Include="Grpc" Version="1.19.0" />
<PackageReference Include="Grpc.Core" Version="2.24.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Grpc.Core;
using Grpc.Extension.Abstract;
using Grpc.Extension.Common.Internal;
using Math;
using System.Threading.Tasks;
using static Helloworld.Greeter;

namespace MathServer.AspNetCore
{
public class ClientTestGrpc : IGrpcService
{
private readonly TestScope _testScope;
private readonly GreeterClient _greeterClient;

public ClientTestGrpc(TestScope testScope, GreeterClient greeterClient)
{
_testScope = testScope;
_greeterClient = greeterClient;
}
public async Task<StringMessage> ClientTest(EmptyMessage request, ServerCallContext context)
{
var result = await _greeterClient.SayHelloAsync(new Helloworld.HelloRequest() { Name = "yilei" });
return new StringMessage() { Value = $"{result.Message},guid {_testScope.Id} " };
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Utils;
using Helloworld;
using Microsoft.Extensions.Logging;

namespace MathServer.AspNetCore
{
public class GreeterGrpcImpl : Greeter.GreeterBase
{
private readonly TestScope _testScope;

public GreeterGrpcImpl(TestScope testScope)
{
_testScope = testScope;
}
// Server side handler of the SayHello RPC
public override async Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return new HelloReply { Message = $"Hello {request.Name}, guid {_testScope.Id}" };
}

public override async Task<HelloReply> SayHelloStream(IAsyncStreamReader<HelloRequest> requestStream, ServerCallContext context)
{
var helloReply = new HelloReply();
var sb = new StringBuilder();
await requestStream.ForEachAsync(req =>
{
sb.AppendLine("Hello " + requestStream.Current.Name);
return Task.CompletedTask;
});
//while (await requestStream.MoveNext())
//{
// sb.AppendLine("Hello " + requestStream.Current.Name);
//}
helloReply.Message = sb.ToString();
return helloReply;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Protobuf Include="..\..\protos\helloworld.proto" GrpcServices="Server,Client">
<Link>Protos\helloworld.proto</Link>
</Protobuf>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.24.0" />
<PackageReference Include="Grpc.Tools" Version="2.24.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Grpc.Extension.AspNetCore\Grpc.Extension.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\src\Grpc.Extension.Client\Grpc.Extension.Client.csproj" />
<ProjectReference Include="..\Math\Math.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

namespace MathServer.AspNetCore
{
/// <summary>
/// 不使用Request Scope,使用这个Middleware即可
/// </summary>
public class ServiceProvidersMiddleware
{
private readonly ServiceProvidersFeature _serviceProvidersFeature;
private readonly RequestDelegate _next;

public ServiceProvidersMiddleware(RequestDelegate next, IServiceProvider serviceProvider)
{
_serviceProvidersFeature = new ServiceProvidersFeature(serviceProvider);
_next = next;
}

public Task InvokeAsync(HttpContext context)
{
// Configure request to use application services to avoid creating a request scope
context.Features.Set<IServiceProvidersFeature>(_serviceProvidersFeature);
return _next(context);
}

private class ServiceProvidersFeature : IServiceProvidersFeature
{
public ServiceProvidersFeature(IServiceProvider requestServices)
{
RequestServices = requestServices;
}

public IServiceProvider RequestServices { get; set; }
}
}
}
12 changes: 12 additions & 0 deletions examples/CodeFirst/MathServer.AspNetCore/Models/TestScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MathServer.AspNetCore
{
public class TestScope
{
public Guid Id = Guid.NewGuid();
}
}
28 changes: 28 additions & 0 deletions examples/CodeFirst/MathServer.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace MathServer.AspNetCore
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls("http://*:0");
webBuilder.UseStartup<Startup>();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"MathServer.AspNetCore": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";
option csharp_namespace = "MathGrpc";


service ClientTestGrpc {
rpc ClientTest(EmptyMessage) returns(StringMessage);

}



message EmptyMessage {
}

message StringMessage {
string Value = 1;
}
Loading

0 comments on commit 4382238

Please sign in to comment.