Particular / NServiceBus

Build, version, and monitor better microservices with the most powerful service platform for .NET
https://particular.net/nservicebus/
Other
2.07k stars 650 forks source link

AssemblyScanner finds private classes #7027

Open pardahlman opened 2 months ago

pardahlman commented 2 months ago

Describe the suggested improvement

Is your improvement related to a problem? Please describe.

We have an integration test suite where NServiceBus endpoints are started and message handlers with different behaviors are used to verify certain aspects of our code. The message handlers and related messages are declared as nested private classes to the test class.

Despite of this, the assembly scanner finds these private handlers and adds them to the DI container. This, in turn, makes the DI validation fail when building the IServiceCollection as the handlers from the other tests have constructor arguments not registered. Moreover, I can not exclude these handlers from the assembly scanning as they are private and not available to me (unless I get the assembly, find the type... but that does not feel right to me).

Describe the suggested solution

I can see how it would be a breaking change to only scan exported types. But to ignore private classes that implement IHandleMessages<> could perhaps be acceptable? Or if not, what about IHandleMessages<TMessage> where TMessage is private?

Describe alternatives you've considered

The workaround I have in place is to ignore the DI validation in my integration tests. That way, the handler is found, the container is invalid but the problematic services are never resolved.

Additional Context

No response

NChaganlal commented 2 months ago

Hi @pardahlman, Thank you for raising this issue. We will look into it and consider it for an upcoming enhancement release.