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

Support multiple project #94

Open
Dotnetstore opened this issue May 31, 2023 · 7 comments
Open

Support multiple project #94

Dotnetstore opened this issue May 31, 2023 · 7 comments

Comments

@Dotnetstore
Copy link

Mediator seems not supporting multiple projects. I Have several projects - WebAPI, User, Company. I have mediator installed and correct setup in User and Company. Running those, only the first project registered in DI works.

serviceCollection.AddCompanyServices();
serviceCollection.AddUserServices();
= Company works correctly, User gets error "No handler registered for message type:"

serviceCollection.AddUserServices();
serviceCollection.AddCompanyServices();
= User works correctly, Company gets error "No handler registered for message type:"

@martinothamar
Copy link
Owner

I'm not sure what's going on here, could you describe in a little more detail how the project is laid out? In terms of project references and Mediator package references

We have a couple of samples where multiple projects are used, such as this one. Maybe that will help in debugging?

@Dotnetstore
Copy link
Author

Dotnetstore commented Jun 1, 2023

Thank you for a quick response. I have made a solution that is vertically sliced. I have several projects:

Company
User
WebAPI

There are more, but I want to keep it simple, Company and User are class libraries. Each project has its own Context. WebAPI are almost empty, besides from DI and other code that is needed. Controllers are in respective class library there they belong. In each class library, I have added mediator and implemented it just as you have in your documentation.

But when I run the application, only the first class library called in DI works. If I call Company first, it will work fine, but User will not work ("No handler registered for message type:"). If I have User first in my DI, it will work vine, but Company will not work ("No handler registered for message type:").

I have tried to move DI registration to the WebAPI project, but I will get ambigious reference, as it should. When I register it in each class library, I get the error I mentioned. My guess that it will only allow one registration in DI.

@martinothamar
Copy link
Owner

My guess that it will only allow one registration in DI.

Yes that sounds right, we can't have several IMediator implementations in the same DI container.

@martinothamar
Copy link
Owner

tracking some improvements around multiple projects here: #97

@XardasLord
Copy link

XardasLord commented Nov 21, 2023

So there is no way to have commands and queries in different projects registered? I have a situation and project structured as described by @Dotnetstore

@matthewblott
Copy link

this one

I've tried this but the weird thing is if you isolate the sample projects and add the Mediator references as NuGet packages the problem @Dotnetstore describes is still present.

@martinothamar
Copy link
Owner

So there is no way to have commands and queries in different projects registered?

You can have commands and queries in different projects, but you can have only one Mediator implementation. So for the project that your running, there can only be one Mediator.SourceGenerator in the dependy graph, otherwise multiple implementations are generated and so which one should the DI container hand you when you ask for IMediator

I've tried this but the weird thing is if you isolate the sample projects and add the Mediator references as NuGet packages the problem @Dotnetstore describes is still present.

I'll try to reproduce this with the multi-project samples.

@martinothamar martinothamar reopened this Dec 12, 2023
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

4 participants