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?