-
Notifications
You must be signed in to change notification settings - Fork 9
拦截器
Kakous edited this page Mar 21, 2019
·
6 revisions
Uragano里可以自定义拦截器,并且拦截器分服务器端拦截器和客户端拦截器
拦截器还分全局拦截器和局部拦截器
public class ClientGlobalInterceptor : InterceptorAbstract
{
private ILogger Logger { get; }
public ClientGlobalInterceptor(ILogger<ClientGlobalInterceptor> logger)
{
Logger = logger;
}
public override async Task<IServiceResult> Intercept(IInterceptorContext context)
{
Logger.LogTrace("\n---------------->Client global interceptor\n");
return await context.Next();
}
}
还可通过自定义属性来注入拦截器
public class ClientInterceptorAttribute : InterceptorAttributeAbstract
{
private ILogger Logger { get; }
public ClientInterceptorAttribute(ILogger<ClientInterceptorAttribute> logger)
{
Logger = logger;
}
public override async Task<IServiceResult> Intercept(IInterceptorContext context)
{
Logger.LogTrace("\n------------------>Server Interceptor attribute\n");
var r = await context.Next();
return r;
}
}
全局拦截器可通过AddClientGlobalInterceptor,AddServerGlobalInterceptor方法注入
services.AddUragano(Configuration, builder =>
{
builder.AddConsul();
builder.AddClient();
builder.AddServer();
builder.AddClientGlobalInterceptor<ClientGlobalInterceptor>();
builder.AddServerGlobalInterceptor<ServerGlobalInterceptor>();
});
局部拦截器是通过自定义属性来注入,那自定义属性如何区分客户端和服务端呢?自定义属性加在服务接口上就是客户端拦截器,加在服务具体的实现类上就是服务器端拦截器。
[ServiceDiscoveryName("RPC")]
[ServiceRoute("hello")]
[ClientInterceptor]
public interface IHelloService : IService
{
[ServiceRoute("say")]
Task<ResultModel> SayHello(string name);
}
属性 | 描述 |
---|---|
ServiceRoute | 服务路由 |
Meta | 元数据 |
Args | 被拦截的服务参数 |
Next | 执行下一个拦截器 |