Memory leak in Owin.AppBuilderExtensions

后端 未结 4 2172
心在旅途
心在旅途 2020-12-15 05:27

I use OWIN + Microsoft.AspNet.Identity.Owin (v.2.0.0.0) in Web application. I register UserManager/DbContext per web request, as widely recommended:

app.Crea         


        
4条回答
  •  遥遥无期
    2020-12-15 06:20

    I also have his issue, nothing that is registered with CreatePerOwinContext gets disposed. I'm using v2.1.

    Here's a temporary fix which is working well for me as a work around until this lib is fixed. You basically have to manually register each of the types that use register with CreatePerOwnContext in the following class, and then at the end of your Startup procedure you register this custom class:

    public sealed class OwinContextDisposal : IDisposable
    {
        private readonly List _disposables = new List(); 
    
        public OwinContextDisposal(IOwinContext owinContext)
        {
            if (HttpContext.Current == null) return;
    
            //TODO: Add all owin context disposable types here
            _disposables.Add(owinContext.Get());
            _disposables.Add(owinContext.Get());
    
            HttpContext.Current.DisposeOnPipelineCompleted(this);
        }
    
        public void Dispose()
        {
            foreach (var disposable in _disposables)
            {
                disposable.Dispose();
            }
        }
    }
    

    At the end up your Startup process register this class:

     app.CreatePerOwinContext(
          (o, c) => new OwinContextDisposal(c));
    

    Now everything will get disposed of at the end of the request pipeline properly.

提交回复
热议问题