question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Manual Mediator Registration Issue (v3.0.1->v5.0.1)

See original GitHub issue

First: Thank you very much for this awesome project, it has inspired me a lot and structures my project architecture so much better. Thanks!

I am currently migrating from the old version 3.0.1 to the latest 5.0.1. Since I use the SimpleIoc from the MvvmLight framework, this was how I registered and setup MediatR in version 3:

var ioc = SimpleIoc.Default;
ioc.Register<IAsyncRequestHandler<DoJobRequest>, DoJobUseCase>();

var mediator = new Mediator(
          new SingleInstanceFactory(ioc.GetInstance),
          new MultiInstanceFactory(ioc.GetAllInstances)
);

await mediator.Send(new DoJobRequest());

With version 5.0.1 and the removal of the MultiInstanceFactory, I tried this (as described in the migration wiki):

var ioc = SimpleIoc.Default;
ioc.Register<IRequestHandler<DoJobRequest>, DoJobUseCase>();

var mediator = new Mediator(
          new ServiceFactory(ioc.GetInstance)
);

await mediator.Send(new DoJobRequest());

But I get the error, that the IEnumerable<IPipelineBehavior<...>> was not found… How should I setup MediatR now to get it going again?

Thank you very much.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Brimcecommented, Jul 20, 2018

Hi, I’m in the same situation with splat container.

My solution :

 _dependencyResolver.RegisterLazySingleton<ServiceFactory>(() => (type =>
            {
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
                {
                    var enumerableType = type.GetGenericArguments()[0];

                    var pipelineInfo = enumerableType.GetGenericArguments()[0];

                    var requestType = pipelineInfo
                        .GetInterfaces()
                        .FirstOrDefault(type1 => type1.IsGenericType && type1.GetGenericTypeDefinition() == typeof(IRequest<>));

                    var responseType = requestType.GetGenericArguments()[0];

                    var typeArguments = new Type[2];
                    typeArguments[0] = requestType;
                    typeArguments[1] = responseType;
                    var pipelineType = typeof(GenericPipelineBehavior<,>).MakeGenericType(typeArguments);

                    var enumerableTypeResult = typeof(List<>).MakeGenericType(pipelineType);
                    var list = (IList) Activator.CreateInstance(enumerableTypeResult);

                    var pipeline = Activator.CreateInstance(typeof(GenericPipelineBehavior<,>).MakeGenericType(requestType, responseType));

                    list.Add(pipeline);

                    return list;
                }

                return _dependencyResolver.GetService(type);
            }));

Just replace _dependencyResolver with _ioc and add the folowing class:

 public class GenericPipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
 {        
        public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
        {
            //log... "-- Handling Request"
            var response = await next();
            //log "-- Finished Request"
            return response;
        }
}

I Hope it can help !

0reactions
jbogardcommented, Jul 23, 2018

The problem that IServiceLocator is pretty much a dead end. A very old version of MediatR used IServiceLocator, but once that concept got abandoned, as did my usage of it.

Interestingly, the new ASP.NET Core DI is just IServiceProvider, which has existed in .NET since…forever. Everything else is extension methods on that, or extensions like IServiceScope

Read more comments on GitHub >

github_iconTop Results From Across the Web

Untitled
... vega pics Implement the microservice application layer using the Web API Manual Mediator Registration Issue (v3.0.1->v5.0.1) #296 - GitHub Web30 dec.
Read more >
Forms and Applications Used for Mediation
Most of the following forms can be completed online and printed. Forms and Applications Used for Mediation. Mentee Evaluation Form [Form ADR-1001] Word...
Read more >
Mediation
The Mediation Information System is an online database used to capture information regarding court-referred mediations. The MIS system is for use by court ......
Read more >
Mediator education & registration
In civil cases, a registered mediator must be an attorney in good standing with the Supreme Court of Indiana and must complete at...
Read more >
Mediation | Superior Court of California - County of San Diego
Review the Mediator Application (CIV-023)PDF and Mediator ManualPDF to learn more about the panel requirements;; Submit your completed Mediator Application (CIV ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found