ABP Vnext introduced JSON profile (AddJsonFile) method

Method JSON external profile (AddJsonFile) of ABP Vnext introduced.

ABP comprises a dynamic dependency injection profile by JSON

 public class WPFAppModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = BuildConfiguration();
            //var configuration = context.Services.GetConfiguration();

            context.Services.AddFromConfigurationFile(configuration.GetSection("Services"));
   
        }
        private static IConfigurationRoot BuildConfiguration()
        {
            return new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("configuration.json", optional: true, reloadOnChange: true)
                //.AddJsonFile("autofac.json", optional: false, reloadOnChange: true)
                .Build();
        }
    }

//configuration.json file

{
  // Description: "Realization implementation class namespace class name, where the assembly name"
  "Services": {
    "the Singleton": [

    ],
    "Transient": [
      {
        "Service": "WpfApp1.ISampleService,WpfApp1",
        "Implementation": "WpfApp1.SampleService,WpfApp1"
      }
    ],
    "AddScoped": [

    ]
  }
}

 

ABP injected through dynamic profile

https://blog.csdn.net/xhl_james/article/details/90700217

 

 


namespace WpfApp1
{
    public class ServicesConfiguration
    {
        public IEnumerable<ServiceItem> Singleton { get; set; }
        public IEnumerable<ServiceItem> Transient { get; set; }
        public IEnumerable<ServiceItem> AddScoped { get; set; }
    }
    public class ServiceItem
    {
        public string Service { get; set; }
        public string Implementation { get; set; }
    }
    public static class AddFromConfigurationFileExtension
    {
        /// <summary>
        /// 通过配置文件进行注入实现。
        /// </summary>
        /// <remarks>
        /// should be noted that the adoption of the configuration file to configure, then make sure interface, and the corresponding assembly
        /// typeof (AddFromConfigurationFileExtension) .Assembly assembly with the directory.
        /// Type.GetType otherwise would not find the corresponding class.
        /// </ Remarks>
        /// <param name = "Services"> </ param>
        /// <param name = "Configuration"> </ param>
        /// <Returns> </ Returns>
        public static IServiceCollection AddFromConfigurationFile (the this IServiceCollection Services,
        IConfigurationSection Configuration)
        {
            var servicesConfiguration = configuration.Get <ServicesConfiguration> ();
            the Type @interface = null, Implementation = null;
            IF (servicesConfiguration.

                the foreach (var-Service in servicesConfiguration.Singleton)
                {
                    @interface the Type.GetType = (service.Service, to false);
                    Implementation = the Type.GetType (service.Implementation, to false);
                    IF (@interface == null)
                        the throw new new ArgumentNullException The ($ "{service.Service} not found please contact the administrator.!", NameOf (@interface));
                    IF (@interface == null)
                        the throw new new ArgumentNullException ($ "} {service.Implementation not found please and administrators. Contact ", NameOf (Implementation));!
                    services.AddSingleton (@interface, Implementation);
                }
            }
            IF (servicesConfiguration.Transient! = null)
            {
                foreach (var Service in servicesConfiguration.Transient)
                {
                    // ensure that properly registered. To prevent the latter influence function.
                    = the Type.GetType @interface (service.Service, to false);
                    Implementation = the Type.GetType (service.Implementation, to false);
                    IF (@interface == null)
                        the throw new new ArgumentNullException The ($ "} {service.Service not found, please. and administrator! ", NameOf (@interface));
                    IF (@interface == null)
                        the throw new new ArgumentNullException ($"} {service.Implementation not found please contact the administrator ", nameof (implementation.!) );
                    services.AddTransient (@interface, Implementation);
                }
            }
            IF (! servicesConfiguration.AddScoped = null)
            {
                the foreach (var servicesConfiguration.AddScoped in-Service)
                {
                    // ensure proper registration. To prevent the latter influence function.
                    = the Type.GetType @interface (service.Service, to false);
                    Implementation = the Type.GetType (service.Implementation, to false);
                    IF (@interface == null)
                        the throw new new ArgumentNullException The ($ "} {service.Service not found, please. and administrator ", NameOf (@interface));!
                    IF (@interface == null)
                        throw new ArgumentNullException ($ "{service.Implementation } not found please contact the administrator.!", NameOf (Implementation));
                    services.AddScoped (@interface, Implementation);
                }
            }
            return Services;
        }
    }

}
 

Published 181 original articles · won praise 35 · views 760 000 +

Guess you like

Origin blog.csdn.net/dacong/article/details/105150279