Unity 2.0 and handling IDisposable types (especially with PerThreadLifetimeManager)

前端 未结 4 918
无人共我
无人共我 2020-12-02 16:50

I know that similar question was asked several times (for example: here, here,here and here) but it was for previous versions of Unity where the answer was dependent on used

4条回答
  •  情深已故
    2020-12-02 17:33

    would it be a viable solution to use the HttpContext.Current.ApplicationInstance.EndRequest event to hook to the end of the request and then disposing of the object stored in this lifetime manager? like so:

    public HttpContextLifetimeManager()
    {
        HttpContext.Current.ApplicationInstance.EndRequest += (sender, e) => {
            Dispose();
        };
    }
    
    public override void RemoveValue()
    {
        var value = GetValue();
        IDisposable disposableValue = value as IDisposable;
    
        if (disposableValue != null) {
            disposableValue.Dispose();
        }
        HttpContext.Current.Items.Remove(ItemName);
    }
    
    public void Dispose()
    {
        RemoveValue();
    }
    

    you don't have to use a child container like the other solution and the code used to dispose the objects is still in the lifetime manager like it should.

提交回复
热议问题