FuncLun / MediatR.ConnectR

ConnectR is a wrapper around MediatR with the goal of connecting distributed processes. The initial Implementation is HttpClient to AspNetCore Middleware. Future plans include Azure Service Bus, NServiceBus, etc. MediatR will be the Lowest Common Denominator for distributing process communication.
Apache License 2.0
2 stars 2 forks source link

Error constructing handler for request of type MediatR.IRequestHandler`2[]. Register your handlers with the container. See the samples in GitHub for examples. #4

Open ghost opened 5 years ago

ghost commented 5 years ago

Issue: Not working in WCF. It is working in console

Setup: builder.RegisterHttpClientRequestHandler

at MediatR.Internal.RequestHandlerBase.GetHandler[THandler](ServiceFactory factory) at MediatR.Internal.RequestHandlerWrapperImpl2.<>c__DisplayClass0_0.<Handle>g__Handler|0() at MediatR.Pipeline.RequestPreProcessorBehavior2.d2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MediatR.Pipeline.RequestPostProcessorBehavior`2.d2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

FuncLun commented 5 years ago

ConnectR depends on MediatR. MediatR depends on Dependency Injection. MediatR is calling it's GetHandler function, using the ServiceFactory (i.e. DI container). The DI container is probably returning null or throwing. MediatR catches and rethrows, setting the InnerException. You have to look at InnerException to find where your problem is.

In your case, you're using Autofac (that information is not knowable from your post). There will probably 2 or 3 layers of InnerException to dig through. Or, carefully read the Autofac exception. They are hard to read, but they actually have all the information in them.

You can reproduce the issue by getting an instance of ILifetimeScope and try to lifetimeScope.Resolve<IRequestHandler<TRequest,TResponse>>().

The issue shouldn't have anything to do with WCF, other than each application has it's own DI container setup. The resolution is the problem will probably be in DI setup. The IRequestHandler<,> may be registered and there's a dependency that is not registered (possibly HttpClient that needs to be registered .AsSelf().

If the handler is not registered, the issue may be with not running .RegisterAssemblyMediatorHandlers<>() or .RegisterMediatorRequestWrappers<>() with the proper class. It should be a handler. The extensions will register the assembly that contains that handler.

Let me know how it goes.