/// <summary> /// Configures the server to propagate a call's <see cref="ServerCallContext.CancellationToken"/> and /// <see cref="ServerCallContext.Deadline"/> onto the gRPC client. /// </summary> /// <param name="builder">The <see cref="IHttpClientBuilder"/>.</param> /// <param name="configureOptions">An <see cref="Action{GrpcContextPropagationOptions}"/> to configure the provided <see cref="GrpcContextPropagationOptions"/>.</param> /// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns> public static IHttpClientBuilder EnableCallContextPropagation(this IHttpClientBuilder builder, Action <GrpcContextPropagationOptions> configureOptions) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (configureOptions == null) { throw new ArgumentNullException(nameof(configureOptions)); } var options = new GrpcContextPropagationOptions(); configureOptions(options); EnableCallContextPropagationCore(builder, options); return(builder); }
private static void EnableCallContextPropagationCore(IHttpClientBuilder builder, GrpcContextPropagationOptions options) { ValidateGrpcClient(builder); builder.Services.AddHttpContextAccessor(); builder.Services.Configure <GrpcClientFactoryOptions>(builder.Name, o => { o.InterceptorRegistrations.Add(new InterceptorRegistration( InterceptorScope.Channel, s => { var accessor = s.GetRequiredService <IHttpContextAccessor>(); var logger = s.GetRequiredService <ILogger <ContextPropagationInterceptor> >(); return(new ContextPropagationInterceptor(options, accessor, logger)); })); }); }