.Net Standard (.Net Core) achieve access to configuration information

I. Introduction

.Net Framework in the framework of a special method for obtaining webconfig configured for our use, but in the method of .Net Core or .Net Standard is not directly used to obtain profile information, the following will be implemented for configuration information.

Second, realize obtain configuration information

In .Net Core, and his carrier configuration information is a json file, we now plan all items (include .Net Framework and .Net Standard (.Net Core) frame) are json file as a carrier configuration.

First, loading the following packets through Nuget:

Install-Package Microsoft.Extensions.Configuration
Install-Package Microsoft.Extensions.Configuration.Json
Install-Package Microsoft.Extensions.DependencyInjection
Install-Package Microsoft.Extensions.Options
Install-Package Microsoft.Extensions.Options.ConfigurationExtensions

Now we use the contents of json configuration file format has it:

{
  "ConnectionStrings": {
    "CxyOrder": "Server=LAPTOP-AQUL6MDE\\MSSQLSERVERS;Database=CxyOrder;User ID=sa;Password=123456;Trusted_Connection=False;"
  },
  "Appsettings": {
    "SystemName": "PDF .NET CORE",
    "Date": "2017-07-23",
    "Author": "PDF"
  },
  "ServiceUrl": "https://www.baidu.com/getnews"
}

Creating PFTConfiguration.cs file, the code is as follows:

 public class PFTConfiguration
    {
        /// <summary>
        /// PFTConfiguration.Configuration.GetConnectionString("CxyOrder"); 
        /// PFTConfiguration.Configuration["ServiceUrl"];
        /// PFTConfiguration.Configuration["Appsettings:SystemName"];
        /// </summary>
        public static IConfiguration Configuration { get; set; }
        static PFTConfiguration()
        {
            Configuration = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
            .Build();
        }

        /// <Summary> 
        /// configuration information 
        /// </ Summary> 
        /// <param name = "path"> JSON File Type </ param> 
        /// <typeParam name = "T"> return entities type </ typeParam> 
        /// <param name = "Key"> JSON keyword </ param> 
        /// <Returns> </ Returns> 
        public  static T GetAppsettings <T> (String Key, String path) WHERE T: class , new new () 
        { the try 
            { IF (string.IsNullOrWhiteSpace (path)) 
                { the throw new new Exception ( " parsing misconfigured profile path is empty "); 
                } IF (String.IsNullOrWhiteSpace(key))
                {thrownew
            
                
                     
                
                      Exception("解析配置错误,配置key为空");
                }
                var config = new ConfigurationBuilder()
                    .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                    .Add(new JsonConfigurationSource { Path = path, ReloadOnChange = true })
                    .Build();
                var appconfig = new ServiceCollection()
                    .AddOptions()
                    .Configure<T>(config.GetSection(key))
                    .BuildServiceProvider()
                    .GetService<IOptions<T>>()
                    .Value; return AppConfig; 
            }
                
            the catch (Exception EX) 
            { the throw new new Exception ( " Resolution Configuration (Object) abnormal ", EX); 
            } 
        } /// <Summary> /// configuration information /// </ Summary> /// <param name = "key"> json keyword </ param> /// <param name = "path"> JSON file type </ param> /// <Returns> </ Returns> public static String GetAppsettings (String Key, String path) 
        { the try 
            { IF (string.IsNullOrWhiteSpace (path)) 
                { the throw new new Exception ( " parsing misconfigured profile path is empty ");
                }if
                 



        
        
        
        
        
        
         
            
                
                     
                 (string.IsNullOrWhiteSpace (key)) 
                { the throw new new Exception ( " parsing configuration error configured key blank "); 
                } var config = new new ConfigurationBuilder () 
                    . setBasePath (AppDomain.CurrentDomain.BaseDirectory) 
                    .Add ( new new JsonConfigurationSource the path = {path, ReloadOnChange = to true , = Optional The to false }) 
                    .build (); var AppConfig = config.GetSection (Key) .Value;
                 return
                     
                
                AppConfig; 
            } the catch (Exception EX)
            
            { The throw new new Exception ( " Resolution Configuration (string) abnormal ", EX); 
            } 
        } 
    } 
}
                 

Which defines three acquisition method

1, PFTConfiguration.Configuration [ "Appsettings: SystemName"] default file path appsettings.json, and is acquired by node a manner somewhat similar to the method of obtaining configuration .Net Framework

2, PFTConfiguration.GetAppsettings <T> (string key, string path), he is to obtain the specified configuration file, the specified node, the node returns the contents of the object in the form of T

3, PFTConfiguration.GetAppsettings (string key, string path), he is to obtain the specified configuration file, specify the node, the node returns the contents of a string

Guess you like

Origin www.cnblogs.com/snailblog/p/11565907.html