Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs

91 lines
3.0 KiB
C#
Raw Normal View History

2021-10-12 16:52:28 +08:00
using Autofac;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;
2021-11-06 17:53:54 +08:00
using Microsoft.AspNetCore.Http;
2021-10-12 16:52:28 +08:00
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
2021-10-12 16:52:28 +08:00
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
2022-04-08 23:44:25 +08:00
using Yi.Framework.Interface;
2021-10-30 18:48:58 +08:00
using Yi.Framework.Job;
2022-04-08 23:44:25 +08:00
using Yi.Framework.Repository;
using Yi.Framework.Service;
2021-10-17 18:37:07 +08:00
using Yi.Framework.WebCore.Utility;
2021-10-12 16:52:28 +08:00
using Module = Autofac.Module;
2021-10-17 18:37:07 +08:00
namespace Yi.Framework.WebCore.Utility
2021-10-12 16:52:28 +08:00
{
public class CustomAutofacModule : Module
{
2021-10-30 18:48:58 +08:00
private Assembly GetDll(string ass)
2021-10-12 16:52:28 +08:00
{
var basePath = AppContext.BaseDirectory;
2021-10-30 18:48:58 +08:00
var servicesDllFile = Path.Combine(basePath, ass);
if (!(File.Exists(servicesDllFile)))
{
var msg = "service.dll 丢失,请编译后重新生成。";
throw new Exception(msg);
}
2021-10-30 18:48:58 +08:00
return Assembly.LoadFrom(servicesDllFile); ;
}
protected override void Load(ContainerBuilder containerBuilder)
{
2022-04-02 17:44:50 +08:00
//containerBuilder.RegisterType<DbContextFactory>().As<IDbContextFactory>().InstancePerDependency().EnableInterfaceInterceptors();
2021-10-30 18:48:58 +08:00
2021-11-06 17:53:54 +08:00
containerBuilder.RegisterType< HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
2021-10-30 18:48:58 +08:00
2022-04-08 23:44:25 +08:00
containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerLifetimeScope();
2021-11-06 17:53:54 +08:00
///反射注入服务层及接口层
2021-10-30 18:48:58 +08:00
var assemblysServices = GetDll( "Yi.Framework.Service.dll");
containerBuilder.RegisterAssemblyTypes(assemblysServices)
.AsImplementedInterfaces()
2022-04-08 23:44:25 +08:00
.InstancePerLifetimeScope()
.EnableInterfaceInterceptors();
2021-10-30 18:48:58 +08:00
2022-04-08 23:44:25 +08:00
///反射注册任务调度层
2021-10-30 18:48:58 +08:00
var assemblysJob = GetDll("Yi.Framework.Job.dll");
containerBuilder.RegisterAssemblyTypes(assemblysJob)
.InstancePerDependency();
2021-10-12 16:52:28 +08:00
containerBuilder.Register(c => new CustomAutofacAop());//AOP注册
2021-10-30 18:48:58 +08:00
2021-10-12 16:52:28 +08:00
//containerBuilder.RegisterType<A>().As<IA>().EnableInterfaceInterceptors();开启Aop
//将数据库对象注入
2021-10-13 15:40:56 +08:00
//containerBuilder.RegisterType<DataContext>().As<DbContext>().InstancePerLifetimeScope().EnableInterfaceInterceptors();
2021-10-12 16:52:28 +08:00
//containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerDependency().EnableInterfaceInterceptors();
2021-10-13 15:40:56 +08:00
2021-10-12 16:52:28 +08:00
}
}
}
public interface IAutofacTest
{
void Show(int id, string name);
}
[Intercept(typeof(CustomAutofacAop))]
public class AutofacTest : IAutofacTest
{
public void Show(int id, string name)
{
Console.WriteLine($"This is {id} _ {name}");
}
}