AddHttpMessageHandler throwing an exception

#103 · closed · 6 comments

View on GitHub ↗

MGrgov90

I was trying to register httpclient and attach a message handler to it. Here is a sample: ``` csharp services.AddHttpClient("namedClient", client =>{ client.BaseAddress = "baseAddress"; }).AddHttpMessageHandler<AuthenticationHandler>(); ``` My authentication handler needs a TokenProviderFactory instance so the registration additionally adds ``` csharp services.AddScoped<TokenProviderFactory>(); ``` and constructor looks like: ``` csharp public class AuthenticationHandler : DelegatingHandler { private readonly TokenProviderFactory _tokenProviderFactory; public AuthenticationHandler(TokenProviderFactory tokenProviderFactory) { _tokenProviderFactory = tokenProviderFactory; } //additional implementation } ``` Later on, I need ScenarioContext (or custom-created Scoped Context) to be injected into TokenProviderFactory ```csharp public class TokenProviderFactory { private readonly ScenarioContext _scenarioContext; private readonly Identity _identity; public TokenProviderFactory(ScenarioContext scenarioContext, Identity identity) { _scenarioContext = scenarioContext; _identity = identity; } //additional implementation } ``` And here is the problem. When I try to create httpClient using httpClientFactory in my steps definitions, I'm not able because I'm not able to resolve ScenarioContext used in TokenProviderFactory. The exception is thrown from here: https://github.com/solidtoken/SpecFlow.DependencyInjection/blob/f6b7c4ca2aa65216e97e763d98832bc7aa9ee0c1/SpecFlow.DependencyInjection/DependencyInjectionPlugin.cs#L175 And it is thrown because the Service Provider which is used in DependencyInjectionPlugin class is not the same as the one used for attaching message handlers. Here is the Microsoft implementation of AddHttpMessageHandler extension method. ``` csharp public static IHttpClientBuilder AddHttpMessageHandler(this IHttpClientBuilder builder, Func<DelegatingHandler> configureHandler) { //some checks builder.Services.Configure<HttpClientFactoryOptions>(builder.Name, options => { options.HttpMessageHandlerBuilderActions.Add(b => b.AdditionalHandlers.Add(configureHandler())); }); return builder; } ``` Any recommendations on how to solve the issue?

Comments

MGrgov90

I'm using the latest prerelease package. [https://github.com/solidtoken/SpecFlow.DependencyInjection/releases/tag/v4.0.0-preview.1](url)

mbhoek

Hey @MGrgov90, thanks for raising this issue. I think this should work if both `AuthenticationHandler` and `TokenProviderFactory` were added as regular dependencies. I'm not sure how the plugin would know that `AddHttpMessageHandler` uses a different service provider. Two questions: 1) can you post the actual exception message? 2) can you try this with plugin v3.9.2 (it has a different bind mapping implementation)

MGrgov90

The exception says that the key is not found, and basically the problem is trying to get value from the BindMappings dictionary. Yes, in v.3.9.2 everything works. Still, the reason I tried to upgrade is a need for MemoryCache which I'm unable to use in this version since registering Singleton instances is basically not working. P.S. The registration is done correctly: ```csharp services.AddScoped<TokenProviderFactory>(); services.AddScoped<AuthenticationHandler>(); services.AddHttpClient("clientName", client => { client.BaseAddress = new Uri("base address"); }).AddHttpMessageHandler<AuthenticationHandler>(); ```

MGrgov90

Hi @mbhoek any progress or recommendation about the described problem? Do you need any additional information, so that you can more easily check the described problem?

mbhoek

Thank you for your contribution. Unfortunately I have postponed working on this plugin until I get a better understanding of [the future of the SpecFlow project](https://github.com/SpecFlowOSS/SpecFlow/issues/2719). If they continue, all my focus will be on [supporting SpecFlow v4](https://github.com/solidtoken/SpecFlow.DependencyInjection/milestone/2).

mbhoek

Closing this issue because [the plugin will be donated to Reqnroll](https://github.com/solidtoken/SpecFlow.DependencyInjection/issues/106).