Mediatr: No registration for type IEnumerable<IPipelineBehavior<CreateOrganization, Identity>> could be found.

Created on 25 Mar 2017  路  8Comments  路  Source: jbogard/MediatR

Did container configuration change with the new version? I'm receiving the following error from SimpleInjector.

No registration for type IEnumerable<IPipelineBehavior<CreateOrganization, Identity>> could be found.

with stack trace:

   at SimpleInjector.Container.ThrowMissingInstanceProducerException(Type serviceType)
   at SimpleInjector.Container.GetInstanceForRootType(Type serviceType)
   at SimpleInjector.Container.GetInstance(Type serviceType)
   at SimpleInjector.Container.GetAllInstances(Type serviceType)
   at MediatR.Internal.RequestHandlerImpl`2.GetPipeline(TRequest request, RequestHandlerDelegate`1 invokeHandler, MultiInstanceFactory factory)
   at MediatR.Internal.RequestHandlerImpl`2.Handle(IRequest`1 request, CancellationToken cancellationToken, SingleInstanceFactory singleFactory, MultiInstanceFactory multiFactory)
   at MediatR.Mediator.Send[TResponse](IRequest`1 request, CancellationToken cancellationToken)

My container configuration looks like:

this.container.Register<IMediator, Mediator>(Lifestyle.Scoped);
this.container.Register(typeof(ICancellableAsyncRequestHandler<,>), assemblies, Lifestyle.Scoped);
this.container.Register(() => new SingleInstanceFactory(this.container.GetInstance), Lifestyle.Scoped);
this.container.Register(() => new MultiInstanceFactory(this.container.GetAllInstances), Lifestyle.Scoped);

Most helpful comment

This is how I did it so far:

container.RegisterCollection(typeof(IPipelineBehavior<,>), Enumerable.Empty<Type>());
container.RegisterCollection(typeof(IRequestPreProcessor<>), Enumerable.Empty<Type>());
container.RegisterCollection(typeof(IRequestPostProcessor<,>), Enumerable.Empty<Type>());

The option container.Options.ResolveUnregisteredCollections = true; is obsolete. So you really need to register those dependencies explicitly.

All 8 comments

I see now that there are new registration requirements. I have no need for the pipeline functionality at the moment, but will later. Is there a way to configure the pipeline without having to create extra classes only to satisfy the pipelines requirements?

So I got everything working now by creating a passthrough pipeline behavior. Can this be added as part of Mediatr so people don't have to write their own? Much like you've already provided a Pre and Post pipeline implementations.

this.container.RegisterCollection(typeof(IPipelineBehavior<,>), new[]{ typeof(DefaultNoOpPipelineBehavior<,>) }); 
public class DefaultNoOpPipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next)
    {
        return next();
    }
}

This would be useful for people like myself who want to take advantage of the simpler IRequest interfaces in 3.0 without having to immediately understand how the pipeline works.

I think you don't need to create your own IPipelineBehavior. What is happening is that MediatR is trying to query the container if you have any registered.

Since you don't and you're using the assertive GetInstance methods versus some TryGetInstance, you do get that exception.

But I can be mistaken.

To reduce the thrown exceptions while resolving instances though the factories I use an extension method

public static object ReturnNullIfServiceTypeNotFound(this SimpleInjector.Container container, Type serviceType)  
{
    if (!container.IsRegistered(serviceType)) return null;
    return container.GetInstance(serviceType);
}

I struggled with this one - should you have to register all these things WITH MediatR? Or just have MediatR inspect the container.

I also looked at having MediatR just support IServiceProvider, then it can use the TryXYZ overloads that ASP.NET Core uses.

But then not every container supports IServiceProvider OOTB.

This is how I did it so far:

container.RegisterCollection(typeof(IPipelineBehavior<,>), Enumerable.Empty<Type>());
container.RegisterCollection(typeof(IRequestPreProcessor<>), Enumerable.Empty<Type>());
container.RegisterCollection(typeof(IRequestPostProcessor<,>), Enumerable.Empty<Type>());

The option container.Options.ResolveUnregisteredCollections = true; is obsolete. So you really need to register those dependencies explicitly.

@jbogard I am also getting this exception so had to do what @Rookian suggested. Its not clear in the documentation.

In the newer versions of MediatR, @Rookian 's code becomes:

container.Collection.Register(typeof(IPipelineBehavior<,>), Enumerable.Empty<Type>());
container.Collection.Register(typeof(IRequestPreProcessor<>), Enumerable.Empty<Type>());
container.Collection.Register(typeof(IRequestPostProcessor<,>), Enumerable.Empty<Type>());
Was this page helpful?
0 / 5 - 0 ratings