.net core Autofac IOC 容器的简单使用


书接上回,介绍了.net core 读取配置文件的几种方式,本文学习Autofac的同时再次增加一种读取配置文件的方法。

本文介绍Auofac,一个优秀的.NET IOC框架

源码地址:https://github.com/autofac/Autofac

1、打开NuGet包管理器安装Autofac.Extensions.DependencyInjection包

img

2、Program.cs 启用Autofac

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) // 启用Autofac
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}

3、Startup.cs 新增Autofac并初始化容器

img

1
2
3
4
5
6
7
8
9
10
// 一、autofac 新增
public ILifetimeScope AutofacContainer { get; private set; }

/// <summary>
/// 二、将内容直接注册到AutofacContainerBuilder中
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
}

img

1
2
3
// 三、autofac 新增 可选
AutofacContainer = app.ApplicationServices.GetAutofacRoot();
IoCContainer.InitContainer(AutofacContainer);

4、新建Autofac IOC 容器类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/// <summary>
/// Autofac IOC 容器
/// </summary>
public class IoCContainer
{
private static ILifetimeScope _container;
/// <summary>
/// 初始化容器
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static void InitContainer(ILifetimeScope autofacContainer)
{
_container = autofacContainer;
}
/// <summary>
/// 从容器中获取对象 Resolve an instance of the default requested type from the container
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <returns></returns>
public static T Resolve<T>()
{
return _container.Resolve<T>();
}
}

5、新建读取配置文件工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// <summary>
/// 配置文件读取操作
/// </summary>
public class Configs
{
private static readonly IConfiguration configuration;
static Configs()
{
configuration = IoCContainer.Resolve<IConfiguration>();
}
/// <summary>
/// 根据Key获取数配置内容
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static IConfigurationSection GetSection(string key)
{
return configuration.GetSection(key);
}
/// <summary>
/// 根据section和key获取配置内容
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetConfigurationValue(string section, string key)
{
return GetSection(section)?[key];
}
/// <summary>
/// 根据Key获取数据库连接字符串
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetConnectionString(string key)
{
return configuration.GetConnectionString(key);
}
}

6、新建控制器,使用!读取配置的另一种方式来啦!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
/// <summary>
/// 读取内容
/// </summary>
[HttpGet, HttpPost]
public void GetContent()
{
var url = Configs.GetSection("Setting:Url").Value; // http://localhost:8080/
var name = Configs.GetConfigurationValue("Setting", "Name"); // localhost
}
}

以上就是.net core AutoFac的简单学习使用 + 读取配置文件的新方式的介绍,做此记录,如有帮助,欢迎点赞关注收藏!


文章作者: GoodTimeGGB
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 GoodTimeGGB !
评论
  目录