我开始使用 Ninject 拦截扩展,但无法让它在我的 WCF 服务中工作。使用 WCF 扩展,ninject 可以正常工作,正是拦截给我带来了麻烦。也许我做错了?当我尝试在内核构造函数中添加 LinFuModel 时,它告诉我它已经加载,所以我想这很好。
基本上所有对绑定的拦截都会破坏我的 wcf 服务,但我的方法拦截仅适用于服务(getData() 在服务合同中)。
编辑:以下也不起作用:
Kernel.Intercept((request) => request.Method.Name.StartsWith("Get"))
.With<TimingInterceptor>();
结束编辑
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel(new ServiceModule());
//var binding = kernel.Bind<MockBroker>().ToSelf();
//binding.Intercept().With<TimingInterceptor>(); // THIS BREAKS
kernel.InterceptAfter<Watch>(m => m.GetData(0), i => { i.ReturnValue = "BLABLABLA"; log.Info("INTERCEPTED!"); }); //WORKS
//kernel.Bind<Watch>().ToSelf().Intercept().With(new TimingInterceptor()); //BREAKS
//kernel.Bind<FileSystemBroker>().ToSelf().Intercept().With<TimingInterceptor>(); //BREAKS
return kernel;
}
public class TimingInterceptor : SimpleInterceptor
{
readonly Stopwatch _stopwatch = new Stopwatch();
//private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected override void BeforeInvoke(IInvocation invocation)
{
_stopwatch.Start();
}
protected override void AfterInvoke(IInvocation invocation)
{
_stopwatch.Stop();
string message = string.Format("[Execution of {0} took {1}.]",
invocation.Request.Method,
_stopwatch.Elapsed);
//log.Info(message);
_stopwatch.Reset();
}
}
提前感谢,Rinze