public class CustomInterceptorAttribute : InterceptorAttribute
{
public async override Task Invoke(IAspectContext context, AspectDelegate next)
{
try
{
Console.WriteLine("Before service call");
await next(context);
}
catch (Exception)
{
Console.WriteLine("Service threw an exception!");
throw;
}
finally
{
Console.WriteLine("After service call");
}
}
}
public interface ICustomService
{
[CustomInterceptor]
void Call();
}
public class CustomService : ICustomService
{
public void Call()
{
Console.WriteLine("service calling...");
}
}
public class HomeController : Controller
{
private readonly ICustomService _service;
public HomeController(ICustomService service)
{
_service = service;
}
public IActionResult Index()
{
_service.Call();
return View();
}
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddTransient<ICustomService, CustomService>();
services.AddMvc();
services.AddAspectCore();
return services.BuildAspectCoreServiceProvider();
}
PM> Install-Package AspectCore.Extensions.Configuration
services.AddAspectCore(config =>
{
config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>();
});
public class CustomInterceptorAttribute : InterceptorAttribute
{
private readonly string _name;
public CustomInterceptorAttribute(string name)
{
_name = name;
}
public async override Task Invoke(AspectContext context, AspectDelegate next)
{
try
{
Console.WriteLine("Before service call");
await next(context);
}
catch (Exception)
{
Console.WriteLine("Service threw an exception!");
throw;
}
finally
{
Console.WriteLine("After service call");
}
}
}
services.AddAspectCore(config =>
{
config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(args: new object[] { "custom" });
});
services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute("service"));
services.AddAspectCore(config =>
{
config.InterceptorFactories.AddServiced<CustomInterceptorAttribute>();
});
services.AddAspectCore(config =>
{
config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(method => method.DeclaringType.Name.EndsWith("Service"));
});
services.AddAspectCore(config =>
{
config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(PredicateFactory.ForService("*Service"));
});
[NonAspect]
public interface ICustomService
{
void Call();
}
services.AddAspectCore(config =>
{
//App1命名空间下的Service不会被代理
config.NonAspectOptions.AddNamespace("App1");
//最后一级为App1的命名空间下的Service不会被代理
config.NonAspectOptions.AddNamespace("*.App1");
//ICustomService接口不会被代理
config.NonAspectOptions.AddService("ICustomService");
//后缀为Service的接口和类不会被代理
config.NonAspectOptions.AddService("*Service");
//命名为Query的方法不会被代理
config.NonAspectOptions.AddMethod("Query");
//后缀为Query的方法不会被代理
config.NonAspectOptions.AddMethod("*Query");
});
public class CustomInterceptorAttribute : InterceptorAttribute
{
[AspectCore.Abstractions.FromServices]
public ILogger<CustomInterceptorAttribute> Logger { get; set; }
public override Task Invoke(AspectContext context, AspectDelegate next)
{
Logger.LogInformation("call interceptor");
return next(context);
}
}
public interface ICustomService
{
[ServiceInterceptor(typeof(CustomInterceptorAttribute))]
void Call();
}
public class CustomInterceptorAttribute : InterceptorAttribute
{
public override Task Invoke(AspectContext context, AspectDelegate next)
{
var logger = context.ServiceProvider.GetService<ILogger<CustomInterceptorAttribute>>();
logger.LogInformation("call interceptor");
return next(context);
}
}
PM> Install-Package Autofac.Extensions.DependencyInjection PM> Install-Package AspectCore.Extensions.Autofac
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var container = new ContainerBuilder();
container.RegisterAspectCore();
container.Populate(services);
container.RegisterType<CustomService>().As<ICustomService>().InstancePerDependency().AsInterfacesProxy();
return new AutofacServiceProvider(container.Build());
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有