-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from yileicn/feature/aspnetcore3.0
Feature/aspnetcore3.0
- Loading branch information
Showing
104 changed files
with
2,466 additions
and
856 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,3 +119,7 @@ nuget/tools/* | |
nuget/*.nupkg | ||
nuget/*.unitypackage | ||
.vs/ | ||
/examples/HttpGateway | ||
*.bat | ||
/nupkgs | ||
*.nuspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
examples/CodeFirst/MathServer.AspNetCore/GrpcServices/ClientTestGrpc.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} " }; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
examples/CodeFirst/MathServer.AspNetCore/GrpcServices/GreeterGrpcImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
examples/CodeFirst/MathServer.AspNetCore/MathServer.AspNetCore.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
57 changes: 57 additions & 0 deletions
57
examples/CodeFirst/MathServer.AspNetCore/Middleware/ServiceProvidersMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
examples/CodeFirst/MathServer.AspNetCore/Models/TestScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
}); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
examples/CodeFirst/MathServer.AspNetCore/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/CodeFirst/MathServer.AspNetCore/Protos/ClientTestGrpc.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.