Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read connectionstring outside startup from appsetting.json in vNext

Tags:

I have a project class (Nuget Package). I need to read in a static class without constructor my connections string to MongoDB.

Static Class Method:

        /// <summary>
        /// The default key MongoRepository will look for in the appsettings.json 
        /// </summary>
        private const string DefaultConnectionstringName = "Data:MongoDB:MongoServerSettings";

        /// <summary>
        /// Retrieves the default connectionstring from appsettings.json
        /// </summary>
        /// <returns>Returns the default connectionstring from the App.config or Web.config file.</returns>
        public static string GetDefaultConnectionString()
        {
            var config = new Configuration();
            return config.Get<string>(DefaultConnectionstringName);
        }

I have always null... How can I obtain the value outside the Startup.cs without using DI?

It is possible?

In my old code I could do something like that:

/// <summary>
    /// Retrieves the default connectionstring from the App.config or Web.config file.
    /// </summary>
    /// <returns>Returns the default connectionstring from the App.config or Web.config file.</returns>
    public static string GetDefaultConnectionString()
    {
        return ConfigurationManager.ConnectionStrings[DefaultConnectionstringName].ConnectionString;
    }

Thanks!!

like image 898
chemitaxis Avatar asked Dec 14 '15 14:12

chemitaxis


2 Answers

1.ConnectionString in appsetting.json

enter image description here

  1. Creating Singleton Instance of Configuration in Startup.cs class

enter image description here

  1. Reading Values from Configuration Instance Using Dependency Injection [Constructor injection ]

enter image description here

  1. Finally Output

enter image description here

like image 135
Saineshwar Avatar answered Sep 20 '22 03:09

Saineshwar


Inside your startup, you should save the connection string to a static property on Startup

public class Startup
{
    public static string ConnectionString { get; private set; }

    public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddUserSecrets();

        if (env.IsDevelopment())
        {
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            builder.AddUserSecrets();
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
        ConnectionString = Configuration.Get<string>("Data:MongoDB:MongoServerSettings");
    }
    // ...
}

Then you should be able to access it from wherever:

public static string GetDefaultConnectionString()
{
    return Startup.ConnectionString;
}
like image 25
armen.shimoon Avatar answered Sep 19 '22 03:09

armen.shimoon