question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use of TraceHeaderPropagatingHandler in .NET Core 2.1

See original GitHub issue

I am using Google.Cloud.Diagnostics.AspNetCore version 3.0.0-beta06. Due to the change of paradigm for creating HttpClient, the following code for tracing outgoing http requests via Stackdriver Trace does not align well with .NET Core 2.1 any longer:

/// <summary>
/// The <see cref="TraceHeaderPropagatingHandler"/> is populated by dependency injection.
/// </summary>
public async Task<HttpResponseMessage> TraceOutgoing(TraceHeaderPropagatingHandler traceHeaderHandler)
{
    // Add a handler to trace outgoing requests and to propagate the trace header.
    using (var httpClient = new HttpClient(traceHeaderHandler))
    {
        return await httpClient.GetAsync("https://weather.com/");
    }
}

Instead, the HttpClient instance must be created via an injected IHttpClientFactory (and “using” statement becomes unnecessary).
More importantly, the registration and “chaining” of HttpMessageHandler into a handler pipeline is supposed to be configured at Startup.cs - ConfigureServices(), for example:

services.AddHttpClient("weather").AddHttpMessageHandler<TraceHeaderPropagatingHandler>();

The HttpClient infrastructure takes care of chaining the TraceHeaderPropagatingHandler into the handler pipeline, but in return is asking that all handlers have a null InnerHandler, otherwise an exception is thrown.
Unfortunately this is exactly what happens with TraceHeaderPropagatingHandler

System.InvalidOperationException: The 'InnerHandler' property must be null. 'DelegatingHandler' instances provided to 'HttpMessageHandlerBuilder' must not be reused or cached.
Handler: 'Google.Cloud.Diagnostics.Common.TraceHeaderPropagatingHandler'
   at Microsoft.Extensions.Http.HttpMessageHandlerBuilder.CreateHandlerPipeline(HttpMessageHandler primaryHandler, IEnumerable`1 additionalHandlers)
   at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandlerEntry(String name)
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateClient(String name)
   at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.&lt;&gt;c__DisplayClass10_0`2.&lt;AddTypedClient&gt;b__0(IServiceProvider s)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)

This is due to the fact that an InnerHandler is always created by the TraceHeaderPropagatingHandler constructor:

public TempTraceHeaderPropagatingHandler(
    Func<IManagedTracer> managedTracerFactory, HttpMessageHandler innerHandler = null)
{
    _managedTracerFactory = GaxPreconditions.CheckNotNull(managedTracerFactory, nameof(managedTracerFactory));
    InnerHandler = innerHandler ?? new HttpClientHandler();
}

The implementation of the handler should be adjusted to support the new way of managing HttpClients in .NET Core.2.1.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
gabrielmuscommented, Nov 14, 2018

Using now the AddOutgoingGoogleTraceHandler() extension. Thanks for the fix. Cheers!

1reaction
amanda-tarafacommented, Nov 14, 2018

Happy to help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Download .NET Core 2.1 (Linux, macOS, and Windows)
NET Core 2.1 downloads for Linux, macOS, and Windows. .NET is a free, cross-platform, open-source developer platform for building many different types of ......
Read more >
Breaking changes in .NET Core 2.1
Aggressive validation of path characters blocks some cross-platform scenarios. This change was introduced so that .NET does not try to replicate ...
Read more >
.NET Core 2.1 and AWS | AWS Developer Tools Blog
Deploying ASP.NET Core 2.1 Applications with AWS Elastic Beanstalk. One of the great strengths of the . NET Core platform is its ability...
Read more >
Windows Authentication in .net Core 2.1 application
I've got a .net Core 2.1 MVC application that hosts an Angular 6 application. I am using Visual Studio 2017 15.7.3.
Read more >
.NET - Wikipedia
NET is a free and open-source, managed computer software framework for Windows, Linux, and macOS operating systems. It is a cross-platform successor to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found