0

I upgraded Ninject from 2.2 to 3.2. Before, the interception worked fine, but now if I set a breakpoint I can see that the parameterless constructor is getting called after the constructor that has the parameters that Ninject is injecting. It wasn't getting hit before. The result of this is that my private variable is now null.

Here's what my code looks like:

public class ContentHandler : IContentHandler
{
    IClientGateway _clientGateway;

    protected ContentHandler()
    {
        // This is hit after the other constructer when using Ninject 3.2 but not 2.2.
        // I think this is what is causing _clientGateway to be null.
    }

    public ContentHandler(IClientGateway clientGateway)
    {
        // This gets hit first regardless of Ninject version.
        this._clientGateway = clientGateway;
    }

    public IClientContent GetClientContent(int clientID)
    {
        // _clientGateway is null and throws an exception here :(
        IClient client = this._clientGateway.GetClientbyClientID(clientID);
    }

What am I doing wrong? In my global.asax, I have this:

kernel.Intercept(ctx => ctx.Request.Service == typeof(IClientGateway))
      .With(InterceptorFactory.GetInterceptor());

The InterceptorFactory looks like this:

public static class InterceptorFactory
{
    private static IKernel _kernel = new StandardKernel(new LoggingInterceptionModule());

    public static ExceptionInterceptor GetInterceptor()
    {
        return GetExceptionInterceptor();
    }

    private static ExceptionInterceptor GetExceptionInterceptor()
    {
        return _kernel.Get<ExceptionInterceptor>();
    }
}

The ExceptionInterceptor looks like this:

public class ExceptionInterceptor : IInterceptor
{
    private LoggingConnector _loggingConnector;
    private const string _logFormat = "Exception occured during invoke of {0} in {1} with arguments {2} EXCEPTION: {3}";

    public ExceptionInterceptor(LoggingConnector lc)
    {
        _loggingConnector = lc;
        _loggingConnector.SetLevel(LogLevel.Error);
    }

    public void Intercept(IInvocation invocation)
    {
        try
        {
            invocation.Proceed();
        }
        catch (Exception e)
        {
            _loggingConnector.Write(String.Format(_logFormat, invocation.Request.Method, invocation.Request.Target, invocation.Request.Arguments, e.StackTrace));

            throw e;
        }
    }
}

Thanks for your help.

4

0 回答 0