© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
2 replies
florent

Will this service will be disposed using DependencyInjection?

I have the following class which is added to the default DI-container (Blazor WASM)
public class ClientGrpcService<TService> : IClientGrpcService<TService>, IDisposable where TService : class
{
    private readonly GrpcChannel _channel;
    private readonly TService _service;

    public ClientGrpcService()
    {
        _channel = GrpcChannel.ForAddress("https://localhost:7046");
        _service = _channel.CreateGrpcService<TService>();
    }

    public TService Service => _service;

    public void Dispose()
    {
        _channel.Dispose();
    }
}
public class ClientGrpcService<TService> : IClientGrpcService<TService>, IDisposable where TService : class
{
    private readonly GrpcChannel _channel;
    private readonly TService _service;

    public ClientGrpcService()
    {
        _channel = GrpcChannel.ForAddress("https://localhost:7046");
        _service = _channel.CreateGrpcService<TService>();
    }

    public TService Service => _service;

    public void Dispose()
    {
        _channel.Dispose();
    }
}

and In my classes I simply do
public TestClass(IClientGrpcService<Example> service)
{
...
}
public TestClass(IClientGrpcService<Example> service)
{
...
}

the
TestClass
TestClass
itself will be instantiated by the DI as well. Will the disposed method of the GrpcService will be called?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Using ` Microsoft.Extensions.DependencyInjection` in a library
C#CC# / help
4mo ago
SqlConnection Connection Disposed
C#CC# / help
15mo ago
TcpClient being Disposed
C#CC# / help
2y ago
❔ Will this operation be expensive or not?
C#CC# / help
3y ago