第二十六节:扩展如何在控制台中使用HttpClientFactory、读取配置文件、数据保护、注入类

整体说明:这里主要是借助依赖注入程序,在控制台上获取需要的相关类,然后进行使用对应方法的调用

(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  } 

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 本人才疏学浅,用郭德纲的话说“我是一个小学生”,如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
 
原文地址:https://www.cnblogs.com/yaopengfei/p/12122623.html