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

65 lines
2.0 KiB
C#
Raw Normal View History

2021-10-12 16:52:28 +08:00
using Autofac;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;
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;
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
{
protected override void Load(ContainerBuilder containerBuilder)
{
var basePath = AppContext.BaseDirectory;
var servicesDllFile = Path.Combine(basePath, "Yi.Framework.Service.dll");
if (!(File.Exists(servicesDllFile)))
{
var msg = "service.dll 丢失,请编译后重新生成。";
throw new Exception(msg);
}
var assemblysServices = Assembly.LoadFrom(servicesDllFile);
containerBuilder.RegisterAssemblyTypes(assemblysServices)
.AsImplementedInterfaces()
.InstancePerDependency()
.EnableInterfaceInterceptors();
2021-10-12 16:52:28 +08:00
containerBuilder.Register(c => new CustomAutofacAop());//AOP注册
//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}");
}
}