Skip to content
This repository was archived by the owner on Nov 17, 2018. It is now read-only.
This repository was archived by the owner on Nov 17, 2018. It is now read-only.

Creating a scoped service with an HttpClientFactory #134

Closed
@twsouthwick

Description

@twsouthwick

I really like the idea of HttpClientFactory, but have found issues that the convenience methods assume you have singleton instances of the services required. For instance, the following fails:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<Test2>(); // Works if I switch this to AddSingleton
    services.AddHttpClient<Test1>();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

public class Test1
{
   public Test1(HttpClient client, Test2 test2)
   {
   }
}

public class Test2 { }

This results in the following:

InvalidOperationException: Cannot resolve scoped service 'WebApplication1.Startup+Test2' from root provider.

Can there be a way to set the lifetime of the scope? In this example, I want to set Test1 as a singleton.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions