sashang
sashang

Reputation: 12224

How to get the connection string in the configureServices function in ASP .NET

I'm trying to use the identity service to manage logins for my application. I have the following

let configureServices (services : IServiceCollection) =
    // Configure InMemory Db for sample application        
    services.AddDbContext<IdentityDbContext<IdentityUser>>(
        fun options ->        
            options.UseInMemoryDatabase("NameOfDatabase") |> ignore
        ) |> ignore

However it uses the in memory database. I want to persist a users registration information, and I have postgresql setup and want to use that database to persist the information. I have the connectionString information in settings.json file. I want to change the above function to this:

let configureServices (services : IServiceCollection) =
    // Configure InMemory Db for sample application        
    services.AddDbContext<IdentityDbContext<IdentityUser>>(
        fun options ->        
            let config = ctx.GetService<IConfiguration>()
            let connString = config.Item("connectionString")
            options.UseNpgsql(connString) |> ignore
        ) |> ignore

but the problem is from within the configureServices function I don't have access to the Httpcontext (represented by ctx above) that handles the configuration of the application. How do I do this? Basically I want to get the value of the connectionString field form the settings.json file in my configureServices function.

Upvotes: 5

Views: 4029

Answers (3)

Ray
Ray

Reputation: 8871

It looks like you're using a Startup class where you want to access configuration in the ConfigureServices(IServiceCollection services) method.

If you created this class from scratch, it will not have a Configuration object available magically. You need to create a constructor it will be injected to and then store a reference to it somewhere. By default, it is done like this:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }
}

Now you can just use the Configuration property as expected in your ConfigureServices and other methods of the class:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContextFactory<IdentityDbContext<IdentityUser>>(options =>
        options.UseInMemoryDatabase(
            Configuration.GetConnectionString("MyConnectionStringKey")));
}

Upvotes: 1

Charles Mager
Charles Mager

Reputation: 26223

I'm going to assume you're using this function similarly to the below:

WebHost
    .CreateDefaultBuilder(args)
    .ConfigureServices(Action<_> configureServices)
    .Configure(Action<_> configureApp)

You'll find that there is an overload of this ConfigureServices method that takes an Action<WebHostBuilderContext, IServiceCollection>. So you can change your function to this:

let configureServices (context:WebHostBuilderContext) (services:IServiceCollection) =
    let config = context.Configuration
    let connString = config.GetConnectionString "DefaultConnection"

    services.AddDbContext<IdentityDbContext<IdentityUser>>(fun opts ->
        opts.UseNpgsql(connString) |> ignore
        ) |> ignore

And change the calling code to this (note Action<_> becomes Action<_,_>):

WebHost
    .CreateDefaultBuilder(args)
    .ConfigureServices(Action<_,_> configureServices)
    .Configure(Action<_> configureApp)

Upvotes: 4

Pooja Suryawanshi
Pooja Suryawanshi

Reputation: 341

Hope so u added following line of code into ur app.cofig file.

  <connectionStrings>
    <add name="DefaultConnection"
         connectionString="Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" />
  </connectionStrings>
</configuration>

Which stored the database connection string. In ConfigureServices() U have the access to Configuration Object which gives you the access of your application connection String from app.config file.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}

For more Details:-Click Here

Upvotes: 3

Related Questions