我試圖使用Unity通過屬性對我的ApiController方法應用調用處理程序,但從不調用調用處理函數。使用ASP.NET Web Api統一屬性攔截
屬性:
public class LogAttribute : HandlerAttribute
{
private readonly int _order;
public LogAttribute(int order)
{
_order = order;
}
public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
{
return new LoggingCallHandler
{
Order = _order
};
}
}
呼叫處理程序:
public class LoggingCallHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
//.....stuff
}
}
註冊:
container.AddNewExtension<Interception>();
container.RegisterType<IMyApiController>();
container.Configure<Interception>()
.SetInterceptorFor<IMyApiController>(new InterfaceInterceptor());
依賴解析器(使用WebActiviatorEx):
public static class UnityWebApiActivator
{
public static void Start()
{
var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
}
你在哪裏申請'LogAttribute'? –
就在一個ApiController方法... –