Search code examples
c#asp.net-coredependency-injectionconnection-stringconstructor-injection

How to inject connection string using ASP.NET Core DI


With Unity one can do something like this:

.RegisterType<IControllerFactory, MyControllerFactory>(
    new InjectionConstructor("connectionstring goes here"));

Is it possible to do the same in ASP.NET Core?

UPD: I have controllers in a separate assembly where I would like to keep my presentation model that's why I'm substituting the controller factory. It needs to create different stuff in order to create controllers and this requires connection string.


Solution

  • Sure, here is an example:

    services.AddScoped<IContextFactory, ContextFactory>(provider => 
        new ContextFactory(server, databaseName, username, password));