Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 740
sashang Avatar asked Jul 04 '18 03:07

sashang


People also ask

Where is connection string in asp net core application?

In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings. json , an environment variable, the user secret store, or another configuration source. See the Configuration section of the ASP.NET Core documentation for more details.

How do I get connection string?

Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.


3 Answers

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

like image 100
Pooja Suryawanshi Avatar answered Sep 17 '22 23:09

Pooja Suryawanshi


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)
like image 37
Charles Mager Avatar answered Sep 18 '22 23:09

Charles Mager


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")));
}
like image 26
Ray Avatar answered Sep 20 '22 23:09

Ray