diff --git a/aspnetcore/grpc/configuration.md b/aspnetcore/grpc/configuration.md index fb8a01331d..02d0126689 100644 --- a/aspnetcore/grpc/configuration.md +++ b/aspnetcore/grpc/configuration.md @@ -25,7 +25,7 @@ The following table describes options for configuring gRPC services: | `CompressionProviders` | gzip | A collection of compression providers used to compress and decompress messages. Custom compression providers can be created and added to the collection. The default configured providers support **gzip** compression. | | `ResponseCompressionAlgorithm` | `null` | The compression algorithm used to compress messages sent from the server. The algorithm must match a compression provider in `CompressionProviders`. For the algorithm to compress a response, the client must indicate it supports the algorithm by sending it in the **grpc-accept-encoding** header. | | `ResponseCompressionLevel` | `null` | The compress level used to compress messages sent from the server. | -| `Interceptors` | None | A collection of interceptors that are run with each gRPC call. Interceptors are run in the order they are registered. Globally configured interceptors are run before interceptors configured for a single service. For more information about gRPC interceptors, see [gRPC Interceptors vs. Middleware](xref:grpc/migration#grpc-interceptors-vs-middleware). | +| `Interceptors` | None | A collection of interceptors that are run with each gRPC call. Interceptors are run in the order they are registered. Globally configured interceptors are run before interceptors configured for a single service.

Interceptors have a per-request lifetime by default. The interceptor constructor is called and parameters are resolved from [dependency injection (DI)](xref:fundamentals/dependency-injection). An interceptor type can also be registered with DI to override how it is created and its lifetime.

Interceptors offer similar functionalities compared to ASP.NET Core middleware. For more information, see [gRPC Interceptors vs. Middleware](xref:grpc/migration#grpc-interceptors-vs-middleware). | | `IgnoreUnknownServices` | `false` | If `true`, calls to unknown services and methods don't return an **UNIMPLEMENTED** status, and the request passes to the next registered middleware in ASP.NET Core. | Options can be configured for all services by providing an options delegate to the `AddGrpc` call in `Startup.ConfigureServices`: @@ -36,6 +36,10 @@ Options for a single service override the global options provided in `AddGrpc` a [!code-csharp[](~/grpc/configuration/sample/GrcpService/Startup2.cs?name=snippet)] +Service interceptors have a per-request lifetime by default. Registering the interceptor type with DI overrides how an interceptor is created and its lifetime. + +[!code-csharp[](~/grpc/configuration/sample/GrcpService/Startup3.cs?name=snippet)] + ## Configure client options gRPC client configuration is set on `GrpcChannelOptions`. Configuration options are in the [`Grpc.Net.Client`](https://www.nuget.org/packages/Grpc.Net.Client) package. diff --git a/aspnetcore/grpc/configuration/sample/GrcpService/Startup3.cs b/aspnetcore/grpc/configuration/sample/GrcpService/Startup3.cs new file mode 100644 index 0000000000..97aeba9e4b --- /dev/null +++ b/aspnetcore/grpc/configuration/sample/GrcpService/Startup3.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace GrcpService +{ + public class Startup2 + { + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 +#region snippet + public void ConfigureServices(IServiceCollection services) + { + services.AddGrpc(options => + { + options.Inteceptors.Add(); + }); + services.AddSingleton(); + } +#endregion + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + // Communication with gRPC endpoints must be made through a gRPC client. + // To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909 + endpoints.MapGrpcService(); + }); + } + } +}