Comments (4)
Thank you! The solution you provided works!
from mediatr.
It looks like your bindings is not setup correctly.
Could you post your code where you setup your Ninject kernel?
from mediatr.
Hi @kiwipiet,
The code inside NinjectWebCommon is as follows:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
private static void RegisterServices(IKernel kernel)
{
kernel.Components.Add<IBindingResolver, ContravariantBindingResolver>();
kernel.Bind(scan => scan.FromAssemblyContaining<IMediator>).SelectAllClasses().BindDefaultInterface());
kernel.Bind(scan => scan.FromAssemblyContaining(typeof(IAsyncRequestHandler<,>)).SelectAllClasses().BindAllInterfaces());
kernel.Bind(scan => scan.FromAssemblyContaining(typeof(IAsyncRequest<>)).SelectAllClasses().BindAllInterfaces());
kernel.Bind<SingleInstanceFactory>().ToMethod(ctx => t => ctx.Kernel.Get(t));
kernel.Bind<MultiInstanceFactory>().ToMethod(ctx => t => ctx.Kernel.GetAll(t));
}
And I try to inject it in a ApiController like so:
private readonly IMediator mediator;
public PeopleController(IMediator mediator)
{
this.mediator = mediator;
}
from mediatr.
kernel.Bind(scan => scan.FromAssemblyContaining(typeof(IAsyncRequestHandler<,>)).SelectAllClasses().BindAllInterfaces()); kernel.Bind(scan => scan.FromAssemblyContaining(typeof(IAsyncRequest<>)).SelectAllClasses().BindAllInterfaces());
These are your problem lines. The assembly it is going to find in both cases is 'MediatR.dll'. And this assembly has already been bound in:
kernel.Bind(scan => scan.FromAssemblyContaining<IMediator>).SelectAllClasses().BindDefaultInterface());
If you have one assembly that contains your handlers and requests then only register that assembly once and don't re-register MediatR.dll
This is clearly a web app, and if you have all your requests and handlers in the web app assembly you could do the following:
kernel.Bind(scan => scan.From(Assembly.GetExecutingAssembly()).SelectAllClasses().InNamespaceOf(type).BindAllInterfaces());
or
kernel.Bind(scan => scan.FromAssemblyContaining<MyAsyncRequestHandler>).SelectAllClasses().BindDefaultInterface());
where MyAsyncRequestHandler is one of your handlers.
from mediatr.
Related Issues (20)
- ipipline handle are not work HOT 1
- How do I register custom interfaces with MediatR? HOT 4
- Is there a way to use MediatR with pure JSON string based on a property name? HOT 2
- ValueTask
- Filtering Registration on Handlers HOT 3
- Bug: Publish() command stuck in combination with await *.ToListASYNC() HOT 2
- Ninject binding issue with MediatR HOT 4
- HostedService getting re-initialized in every publish HOT 8
- Assembly.LoadFile("c:\SomePath\MyApp.dll") doesn't find handlers HOT 2
- How to replicate previous api for registering pre/post processors HOT 6
- Generic notification handler not called when non generic handler also exist HOT 2
- pipeline behaviour calling scoped object is loosing its state HOT 2
- System.Reflection.AmbiguousMatchException thrown in ServiceRegistrar HOT 2
- Skipping Pipeline when constraint applied for IRequest<TResponse> HOT 5
- System.InvalidOperationException: No service for type 'MediatR.IRequestHandler`2 has been registered. HOT 4
- Add single RequestHandler to DI HOT 5
- .NET 6 Target Dependencies HOT 4
- docs: I'm able to edit the wiki HOT 3
- .NET 8 Calling IPublisher.Publish method throws ObjectDisposedException for IServiceProvider HOT 1
- "No service for type 'MediatR.IRequestHandler' has been registered." issue HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mediatr.