提交.Net6版本
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using Castle.DynamicProxy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.WebCore.Utility
|
||||
{
|
||||
public class CustomAutofacAop : IInterceptor
|
||||
{
|
||||
public void Intercept(IInvocation invocation)
|
||||
{
|
||||
Console.WriteLine($"invocation.Methond={invocation.Method}");
|
||||
Console.WriteLine($"invocation.Arguments={string.Join(",", invocation.Arguments)}");
|
||||
|
||||
invocation.Proceed(); //继续执行
|
||||
|
||||
Console.WriteLine($"方法{invocation.Method}执行完成了");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using Autofac;
|
||||
using Autofac.Extras.DynamicProxy;
|
||||
using Castle.DynamicProxy;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Job;
|
||||
using Yi.Framework.Model.ModelFactory;
|
||||
using Yi.Framework.WebCore.Utility;
|
||||
using Module = Autofac.Module;
|
||||
|
||||
namespace Yi.Framework.WebCore.Utility
|
||||
{
|
||||
public class CustomAutofacModule : Module
|
||||
{
|
||||
private Assembly GetDll(string ass)
|
||||
{
|
||||
var basePath = AppContext.BaseDirectory;
|
||||
var servicesDllFile = Path.Combine(basePath, ass);
|
||||
if (!(File.Exists(servicesDllFile)))
|
||||
{
|
||||
var msg = "service.dll 丢失,请编译后重新生成。";
|
||||
throw new Exception(msg);
|
||||
}
|
||||
return Assembly.LoadFrom(servicesDllFile); ;
|
||||
}
|
||||
|
||||
protected override void Load(ContainerBuilder containerBuilder)
|
||||
{
|
||||
|
||||
containerBuilder.RegisterType<DbContextFactory>().As<IDbContextFactory>().InstancePerDependency().EnableInterfaceInterceptors();
|
||||
|
||||
containerBuilder.RegisterType< HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
|
||||
|
||||
|
||||
|
||||
///反射注入服务层及接口层
|
||||
var assemblysServices = GetDll( "Yi.Framework.Service.dll");
|
||||
containerBuilder.RegisterAssemblyTypes(assemblysServices)
|
||||
.AsImplementedInterfaces()
|
||||
.InstancePerDependency()
|
||||
.EnableInterfaceInterceptors();
|
||||
|
||||
///反射注册任务调度层
|
||||
var assemblysJob = GetDll("Yi.Framework.Job.dll");
|
||||
containerBuilder.RegisterAssemblyTypes(assemblysJob)
|
||||
.InstancePerDependency();
|
||||
|
||||
|
||||
|
||||
containerBuilder.Register(c => new CustomAutofacAop());//AOP注册
|
||||
|
||||
|
||||
//containerBuilder.RegisterType<A>().As<IA>().EnableInterfaceInterceptors();开启Aop
|
||||
|
||||
//将数据库对象注入
|
||||
//containerBuilder.RegisterType<DataContext>().As<DbContext>().InstancePerLifetimeScope().EnableInterfaceInterceptors();
|
||||
|
||||
//containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerDependency().EnableInterfaceInterceptors();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[assembly: HostingStartup(typeof(Yi.Framework.WebCore.Utility.CustomHostingStartup))]
|
||||
namespace Yi.Framework.WebCore.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 必须实现IHostingStartup接口
|
||||
/// 必须标记HostingStartup特性
|
||||
///
|
||||
/// 就像木马一样
|
||||
/// </summary>
|
||||
public class CustomHostingStartup : IHostingStartup
|
||||
{
|
||||
public void Configure(IWebHostBuilder builder)
|
||||
{
|
||||
Console.WriteLine("This is CustomHostingStartup Invoke");
|
||||
|
||||
//有IWebHostBuilder,一切都可以做。。
|
||||
#region MyRegion
|
||||
//builder.ConfigureAppConfiguration(configurationBuilder =>
|
||||
//{
|
||||
// configurationBuilder.AddXmlFile("appsettings1.xml", optional: false, reloadOnChange: true);
|
||||
//});//添加配置
|
||||
|
||||
//builder.ConfigureServices(services =>
|
||||
//{
|
||||
// services.AddTransient<ITestServiceA, TestServiceA>();
|
||||
//});//IOC注册
|
||||
|
||||
//builder.Configure(app =>
|
||||
//{
|
||||
// app.Use(next =>
|
||||
// {
|
||||
// Console.WriteLine("This is CustomHostingStartup-Middleware Init");
|
||||
// return new RequestDelegate(
|
||||
// async context =>
|
||||
// {
|
||||
// Console.WriteLine("This is CustomHostingStartup-Middleware start");
|
||||
// await next.Invoke(context);
|
||||
// Console.WriteLine("This is CustomHostingStartup-Middleware end");
|
||||
// });
|
||||
// });
|
||||
//});//甚至来个中间件
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user