2014-09-04 130 views
0

我对这个问题的新手,所以我会尽力使这个明确的,因为我可以...Ninject拦截WCF服务

我创建了一个WcfModule,在那里我加载以下包:

Bind<IDistributorService>().To<DistributorService>().InRequestScope().Intercept().With<ExceptionInterceptor>(); 

起初,我没有收到任何错误,但我把一个InterceptAttribute我的功能:

[AttributeUsage(AttributeTargets.Method)] 
public sealed class HandleExceptionsAttribute : InterceptAttribute 
{ 
    public override IInterceptor CreateInterceptor(IProxyRequest request) 
    { 
     return request.Kernel.Get<ExceptionInterceptor>(); 
    } 
} 

[HandleExceptions] 
public virtual Result<List<DistributorDataContract>> GetDistributor(string id) 
{ 
    //...code... 

我得到了这个功能的错误:(在法第一行)

private ServiceHost CreateNewServiceHost(Type serviceType, Uri[] baseAddresses, WebHttpBehavior webBehavior, WebHttpBinding webHttpBinding) 
{ 
    var host = base.CreateServiceHost(serviceType, baseAddresses); 
    //... 
} 

与错误:

InvalidProxyConstructorArgumentsException was unhandled by user code Can not instantiate proxy of class: My.Namespace.DistributorService. Could not find a parameterless constructor.

任何人谁知道这个问题可能是什么?谢谢!

回答

0

当指示创建一个没有无参数构造函数并且没有构造函数参数传递给城堡的类代理时,城堡核心动态代理引发此异常(请参见source)。

我最好的猜测是,当你使用ninject拦截属性时,ninject会指示城堡核心创建一个类代理,不管你的绑定是Bind<IFoo>().To<Foo>()还是Bind<Foo>().ToSelf()

然而,这似乎有点奇怪,ninject不解决并传递所有必需的构造函数参数。

什么是DistributorService的实现和什么是包含CreateNewServiceHost的类的基类的实现?

解决方法: 当然,切换到Intercept().With<TInterceptor>()语法可能还允许您使用拦截(见http://codepyre.com/2010/03/using-ninject-extensions-interception-part-2-working-with-interceptors/