Skip to content

Commit

Permalink
Minor improvements to grpc-dotnet
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Aug 11, 2020
1 parent 060f6c5 commit 5854edd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
54 changes: 54 additions & 0 deletions dotnet_grpc_bench/GreeterServer/ServiceProvidersMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#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 GreeterServer
{
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; }
}
}
}
5 changes: 5 additions & 0 deletions dotnet_grpc_bench/GreeterServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using GreeterServer.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand All @@ -29,6 +30,8 @@ public class Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc(o => o.IgnoreUnknownServices = true);
services.Configure<RouteOptions>(c => c.SuppressCheckForUnhandledSecurityMetadata = true);
services.AddSingleton<GreeterService>();
}

public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicationLifetime)
Expand All @@ -37,6 +40,8 @@ public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicat

app.UseRouting();

app.UseMiddleware<ServiceProvidersMiddleware>();

app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<GreeterService>();
Expand Down

0 comments on commit 5854edd

Please sign in to comment.