Yola
Yola

Reputation: 19033

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.

Upvotes: 1

Views: 1245

Answers (1)

Roman.Pavelko
Roman.Pavelko

Reputation: 1665

Sure, here is an example:

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

Upvotes: 1

Related Questions