diff --git a/DotNetCoreConfigurationStudy.sln b/DotNetCoreConfigurationStudy.sln new file mode 100644 index 0000000..ebbff77 --- /dev/null +++ b/DotNetCoreConfigurationStudy.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Study.WebApi", "Study.WebApi\Study.WebApi.csproj", "{9601789A-D64E-4A55-A328-FEDDFE8FDD2B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Study.XUnitTest", "Study.XUnitTest\Study.XUnitTest.csproj", "{F089A6B8-72B8-46D1-BFB9-8202D238E50F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9601789A-D64E-4A55-A328-FEDDFE8FDD2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9601789A-D64E-4A55-A328-FEDDFE8FDD2B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9601789A-D64E-4A55-A328-FEDDFE8FDD2B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9601789A-D64E-4A55-A328-FEDDFE8FDD2B}.Release|Any CPU.Build.0 = Release|Any CPU + {F089A6B8-72B8-46D1-BFB9-8202D238E50F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F089A6B8-72B8-46D1-BFB9-8202D238E50F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F089A6B8-72B8-46D1-BFB9-8202D238E50F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F089A6B8-72B8-46D1-BFB9-8202D238E50F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5ACAFD17-DAE5-4D86-BBA4-795056BE8D2F} + EndGlobalSection +EndGlobal diff --git a/Study.WebApi/Controllers/ValuesController.cs b/Study.WebApi/Controllers/ValuesController.cs new file mode 100644 index 0000000..20e621f --- /dev/null +++ b/Study.WebApi/Controllers/ValuesController.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace Study.WebApi.Controllers +{ + [Route("api/[controller]")] + public class ValuesController : Controller + { + // GET api/values + [HttpGet] + public IEnumerable Get() + { + return new string[] { "value1", "value2" }; + } + + // GET api/values/5 + [HttpGet("{id}")] + public string Get(int id) + { + return "value"; + } + + // POST api/values + [HttpPost] + public void Post([FromBody]string value) + { + } + + // PUT api/values/5 + [HttpPut("{id}")] + public void Put(int id, [FromBody]string value) + { + } + + // DELETE api/values/5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/Study.WebApi/Program.cs b/Study.WebApi/Program.cs new file mode 100644 index 0000000..f30bce3 --- /dev/null +++ b/Study.WebApi/Program.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +namespace Study.WebApi +{ + public class Program + { + public static void Main(string[] args) + { + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup() + .Build(); + } +} diff --git a/Study.WebApi/Properties/launchSettings.json b/Study.WebApi/Properties/launchSettings.json new file mode 100644 index 0000000..a087c3e --- /dev/null +++ b/Study.WebApi/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:7344/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Study.WebApi": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:7345/" + } + } +} diff --git a/Study.WebApi/Startup.cs b/Study.WebApi/Startup.cs new file mode 100644 index 0000000..4dbb863 --- /dev/null +++ b/Study.WebApi/Startup.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Study.WebApi +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseMvc(); + } + } +} diff --git a/Study.WebApi/Study.WebApi.csproj b/Study.WebApi/Study.WebApi.csproj new file mode 100644 index 0000000..d97fb14 --- /dev/null +++ b/Study.WebApi/Study.WebApi.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp2.0 + + + + + + + + + + + + + + + diff --git a/Study.WebApi/appsettings.Development.json b/Study.WebApi/appsettings.Development.json new file mode 100644 index 0000000..fa8ce71 --- /dev/null +++ b/Study.WebApi/appsettings.Development.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/Study.WebApi/appsettings.json b/Study.WebApi/appsettings.json new file mode 100644 index 0000000..26bb0ac --- /dev/null +++ b/Study.WebApi/appsettings.json @@ -0,0 +1,15 @@ +{ + "Logging": { + "IncludeScopes": false, + "Debug": { + "LogLevel": { + "Default": "Warning" + } + }, + "Console": { + "LogLevel": { + "Default": "Warning" + } + } + } +} diff --git a/Study.XUnitTest/App.config b/Study.XUnitTest/App.config new file mode 100644 index 0000000..49cc43e --- /dev/null +++ b/Study.XUnitTest/App.config @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Study.XUnitTest/App.json b/Study.XUnitTest/App.json new file mode 100644 index 0000000..b1ad992 --- /dev/null +++ b/Study.XUnitTest/App.json @@ -0,0 +1,11 @@ +{ + "AppConfiguration": { + "MainWindow": { + "Height": "400", + "Width": "600", + "Top": "0", + "Left": "0" + }, + "ConnectionString":"Server=(localdb)\\\\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true" + } +} \ No newline at end of file diff --git a/Study.XUnitTest/ConfigrationTest.cs b/Study.XUnitTest/ConfigrationTest.cs new file mode 100644 index 0000000..b0960e4 --- /dev/null +++ b/Study.XUnitTest/ConfigrationTest.cs @@ -0,0 +1,48 @@ +using System; +using System.Linq; +using Microsoft.Extensions.Configuration; +using Xunit; + +namespace Study.XUnitTest +{ + public class ConfigrationTest + { + [Fact] + public void Test1() + { + CustomProviderStudy sudyProvider=new CustomProviderStudy(); + var customConfiguration= sudyProvider.BuildConfigurationFromInMemoryCollection(); + + Assert.Equal(Environment.UserName, customConfiguration["Profile:UserName"]); + Assert.Equal("love", customConfiguration["Profile:UserName"]); + + Assert.Equal(400, customConfiguration.GetValue("AppConfiguration:MainWindow:Height")); + Assert.Equal(600, customConfiguration.GetValue("AppConfiguration:MainWindow:Width")); + Assert.Equal(0, customConfiguration.GetValue("AppConfiguration:MainWindow:Top")); + Assert.Equal(0, customConfiguration.GetValue("AppConfiguration:MainWindow:Left")); + Assert.Contains("localdb", customConfiguration.GetValue("AppConfiguration:ConnectionString")); + } + + [Fact] + public void AppJsonTest() + { + CustomProviderStudy sudyProvider = new CustomProviderStudy(); + var appJsonConfiguration = sudyProvider.BuildProviderFromAppJson(); + + Assert.NotNull(appJsonConfiguration); + + //Assert.True(appJsonConfiguration.GetSection("club") != null); + } + + [Fact] + public void BuildJsonTest() + { + CustomProviderStudy customProvider=new CustomProviderStudy(); + var cc = customProvider.BuildProviderFromJson(); + var dd= cc.GetChildren(); + + Assert.NotNull(cc); + Assert.NotNull(dd); + } + } +} diff --git a/Study.XUnitTest/CustomProviderStudy.cs b/Study.XUnitTest/CustomProviderStudy.cs new file mode 100644 index 0000000..19febda --- /dev/null +++ b/Study.XUnitTest/CustomProviderStudy.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.Json; +using Microsoft.Extensions.Configuration.FileExtensions; +using Microsoft.Extensions.Configuration.EnvironmentVariables; +using Microsoft.Extensions.Configuration.Memory; +using Microsoft.Extensions.Configuration.Binder; +using Microsoft.Extensions.Configuration.CommandLine; +using Microsoft.Extensions.Configuration.Ini; +using Microsoft.Extensions.Configuration.Xml; +using Microsoft.Extensions.Configuration.UserSecrets; + +namespace Study.XUnitTest +{ + /// + /// ConfigurationProvider 使用学习 + /// Microsoft.Extensions.Configuration 可用Nuget获取 + /// InMemoryConfigurationProvider + /// 适用于 Config.json 的 JsonFileConfigurationProvider + /// 适用于 Config.Production.json 的 JsonFileConfigurationProvider + /// EnvironmentVariableConfigurationProvider + /// CommandLineConfigurationProvider + /// + /// https://msdn.microsoft.com/magazine/mt632279 + /// https://www.cnblogs.com/artech/p/asp-net-core-config-4-1.html + public class CustomProviderStudy + { + + public IConfiguration ConfigurationFromDictionary() + { + Dictionary source = new Dictionary + { + ["LongDatePattern"] = "dddd, MMMM d, yyyy", + ["LongTimePattern"] = "h:mm:ss tt", + ["ShortDatePattern"] = "M/d/yyyy", + ["ShortTimePattern"] = "h:mm tt" + }; + ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddInMemoryCollection(GetDictionary()); + var configuration = configurationBuilder.Build(); + return configuration; + } + + public IConfiguration BuildConfigurationFromInMemoryCollection() + { + ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); + + // 添加默认连接 内存中.NET对象 + configurationBuilder.AddInMemoryCollection(GetDictionary()); + IConfiguration configuration = configurationBuilder.Build(); + + return configuration; + } + + public IConfiguration BuildProviderFromObject() + { + ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); + configurationBuilder + .AddInMemoryCollection(GetDictionary()) + .AddJsonFile("Config.json", true) + .AddEnvironmentVariables("EssentialDotNetConfiguration") + .AddIniFile("", true, true) + .AddXmlFile("", true, true) + .AddCommandLine(new string[]{"1","2"}); + return configurationBuilder.Build(); + } + + public IConfigurationProvider BuildProviderFromXml() + { + + return null; + } + + public IConfiguration BuildProviderFromIni() + { + return null; + } + + public IConfiguration BuildProviderFromAppCOnfig() + { + return null; + } + + public IConfiguration BuildProviderFromJson() + { + JsonConfigurationSource source = new JsonConfigurationSource(); + source.Path = "App.json"; + JsonConfigurationProvider jsonConfigurationProvider = new JsonConfigurationProvider(source); + jsonConfigurationProvider.Load(); + return null; + } + + public IConfiguration BuildProviderFromAppJson() + { + return null; + } + + /// + /// 字典 + /// + private static IReadOnlyDictionary GetDictionary() + { + IReadOnlyDictionary DefaultConfigurationStrings = + new Dictionary() + { + ["Profile:UserName"] = Environment.UserName, + [$"AppConfiguration:ConnectionString"] = GetDefaultionString(), + [$"AppConfiguration:MainWindow:Height"] = "400", + [$"AppConfiguration:MainWindow:Width"] = "600", + [$"AppConfiguration:MainWindow:Top"] = "0", + [$"AppConfiguration:MainWindow:Left"] = "0", + }; + + return DefaultConfigurationStrings; + } + + /// + /// MsSQL连接字串 + /// + private static string GetDefaultionString() + { + return @"Server = (localdb)\\mssqllocaldb; Database = SampleData; Trusted_Connection = True; MultipleActiveResultSets = true"; + } + } +} diff --git a/Study.XUnitTest/Study.XUnitTest.csproj b/Study.XUnitTest/Study.XUnitTest.csproj new file mode 100644 index 0000000..972365e --- /dev/null +++ b/Study.XUnitTest/Study.XUnitTest.csproj @@ -0,0 +1,37 @@ + + + + netcoreapp2.0 + + false + + + + + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + Always + + + +