Skip to content

Commit

Permalink
Updated Grpc.AspNetCore package and used file scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Vake93 authored and LesnyRumcajs committed Jan 16, 2022
1 parent a19fd70 commit bd51360
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
6 changes: 4 additions & 2 deletions dotnet_grpc_bench/GreeterServer/GreeterServer.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.37.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.41.0" />

<Protobuf Include="..\..\proto\helloworld\helloworld.proto" Link="helloworld.proto" />
</ItemGroup>
Expand Down
13 changes: 3 additions & 10 deletions dotnet_grpc_bench/GreeterServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,9 @@

#endregion

using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using GreeterServer.Services;
using GreeterServer;
using System;
using GreeterServer.Services;
using Microsoft.AspNetCore.Server.Kestrel.Core;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -50,4 +43,4 @@
app.UseMiddleware<ServiceProvidersMiddleware>();
app.MapGrpcService<GreeterService>();

await app.RunAsync();
app.Run();
48 changes: 22 additions & 26 deletions dotnet_grpc_bench/GreeterServer/ServiceProvidersMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,35 @@

#endregion

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

namespace GreeterServer
namespace GreeterServer;

public class ServiceProvidersMiddleware
{
public class ServiceProvidersMiddleware
private readonly ServiceProvidersFeature _serviceProvidersFeature;
private readonly RequestDelegate _next;

public ServiceProvidersMiddleware(RequestDelegate next, IServiceProvider serviceProvider)
{
private readonly ServiceProvidersFeature _serviceProvidersFeature;
private readonly RequestDelegate _next;
_serviceProvidersFeature = new ServiceProvidersFeature(serviceProvider);
_next = 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);
}

public Task InvokeAsync(HttpContext context)
private class ServiceProvidersFeature : IServiceProvidersFeature
{
public ServiceProvidersFeature(IServiceProvider requestServices)
{
// Configure request to use application services to avoid creating a request scope
context.Features.Set<IServiceProvidersFeature>(_serviceProvidersFeature);
return _next(context);
RequestServices = requestServices;
}

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

public IServiceProvider RequestServices { get; set; }
}
public IServiceProvider RequestServices { get; set; }
}
}
}
16 changes: 7 additions & 9 deletions dotnet_grpc_bench/GreeterServer/Services/GreeterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading.Tasks;
using Grpc.Core;
using Helloworld;

namespace GreeterServer.Services
namespace GreeterServer.Services;

public class GreeterService : Greeter.GreeterBase
{
public class GreeterService : Greeter.GreeterBase
// Server side handler of the SayHello RPC
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
// Server side handler of the SayHello RPC
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply { Response = request.Request });
}
return Task.FromResult(new HelloReply { Response = request.Request });
}
}
}

0 comments on commit bd51360

Please sign in to comment.