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

Registering IAsyncDisposable implementations throws an InvalidOperationException #6659

Closed
danielmarbach opened this issue Jan 9, 2023 · 1 comment · Fixed by #6650
Closed
Labels

Comments

@danielmarbach
Copy link
Contributor

danielmarbach commented Jan 9, 2023

Symptoms

When registering Instance per unit of work implementations of IAsyncDisposable and resolving them as part of a message handler

public class MyHandler {
   public MyHandler(ScopedAsyncDisposable asyncDisposable) { }
}

the message will go through a series of retries and will eventually be moved to the error queue with a similar exception message:

2022-12-23 11:04:23.388 ERROR Moving message '1243a077-6606-4fc7-ae43-af7400a56572' to the error queue 'error' because processing failed due to an exception:
System.InvalidOperationException: 'AsyncDisposable' type only implements IAsyncDisposable. Use DisposeAsync to dispose the container.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.Dispose()
   at NServiceBus.MainPipelineExecutor.Invoke(MessageContext messageContext, CancellationToken cancellationToken) in /_/src/NServiceBus.Core/Pipeline/MainPipelineExecutor.cs:line 63
   at NServiceBus.LearningTransportMessagePump.ProcessFile(ILearningTransportTransaction transaction, String messageId, CancellationToken messageProcessingCancellationToken) in /_/src/NServiceBus.Core/Transports/Learning/LearningTransportMessagePump.cs:line 340

For Single instance implementations, the InvalidOperationException will be raised when attempting to stop the endpoint:

2023-01-12 17:10:23.046 INFO  Initiating shutdown.
Unhandled exception. System.InvalidOperationException: 'SingletonAsyncDisposable' type only implements IAsyncDisposable. Use DisposeAsync to dispose the container.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.Dispose()
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.Dispose()
   at NServiceBus.HostingComponent.Stop() in /_/src/NServiceBus.Core/Hosting/HostingComponent.cs:line 100
   at NServiceBus.RunningEndpointInstance.Stop(CancellationToken cancellationToken) in /_/src/NServiceBus.Core/Unicast/RunningEndpointInstance.cs:line 66
   at Program.Main() in /home/danielmarbach/Projects/docs.particular.net/samples/fullduplex/Core_8/Server/Program.cs:line 29
   at Program.<Main>()

Who's affected

You are affected if:

  • The dependency injection container is managed by NServiceBus
  • An implementation of IAsyncDisposable is registered by endpointConfiguration.RegisterComponents(…) as either:

Root cause

The internally managed container is not disposed using the IAsyncDisposable interface.

Backported to

@danielmarbach
Copy link
Contributor Author

Will be released with 8.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant