Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null reference exception in RequestHandlerWrapperImpl #438

Closed
CDargis opened this issue Sep 10, 2019 · 11 comments
Closed

Null reference exception in RequestHandlerWrapperImpl #438

CDargis opened this issue Sep 10, 2019 · 11 comments

Comments

@CDargis
Copy link

CDargis commented Sep 10, 2019

Hi there. We are currently experiencing intermittent object reference exceptions. Here are a few:

Message : Object reference not set to an instance of an object.
StackTrace :
   at MediatR.Internal.RequestHandlerWrapperImpl`2.<>c__DisplayClass0_1.<Handle>b__2()
   at WellRight.Platform.Microservices.MediatR.Pipeline.IdentityPipelineBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at WellRight.Platform.Microservices.MediatR.Pipeline.ValidationPipelineBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at WellRight.Platform.Microservices.MediatR.Pipeline.CatchAllPipelineBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
Type : System.NullReferenceException
Message : Object reference not set to an instance of an object.
StackTrace :
   at MediatR.Internal.RequestHandlerWrapperImpl`2.<>c__DisplayClass0_1.<Handle>b__2()
   at WellRight.Platform.Microservices.MediatR.Pipeline.ConcurrentRequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at WellRight.Platform.Microservices.MediatR.Pipeline.SequentialRequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at WellRight.Platform.Microservices.MediatR.Pipeline.IdentityPipelineBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at WellRight.Platform.Microservices.MediatR.Pipeline.ValidationPipelineBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at WellRight.Platform.Microservices.MediatR.Pipeline.CatchAllPipelineBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
Type : System.NullReferenceException

MediatR version 7.0.0
Lamar.Microsoft.DependencyInjection version 3.1.0
We are using generic constraints. All of the above behaviors implement IPipelineBehavior<,>. One thing to note here is that every request should be passing through each behavior. It is odd that sometimes the request makes it through halfway through the pipeline behaviors and sometimes maybe 1/4 of the way through.

Any thoughts or insight as to what is going on here? After taking a look at the RequestHandlerWrapperImpl code I think that maybe our container is returning something null? Thanks in advance for any help.

@CDargis
Copy link
Author

CDargis commented Sep 10, 2019

Ah one more thing. I mentioned this issue is intermittent. It seems to come on with greater load.

@CDargis
Copy link
Author

CDargis commented Sep 10, 2019

So I pulled down the mediatr code and rewrote a bit for readability to debug locally. Definitely a null object coming back in the list of pipeline behaviors. Also really strange there are some duplicates.

image

@CDargis
Copy link
Author

CDargis commented Sep 10, 2019

Our registration code:

public MediatRRegistry(string assemblyPrefix)
{
    Scan(_ =>
    {
        _.AssembliesFromApplicationBaseDirectory(x => 
            string.IsNullOrEmpty(assemblyPrefix) || x.FullName.StartsWith(assemblyPrefix));
        _.ConnectImplementationsToTypesClosing(typeof(IValidator<>));
        _.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<>));
        _.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
        _.ConnectImplementationsToTypesClosing(typeof(ISequentialRequestPreProcessor<>));
        _.ConnectImplementationsToTypesClosing(typeof(ISequentialRequestPostProcessor<,>));
        _.ConnectImplementationsToTypesClosing(typeof(IConcurrentRequestPreProcessor<>));
        _.ConnectImplementationsToTypesClosing(typeof(IConcurrentRequestPostProcessor<,>));
    });

    // Pipeline order of operation
    For(typeof(IPipelineBehavior<,>)).Add(typeof(CatchAllPipelineBehavior<,>));
    For(typeof(IPipelineBehavior<,>)).Add(typeof(ValidationPipelineBehavior<,>));
    For(typeof(IPipelineBehavior<,>)).Add(typeof(IdentityPipelineBehavior<,>));
    For(typeof(IPipelineBehavior<,>)).Add(typeof(ServiceIdentityPipelineBehavior<,>));
    For(typeof(IPipelineBehavior<,>)).Add(typeof(RequestLockPipelineBehavior<,>));
    For(typeof(IPipelineBehavior<,>)).Add(typeof(SequentialRequestPreProcessorBehavior<,>));
    For(typeof(IPipelineBehavior<,>)).Add(typeof(ConcurrentRequestPreProcessorBehavior<,>));
    For(typeof(IPipelineBehavior<,>)).Add(typeof(SequentialRequestPostProcessorBehavior<,>));
    For(typeof(IPipelineBehavior<,>)).Add(typeof(ConcurrentRequestPostProcessorBehavior<,>));

    For<IMediator>().Use<Mediator>().Transient();
    For<ServiceFactory>().Use(ctx => ctx.GetInstance);
}

Help us Obi-Wan.

@CDargis
Copy link
Author

CDargis commented Sep 10, 2019

Probably related: JasperFx/lamar#157

@jbogard
Copy link
Owner

jbogard commented Sep 10, 2019

Uhhhhhhh

No idea. You aren't using services.AddMediatR, right?

@CDargis
Copy link
Author

CDargis commented Sep 10, 2019

No. Was not aware that existed.

@jbogard
Copy link
Owner

jbogard commented Sep 10, 2019

I have no idea about duplicates. You can check for sure by resolving the same pipeline directly from Lamar yourself:

https://github.com/jbogard/MediatR/blob/master/src/MediatR/Internal/RequestHandlerWrapper.cs#L57

What does this return for you?

@CDargis
Copy link
Author

CDargis commented Sep 10, 2019

That is exactly the screenshot I posted. The code looks slightly different because I had to rewrite a bit to be able to debug at runtime.

It returns a list of 11 objects - one of which is null and there are several duplicates.

@CDargis CDargis closed this as completed Sep 10, 2019
@CDargis CDargis reopened this Sep 10, 2019
@CDargis
Copy link
Author

CDargis commented Sep 11, 2019

Definitely a Lamar issue. Doesn't look like the issue is going to get much movement there. I can't think of any solutions on your end that doesn't involve providing a way for consumers to serve up the IEnumerable<>

@CDargis
Copy link
Author

CDargis commented Sep 11, 2019

We could probably get something working by implementing a more complicated ServiceFactory but I think we are just going to use a different DI container.

@jorhar
Copy link

jorhar commented Sep 13, 2019

Moved back to StructureMap to resolve this issue.

@CDargis CDargis closed this as completed Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants