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 IAsyncDisposable services #1582

Open
Dzliera opened this issue Sep 16, 2024 · 2 comments
Open

Support IAsyncDisposable services #1582

Dzliera opened this issue Sep 16, 2024 · 2 comments

Comments

@Dzliera
Copy link

Dzliera commented Sep 16, 2024

CAP subscribe currently does not work if I use scoped service that implements IAsyncDisposable but does not implement IDisposable. For example:

public interface IMyScopedServiceExample : IAsyncDisposable // does not implement IDisposable
{
}

[CapSubscribe("customers")]
public class CustomersSubscriberService : ICapSubscribe
{
    private readonly IMyScopedServiceExample _scopedService;

    public CustomerSubscriberService(IMyScopedServiceExample scopedService)
    {
        this.scopedService = scopedService
    }

    [CapSubscribe("create", isPartial: true)]
    public void Create(Customer customer)
    {
    }
}

Exception is thrown while handling event:

MyScopedServiceExample type only implements IAsyncDisposable. Use DisposeAsync to dispose the container

This happens because CAP does use IServiceScope which does not support IAsyncDisposable services.

Proposed solution:
Change this line https://github.com/dotnetcore/CAP/blob/master/src/DotNetCore.CAP/Internal/ISubscribeInvoker.Default.cs#L44C9-L44C58
To:

        await using var scope = _serviceProvider.CreateAsyncScope();
@yang-xiaodong
Copy link
Member

Thanks for your feedback, Will be improved in the next version.

@yang-xiaodong
Copy link
Member

Fixed in version 8.3.0-preview-243613753

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

No branches or pull requests

2 participants