ASP.Net Core carrying external assembly

story background

  In general ASP.Net Coreproject configuration can be directly appsetting.jsonadded, 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 Corecarrying external assembly function, realize logic is achieved through external class IHostingStartupinterface enhancements at startup from an external assembly added to the application. For external projects we mentioned earlier to ASP.Net Coreadd the demand profile is how to achieve it? Nothing ASP.Net Coreperformed when starting 启动依赖程序集the characteristics specified in the class Configuremethods, and in the process will need to share configuration to ASP.Net Coreruntime.

The basic flow

External library assembly
  • Create a class library project HostingStartupLibrary.
  • Open the Nugetmanagement interface, then click Install Microsoft.AspNetCore.Hosting(2.2.7), Microsoft.Extensions.Configuration(3.0.0)package.
  • New classes bearer ServiceKeyInjection, implement IHostingStartupthe interface Configuremethod, 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 HostingStartupLibraryreferenced 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, find CreateHostBuildermethods.
      • In the UseStartup<Startup>()before, webBuilderafter the addition UseSetting(WebHostDefaults.HostingStartupAssembliesKey, "HostingStartupLibrary"), HostingStartupLibraryis the name of the external assembly.
    • Environment variable configuration
      • Open the launchSettings.jsonfile;
      • Find all environmentVariablesnodes below the node in the added ASPNETCORE_HOSTINGSTARTUPASSEMBLIESvalue of HostingStartupLibrary, i.e. external assembly name.
  • Configure get tested.
    • By constructor injection, the IConfigurationinjection into the controller.
    • By config["DevAccount_FromLibrary"]acquiring the configuration data form, whether or not correct.

Detailed interpretation

Guess you like

Origin www.cnblogs.com/fuxuyang/p/11819328.html