forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDI_Test.cs
More file actions
151 lines (128 loc) · 6.06 KB
/
DI_Test.cs
File metadata and controls
151 lines (128 loc) · 6.06 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.DynamicProxy;
using AutoMapper;
using Blog.Core.AuthHelper;
using Blog.Core.Common;
using Blog.Core.Common.AppConfig;
using Blog.Core.Common.DB;
using Blog.Core.Common.LogHelper;
using Blog.Core.Common.Seed;
using Blog.Core.Extensions;
using Blog.Core.IRepository.Base;
using Blog.Core.Repository.Base;
using Blog.Core.Repository.MongoRepository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Claims;
using System.Text;
using Microsoft.Extensions.Logging;
namespace Blog.Core.Tests
{
public class DI_Test
{
/// <summary>
/// 连接字符串
/// Blog.Core
/// </summary>
public static MutiDBOperate GetMainConnectionDb()
{
var mainConnetctDb = BaseDBConfig.MutiConnectionString.allDbs.Find(x => x.ConnId == MainDb.CurrentDbConnId);
if (BaseDBConfig.MutiConnectionString.allDbs.Count > 0)
{
if (mainConnetctDb == null)
{
mainConnetctDb = BaseDBConfig.MutiConnectionString.allDbs[0];
}
}
else
{
throw new Exception("请确保appsettigns.json中配置连接字符串,并设置Enabled为true;");
}
return mainConnetctDb;
}
public IContainer DICollections()
{
var basePath = AppContext.BaseDirectory;
IServiceCollection services = new ServiceCollection();
services.AddAutoMapper(typeof(Startup));
services.AddSingleton(new AppSettings(basePath));
services.AddSingleton(new LogLock(basePath));
services.AddScoped<DBSeed>();
services.AddScoped<MyContext>();
//读取配置文件
var symmetricKeyAsBase64 = AppSecretConfig.Audience_Secret_String;
var keyByteArray = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);
var signingKey = new SymmetricSecurityKey(keyByteArray);
var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
var permission = new List<PermissionItem>();
var permissionRequirement = new PermissionRequirement(
"/api/denied",
permission,
ClaimTypes.Role,
AppSettings.app(new string[] { "Audience", "Issuer" }),
AppSettings.app(new string[] { "Audience", "Audience" }),
signingCredentials,//签名凭据
expiration: TimeSpan.FromSeconds(60 * 60)//接口的过期时间
);
services.AddSingleton(permissionRequirement);
//【授权】
services.AddAuthorization(options =>
{
options.AddPolicy(Permissions.Name,
policy => policy.Requirements.Add(permissionRequirement));
});
services.AddScoped<SqlSugar.ISqlSugarClient>(o =>
{
return new SqlSugar.SqlSugarScope(new SqlSugar.ConnectionConfig()
{
ConnectionString = GetMainConnectionDb().Connection,//必填, 数据库连接字符串
DbType = (SqlSugar.DbType)GetMainConnectionDb().DbType,//必填, 数据库类型
IsAutoCloseConnection = true,//默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
InitKeyType = SqlSugar.InitKeyType.SystemTable//默认SystemTable, 字段信息读取, 如:该属性是不是主键,标识列等等信息
});
});
//实例化 AutoFac 容器
var builder = new ContainerBuilder();
//builder.RegisterType<AdvertisementServices>().As<IAdvertisementServices>();
builder.RegisterInstance(new LoggerFactory())
.As<ILoggerFactory>();
builder.RegisterGeneric(typeof(Logger<>))
.As(typeof(ILogger<>))
.SingleInstance();
//指定已扫描程序集中的类型注册为提供所有其实现的接口。
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>)).InstancePerDependency(); //注册仓储
builder.RegisterGeneric(typeof(MongoBaseRepository<>)).As(typeof(IMongoBaseRepository<>)).InstancePerDependency(); //注册仓储
// 属性注入
var controllerBaseType = typeof(ControllerBase);
builder.RegisterAssemblyTypes(typeof(Startup).Assembly)
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
.PropertiesAutowired();
var servicesDllFile = Path.Combine(basePath, "Blog.Core.Services.dll");
var assemblysServices = Assembly.LoadFrom(servicesDllFile);
builder.RegisterAssemblyTypes(assemblysServices)
.AsImplementedInterfaces()
.InstancePerLifetimeScope()
.PropertiesAutowired()
.EnableInterfaceInterceptors();
var repositoryDllFile = Path.Combine(basePath, "Blog.Core.Repository.dll");
var assemblysRepository = Assembly.LoadFrom(repositoryDllFile);
builder.RegisterAssemblyTypes(assemblysRepository)
.PropertiesAutowired().AsImplementedInterfaces();
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
services.AddAutoMapperSetup();
//将services填充到Autofac容器生成器中
builder.Populate(services);
//使用已进行的组件登记创建新容器
var ApplicationContainer = builder.Build();
return ApplicationContainer;
}
}
}