mirror of
https://github.com/anjoy8/Blog.Core.git
synced 2024-09-20 23:48:27 +08:00
Update Startup.cs
This commit is contained in:
parent
5db2079796
commit
93e02fcc53
|
@ -96,88 +96,76 @@ namespace Blog.Core
|
|||
public void ConfigureContainer(ContainerBuilder builder)
|
||||
{
|
||||
var basePath = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath;
|
||||
|
||||
//注册要通过反射创建的组件
|
||||
//builder.RegisterType<AdvertisementServices>().As<IAdvertisementServices>();
|
||||
|
||||
// ※※★※※ 如果你是第一次下载项目,请先F6编译,然后再F5执行,※※★※※
|
||||
|
||||
#region 带有接口层的服务注入
|
||||
|
||||
#region Service.dll 注入,有对应接口
|
||||
|
||||
//获取项目绝对路径,请注意,这个是实现类的dll文件,不是接口 IService.dll ,注入容器当然是Activatore
|
||||
try
|
||||
var servicesDllFile = Path.Combine(basePath, "Blog.Core.Services.dll");
|
||||
var repositoryDllFile = Path.Combine(basePath, "Blog.Core.Repository.dll");
|
||||
|
||||
if (!(File.Exists(servicesDllFile) && File.Exists(repositoryDllFile)))
|
||||
{
|
||||
//直接采用加载文件的方法 ※※★※※ 如果你是第一次下载项目,请先F6编译,然后再F5执行,※※★※※
|
||||
var servicesDllFile = Path.Combine(basePath, "Blog.Core.Services.dll");
|
||||
var assemblysServices = Assembly.LoadFrom(servicesDllFile);
|
||||
throw new Exception("Repository.dll和service.dll 丢失,因为项目解耦了,所以需要先F6编译,再F5运行,请检查 bin 文件夹,并拷贝。");
|
||||
}
|
||||
|
||||
// AOP 开关,如果想要打开指定的功能,只需要在 appsettigns.json 对应对应 true 就行。
|
||||
var cacheType = new List<Type>();
|
||||
|
||||
|
||||
// AOP 开关,如果想要打开指定的功能,只需要在 appsettigns.json 对应对应 true 就行。
|
||||
var cacheType = new List<Type>();
|
||||
if (Appsettings.app(new string[] { "AppSettings", "RedisCachingAOP", "Enabled" }).ObjToBool())
|
||||
{
|
||||
builder.RegisterType<BlogRedisCacheAOP>();
|
||||
builder.RegisterType<BlogCacheAOP>();
|
||||
builder.RegisterType<BlogTranAOP>();
|
||||
builder.RegisterType<BlogLogAOP>();
|
||||
|
||||
if (Appsettings.app(new string[] { "AppSettings", "RedisCachingAOP", "Enabled" }).ObjToBool())
|
||||
{
|
||||
cacheType.Add(typeof(BlogRedisCacheAOP));
|
||||
}
|
||||
if (Appsettings.app(new string[] { "AppSettings", "MemoryCachingAOP", "Enabled" }).ObjToBool())
|
||||
{
|
||||
cacheType.Add(typeof(BlogCacheAOP));
|
||||
}
|
||||
if (Appsettings.app(new string[] { "AppSettings", "TranAOP", "Enabled" }).ObjToBool())
|
||||
{
|
||||
cacheType.Add(typeof(BlogTranAOP));
|
||||
}
|
||||
if (Appsettings.app(new string[] { "AppSettings", "LogAOP", "Enabled" }).ObjToBool())
|
||||
{
|
||||
cacheType.Add(typeof(BlogLogAOP));
|
||||
}
|
||||
|
||||
builder.RegisterAssemblyTypes(assemblysServices)
|
||||
.AsImplementedInterfaces()
|
||||
.InstancePerDependency()
|
||||
.EnableInterfaceInterceptors()//引用Autofac.Extras.DynamicProxy;
|
||||
.InterceptedBy(cacheType.ToArray());//允许将拦截器服务的列表分配给注册。
|
||||
|
||||
#endregion
|
||||
|
||||
#region Repository.dll 注入,有对应接口
|
||||
var repositoryDllFile = Path.Combine(basePath, "Blog.Core.Repository.dll");
|
||||
var assemblysRepository = Assembly.LoadFrom(repositoryDllFile);
|
||||
builder.RegisterAssemblyTypes(assemblysRepository)
|
||||
.AsImplementedInterfaces()
|
||||
.InstancePerDependency()
|
||||
;
|
||||
cacheType.Add(typeof(BlogRedisCacheAOP));
|
||||
}
|
||||
catch (Exception ex)
|
||||
if (Appsettings.app(new string[] { "AppSettings", "MemoryCachingAOP", "Enabled" }).ObjToBool())
|
||||
{
|
||||
log.Error("Repository.dll和service.dll 丢失,因为项目解耦了,所以需要先F6编译,再F5运行,请检查并拷贝。\n" + ex.Message);
|
||||
|
||||
//throw new Exception("※※★※※ 如果你是第一次下载项目,请先对整个解决方案dotnet build(F6编译),然后再对api层 dotnet run(F5执行),\n因为解耦了,如果你是发布的模式,请检查bin文件夹是否存在Repository.dll和service.dll ※※★※※" + ex.Message + "\n" + ex.InnerException);
|
||||
builder.RegisterType<BlogCacheAOP>();
|
||||
cacheType.Add(typeof(BlogCacheAOP));
|
||||
}
|
||||
#endregion
|
||||
if (Appsettings.app(new string[] { "AppSettings", "TranAOP", "Enabled" }).ObjToBool())
|
||||
{
|
||||
builder.RegisterType<BlogTranAOP>();
|
||||
cacheType.Add(typeof(BlogTranAOP));
|
||||
}
|
||||
if (Appsettings.app(new string[] { "AppSettings", "LogAOP", "Enabled" }).ObjToBool())
|
||||
{
|
||||
builder.RegisterType<BlogLogAOP>();
|
||||
cacheType.Add(typeof(BlogLogAOP));
|
||||
}
|
||||
|
||||
// 获取 Service.dll 程序集服务,并注册
|
||||
var assemblysServices = Assembly.LoadFrom(servicesDllFile);
|
||||
builder.RegisterAssemblyTypes(assemblysServices)
|
||||
.AsImplementedInterfaces()
|
||||
.InstancePerDependency()
|
||||
.EnableInterfaceInterceptors()//引用Autofac.Extras.DynamicProxy;
|
||||
.InterceptedBy(cacheType.ToArray());//允许将拦截器服务的列表分配给注册。
|
||||
|
||||
// 获取 Repository.dll 程序集服务,并注册
|
||||
var assemblysRepository = Assembly.LoadFrom(repositoryDllFile);
|
||||
builder.RegisterAssemblyTypes(assemblysRepository)
|
||||
.AsImplementedInterfaces()
|
||||
.InstancePerDependency();
|
||||
|
||||
#endregion
|
||||
|
||||
#region 没有接口层的服务层注入
|
||||
|
||||
////因为没有接口层,所以不能实现解耦,只能用 Load 方法。
|
||||
////注意如果使用没有接口的服务,并想对其使用 AOP 拦截,就必须设置为虚方法
|
||||
////var assemblysServicesNoInterfaces = Assembly.Load("Blog.Core.Services");
|
||||
////builder.RegisterAssemblyTypes(assemblysServicesNoInterfaces);
|
||||
//因为没有接口层,所以不能实现解耦,只能用 Load 方法。
|
||||
//注意如果使用没有接口的服务,并想对其使用 AOP 拦截,就必须设置为虚方法
|
||||
//var assemblysServicesNoInterfaces = Assembly.Load("Blog.Core.Services");
|
||||
//builder.RegisterAssemblyTypes(assemblysServicesNoInterfaces);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 没有接口的单独类 class 注入
|
||||
////只能注入该类中的虚方法
|
||||
|
||||
//只能注入该类中的虚方法
|
||||
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(Love)))
|
||||
.EnableClassInterceptors()
|
||||
.InterceptedBy(typeof(BlogLogAOP));
|
||||
.InterceptedBy(cacheType.ToArray());
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user