第二十六節:擴展如何在控制台中使用HttpClientFactory、讀取配置文件、數據保護、注入類、EFCore的使用


整體說明:這里主要是借助依賴注入程序,在控制台上獲取需要的相關類,然后進行使用對應方法的調用

(1).首先需要添加依賴注入的程序集【Microsoft.Extensions.DependencyInjection】

(2).添加所需組件對應的程序集,並進行Addxxxx

(3).創建Provicder

(4).利用Provider調用GetService獲取對應對象

PS:除了讀取配置文件的創建例外,其他都大致遵循上述規律。

1. HttpClientFactory

  添加程序集:【Microsoft.Extensions.Http】

 1           {
 2                 var serviceCollection = new ServiceCollection();
 3                 serviceCollection.AddHttpClient();
 4                 var serviceProvider = serviceCollection.BuildServiceProvider();
 5                 //var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();  //等價於以上三句話
 6                 IHttpClientFactory httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
 7 
 8                 //下面是使用
 9                 string url2 = "http://XXX:8055/Home/SendAllMsg";
10                 var client = httpClientFactory.CreateClient();
11                 var content = new StringContent("msg=123", Encoding.UTF8, "application/x-www-form-urlencoded");
12                 var response = client.PostAsync(url2, content).Result;
13                 string result = "";
14                 if (response.IsSuccessStatusCode)
15                 {
16                     result = response.Content.ReadAsStringAsync().Result;
17                 }
18             }

2. 數據保護

  添加程序集:【Microsoft.AspNetCore.DataProtection】

 1  {
 2                 var serviceCollection = new ServiceCollection();
 3                 serviceCollection.AddDataProtection();
 4                 var serviceProvider = serviceCollection.BuildServiceProvider();
 5                 var dataProtectionProvider = serviceProvider.GetRequiredService<IDataProtectionProvider>();
 6                
 7                 //下面是使用
 8                 var protector = dataProtectionProvider.CreateProtector("DataProtectionSample.Program");
 9                 string input = "ypf001";
10                 string protetedPayload = protector.Protect(input);
11                 string unprotectedPayload = protector.Unprotect(protetedPayload);
12 }

3. 注入對象(類與接口)

  比如可以利用AddSingleton聲明成單例的。

 1  public interface IUserService
 2  {
 3    string GetInfor();
 4  }
 5  public class UserService : IUserService
 6  {
 7    public string GetInfor()
 8    {
 9      return "ypf123";
10    }
11  }
12  {
13    var serviceCollection = new ServiceCollection();
14    serviceCollection.AddSingleton<IUserService, UserService>();
15    var serviceProvider = serviceCollection.BuildServiceProvider();
16    IUserService userService = serviceProvider.GetRequiredService<IUserService>();
17 
18    //下面是使用
19    var result = userService.GetInfor();
20 }

4. 讀取配置文件

  添加程序集:【Microsoft.Extensions.Configuration】和【Microsoft.Extensions.Configuration.Json】,並將配置文件屬性改為“始終復制”。

1  {
2      //注:配置文件要改成始終復制
3      var configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
4      var Configuration = configurationBuilder.Build();
5      //下面是使用
6      var data1 = Configuration["MyFullName"];
7  } 

 5. EFCore

  var optionsBuilder = new DbContextOptionsBuilder<ESHOPContext>();
  //傳入連接字符串           
  optionsBuilder.UseSqlServer(_Configuration.GetConnectionString("EFStr"));
  //創建EF上下文
  ESHOPContext context = new ESHOPContext(optionsBuilder.Options);

 

 

 

 

 

!

  • 作       者 : Yaopengfei(姚鵬飛)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 聲     明1 : 本人才疏學淺,用郭德綱的話說“我是一個小學生”,如有錯誤,歡迎討論,請勿謾罵^_^。
  • 聲     明2 : 原創博客請在轉載時保留原文鏈接或在文章開頭加上本人博客地址,否則保留追究法律責任的權利。
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM