story background
In general ASP.Net Core
project configuration can be directly appsetting.json
added, you can add a new profile in the project. But if you want to achieve configuration files and other items with universal it? We can use absolute positioning to access the configuration file, but you may experience problems such access; we can also be achieved through the development of the configuration file access interface, but too much trouble, but could not add a configuration I went to change once the access code. So, there is wood there to provide what the official program it?
Yes, Microsoft officials provided to allow ASP.Net Core
carrying external assembly function, realize logic is achieved through external class IHostingStartup
interface enhancements at startup from an external assembly added to the application. For external projects we mentioned earlier to ASP.Net Core
add the demand profile is how to achieve it? Nothing ASP.Net Core
performed when starting 启动依赖程序集
the characteristics specified in the class Configure
methods, and in the process will need to share configuration to ASP.Net Core
runtime.
The basic flow
External library assembly
- Create a class library project
HostingStartupLibrary
. - Open the
Nuget
management interface, then click InstallMicrosoft.AspNetCore.Hosting(2.2.7)
,Microsoft.Extensions.Configuration(3.0.0)
package. New classes bearer
ServiceKeyInjection
, implementIHostingStartup
the interfaceConfigure
method, the data part is added to the memory.using System.Collections.Generic; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; [assembly: HostingStartup(typeof(HostingStartupLibrary.ServiceKeyInjection))] namespace HostingStartupLibrary { public class ServiceKeyInjection : IHostingStartup { var dict = new Dictionary<string, string>() { {"DevAccount_FromLibrary", "DEV_1111111-1111"} }; //配置方法一:主项目配置优先加载,再加载当前配置。 builder.ConfigureAppConfiguration(config => { config.AddInMemoryCollection(dict); }); //配置方法二:当前配置优先加载,再加载主项目配置。 //var builderConfig = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); //builder.UseConfiguration(builderConfig); } }
ASP.Net Core main project
- Add a class library project
HostingStartupLibrary
referenced dll files you can also reference the class library project compiled directly. - Configuration
主机启动依赖程序集
, there are two possible configurations: host configuration and configuration environment variable. If both host provided configuration and configuration environment variable, then the actual use of host configuration control.- Host Configuration
- Open
Program.cs
, findCreateHostBuilder
methods. - In the
UseStartup<Startup>()
before,webBuilder
after the additionUseSetting(WebHostDefaults.HostingStartupAssembliesKey, "HostingStartupLibrary")
,HostingStartupLibrary
is the name of the external assembly.
- Open
- Environment variable configuration
- Open the
launchSettings.json
file; - Find all
environmentVariables
nodes below the node in the addedASPNETCORE_HOSTINGSTARTUPASSEMBLIES
value ofHostingStartupLibrary
, i.e. external assembly name.
- Open the
- Host Configuration
- Configure get tested.
- By constructor injection, the
IConfiguration
injection into the controller. - By
config["DevAccount_FromLibrary"]
acquiring the configuration data form, whether or not correct.
- By constructor injection, the