IIS托管的WCF应用程序中的Ninject拦截

时间:2018-02-07 22:15:32

标签: wcf iis ninject interception

我有一个在IIS(.svc)中托管的工作WCF应用程序,使用Ninject(Ninject.Web.Common)进行依赖注入。一切都很好,很好。但是我试图在服务端点上实施拦截并获得ServiceActiviationException。我已经为wcf,拦截和interception.dynamic代理以及castle.core包含了ninject扩展,所以我不确定我在这里缺少什么。有什么想法吗?

更新:我能够通过服务验证异常但我现在收到以下错误:

Can not instantiate proxy of class: MyNameSpace.CustomerService.
Could not find a parameterless constructor.

My NinjectWebCommon内容:

public class NinjectWebCommon
{
    private static readonly Bootstrapper Bootstrapper = new Bootstrapper();

    public static void Start()
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        Bootstrapper.Initialize(CreateKernel);
    }

    public static void PostStart()
    {
        // Inject the WITS Application Security service into the ApplicationSecurityRoleProvider
        //_kernel.Inject(Roles.Provider);
    }

    public static void Stop()
    {
        Bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel(
            new AutoMapperModule(), 
            new CustomerServiceModule(), 
            new AccountServiceModule());
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            RegisterServices(kernel);
            return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
    }

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<LoggingInterceptor>().ToSelf().InRequestScope();
        kernel.Bind<CustomerService>().ToSelf().Intercept().With<LoggingInterceptor>();

    }

一个简单的拦截器(我试过这个也只是实现IInterceptor无效):

public class LoggingInterceptor : SimpleInterceptor
{
    /// <summary>
    /// Intercepts the specified invocation.
    /// </summary>
    /// <param name="invocation">The invocation to intercept.</param>
    protected override void BeforeInvoke(IInvocation invocation)
    {
        Debug.WriteLine("Running " + invocation.ReturnValue);
        base.BeforeInvoke(invocation);
    }

    public new void Intercept(IInvocation invocation)
    {
        try
        {
            base.Intercept(invocation);
        }
        catch (Exception e)
        {
            Debug.WriteLine("Exception: " + e.Message);
        }
    }

    protected override void AfterInvoke(IInvocation invocation)
    {
        Debug.WriteLine("After Method");
        base.AfterInvoke(invocation);
    }

}

0 个答案:

没有答案