With ASP.NET Core's Options pattern one can create Service and register it with two separate calls.
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<MyService>();
services.Configure<MyServiceOptions>(o => o.Param = 1);
services.AddMvc();
};
However, I am entirely unclear on how and if it is possible to instantiate two instances of a service and bind different options to them? i.e. given two specialisations of some base class, how do we share a single options class between them?
public class MyService {}
public class MyService1 : MyService {}
public class MyService2 : MyService2 {}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<MyService1>();
services.AddTransient<MyService2>();
// What goes here?
// config for instance 1
//services.Configure<MyServiceOptions>(o => o.Param = 1);
// config for instance 2
//services.Configure<MyServiceOptions>(o => o.Param = 2);
services.AddMvc();
};
Basically I want something like the IServiceCollection.AddDbContext extension method, but for services, and I've looked at the EF Core extension methods and I don't get them at all.
MyServiceOptionsgeneric and register it twice asMyServiceOptions<MyService1>andMyServiceOptions<MyService2>?