Skip to content

IDisposable dependencies does not get disposed when web request ends #627

@Madajevas

Description

@Madajevas

Hey. I have the following in .NET 4.8 WEB API project:

// disposable dependency
public interface ITheDependency { }
class TheDependency : ITheDependency, IDisposable
{
    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

// WebApiConfig.cs.Register
var container = new Container();
container.Register<ITheDependency, TheDependency>(
    Reuse.InCurrentScope                                                      // tried various scopes here
    // setup: Setup.With(allowDisposableTransient: true)
    );
container.WithWebApi(config);

// and dependant controller
public class ValuesController : ApiController
{
    private readonly ITheDependency dependency;
    private readonly IContainer container;

    public ValuesController(ITheDependency dependency, IContainer container)
    {
        this.dependency = dependency;
        this.container = container;
    }

    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    public string Get(int id)
    {
        using (var scope = container.OpenScope())
        {
            var disposable = scope.Resolve<ITheDependency>();
        }

        return "value";
    }
}

New instance of TheDependency gets created and injected into ValuesController each time controller is necessary, however, once request is finished Dispose is never called on TheDependency. If scope is created and disposed manually (as in Get action with parameter) and TheDependency get resolved using that scope, Dispose is invoked.

Shouldn't disposable dependencies be disposed when web request scope is being disposed? What am I missing? DryIoc.WebApi.dll v5.0.0 is used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions