无论如何,有没有为集线器功能定义和添加方法过滤器(例如mvc中的ActionFilters),我的意思是这样的:
public class MyHub : Hub
{
[Log]
public string RegisterUser(UserModel model){
...
}
}
在这里我可以在LogAttribute实现内部进行一些控制。
通过使用SignalR的Hub管道,您应该能够实现与ASP.NET MVC中的动作过滤器类似的功能:
public class LoggingPipelineModule : HubPipelineModule
{
protected override bool OnBeforeIncoming(IHubIncomingInvokerContext context)
{
Debug.WriteLine("Invoking '{0}.{1}({2})'.",
context.MethodDescriptor.Hub.Name,
context.MethodDescriptor.Name,
string.Join(", ", context.Args));
return base.OnBeforeIncoming(context);
}
protected override object OnAfterIncoming(object result, IHubIncomingInvokerContext context)
{
Debug.WriteLine("Finished Invoking '{0}.{1}'. Returned '{2}'.",
context.MethodDescriptor.Hub.Name,
context.MethodDescriptor.Name,
result);
return base.OnAfterIncoming(result, context);
}
}
如果只想记录附加了定制属性的方法,则可以在记录之前检查定制属性:
protected override bool OnBeforeIncoming(IHubIncomingInvokerContext context)
{
if (context.MethodDescriptor.Attributes.OfType<MyAttribute>().Any())
{
// Log here.
}
return base.OnBeforeIncoming(context);
}
您可以在致电之前注册模块MapSignalR
:
public void Configuration(IAppBuilder app)
{
GlobalHost.HubPipeline.AddModule(new LoggingPipelineModule());
app.MapSignalR();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句