Skip to content

Unable to resolve endpoint-keyed root services without [FromKeyedServices] when multi-hosting #7834

Description

@danielmarbach

Description

In NServiceBus 10.2.x, when hosting an endpoint with a keyed endpoint identifier, services registered on the root IServiceCollection with that endpoint key are not resolved from within NServiceBus endpoint components.

This affects scenarios where endpoint-specific services are registered globally using Microsoft.Extensions.DependencyInjection keyed services, for example:

  services.AddKeyedSingleton<IMyComponent, MyComponent>(endpointIdentifier);
  services.AddNServiceBusEndpoint(endpointConfiguration, endpointIdentifier);

A handler, saga, feature startup task, or other endpoint-created component that depends on IMyComponent should resolve the service for its endpoint automatically, without requiring [FromKeyedServices].

Expected behavior

Endpoint-created components should resolve services registered on the root service collection with the endpoint identifier as their key.

For multiple hosted endpoints, each endpoint should resolve the service registered under its own endpoint identifier and should not resolve another endpoint’s registration.

Endpoint-local keyed services should continue to work independently from root endpoint-keyed registrations.

Actual behavior

Endpoint-created components do not resolve services registered globally under the endpoint identifier. The endpoint-scoped service provider only recognizes services registered through the endpoint’s local service collection adapter.

As a result, constructor injection of a root endpoint-keyed service can fail even though the service is registered in the underlying Microsoft DI container.

Who's affected

All users of NServiceBus version 10.2.x registering dependency injection services for multiple endpoints.

Versions

Affected package:

  • NServiceBus 10.2.x

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions