public static string GetConnectionString(string str) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); return(Configuration.GetConnectionString(str)); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) // Подключение используемых в проекте сервисов (порядок, сука, важен!!!!) { services.AddDbContext <AppDBContent>(options => options.UseSqlServer(ConfString.GetConnectionString("DefaultConnection"))); // Подключение SQL сервака services.AddTransient <IAllCars, CarsRepository>(); // Создание связи между интерфейсом и репозиторием для того, что бы к уже реализованным методам можно было обращаться через интерфейс services.AddTransient <ICarsCategory, CategoryRepository>(); services.AddMvc(option => option.EnableEndpointRouting = false); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped(sp => ShopCart.GetCart(sp)); services.AddMvc(); services.AddMemoryCache(); services.AddSession(); }
} // End Function EscapeString static void Main(string[] args) { string ms = GetMsCon(); string pg = GetPgCon(); ms = EscapeString(ms); pg = EscapeString(pg); // DESKTOP-4P9UFE8 System.Console.WriteLine($"MS: {ms}\r\nPG: {pg}"); ConsoleEnvironment env = new ConsoleEnvironment(); // https://stackoverflow.com/questions/40169673/read-appsettings-in-asp-net-core-console-application // Microsoft.Extensions.Configuration // Microsoft.Extensions.Configuration.Json // Set to copy to output Microsoft.Extensions.Configuration.IConfigurationBuilder builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddJsonFile($"appsettings.{env.MachineName}.json", optional: true) .AddEnvironmentVariables() .AddUserSecrets() ; // myc - connection // time added - dbtype // System.Collections.Generic.Dictionary<System.Guid, System.Data.Common.DbConnection> didi; // System.Collections.Generic.Dictionary<System.Guid, CoreDb.ReadDAL> didi; // new { DAL = CoreDb.ReadDAL, Con=null, dateTimeAdded} // '...Visual Studio 2017\Projects\Loggy\CoreDbTest\bin\Debug\netcoreapp1.0\appsettings.json'.' Config = builder.Build(); Console.WriteLine($"option1 = {Config.GetSection("ConnectionStrings")[env.MachineName]}"); System.Collections.Generic.List <string> list = new System.Collections.Generic.List <string>(); foreach (IConfigurationSection kvp in Config.GetSection("ConnectionStrings").GetChildren()) { System.Console.WriteLine($"{kvp.Key}:\t{kvp.Value}"); if (System.StringComparer.OrdinalIgnoreCase.Equals(kvp.Key, env.MachineName)) { System.Console.WriteLine(kvp.Value); } } // Next kvp foreach (IConfigurationSection kvp in Config.GetSection("ConnectionStrings").GetChildren()) { System.Console.WriteLine($"{kvp.Key}:\t{kvp.Value}"); if (System.StringComparer.OrdinalIgnoreCase.Equals(kvp.Key, env.MachineName)) { System.Console.WriteLine(kvp.Value); } } // Next kvp // CoreDb.DalConfig conme = new CoreDb.DalConfig("constring"); CoreDb.DalConfig con = new CoreDb.DalConfig( delegate(CoreDb.DalConfig config) { string sectionName = Config.GetSection("DbProviderName").Value; if (sectionName == null) { sectionName = "ConnectionStrings"; } string cs = Config.GetSection(sectionName).GetSection(env.MachineName).Value; if (cs == null) { cs = Config.GetSection(sectionName).GetSection("DefaultConnection").Value; } if (cs == null) { throw new System.IO.InvalidDataException("No connection string found"); } return(cs); } ); System.Console.WriteLine(con.ConnectionString); string strJSO = Newtonsoft.Json.JsonConvert.SerializeObject(Config, Newtonsoft.Json.Formatting.Indented); System.Console.WriteLine(strJSO); // Overwrites configuration, but one value at a time, // does not replace entire section. string foo = Config.GetConnectionString("PG3"); System.Console.WriteLine(foo); foo = Config.GetConnectionString("DefaultConnection"); System.Console.WriteLine(foo); foo = Config.GetConnectionString("DefaultConnection2"); System.Console.WriteLine(foo); Console.WriteLine("Hello World!"); }