gpt4 book ai didi

c# - Owin.AppBuilderExtensions 中的内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:14 27 4
gpt4 key购买 nike

我在 Web 应用程序中使用 OWIN + Microsoft.AspNet.Identity.Owin (v.2.0.0.0)。我按照广泛推荐的方式为每个 Web 请求注册 UserManager/DbContext:

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

但两者都没有被处理掉。我瞥了一眼反射器,它似乎是扩展方法中的一个错误:

public static IAppBuilder CreatePerOwinContext<T>(this IAppBuilder app, Func<IdentityFactoryOptions<T>, IOwinContext, T> createCallback) where T: class, IDisposable
{
if (app == null)
{
throw new ArgumentNullException("app");
}
if (createCallback == null)
{
throw new ArgumentNullException("createCallback");
}
object[] args = new object[1];
IdentityFactoryOptions<T> options = new IdentityFactoryOptions<T> {
DataProtectionProvider = app.GetDataProtectionProvider()
};
IdentityFactoryProvider<T> provider = new IdentityFactoryProvider<T> {
OnCreate = createCallback
};
options.Provider = provider;
args[0] = options;
app.Use(typeof(IdentityFactoryMiddleware<T, IdentityFactoryOptions<T>>), args);
return app;
}

IdentityFactoryProvider 有两个回调 - create 和 dispose,但是这里没有注册 dispose 回调。我还用内存分析器证实了我的怀疑。

我在 codeplex/github 上没有看到 Owin(实际上我认为它是开源的),所以我不知道去哪里问我的问题:其他人可以确认这是内存泄漏吗?我不太确定,因为谷歌对此只字不提,我希望它应该到处讨论,如果这是一个错误。

最佳答案

我也有他的问题,在 CreatePerOwinContext 注册的任何东西都不会被处理掉。我正在使用 v2.1。

这是一个临时修复程序,在修复此库之前,它对我来说效果很好。您基本上必须在以下类中手动注册使用 CreatePerOwnContext 注册的每种类型,然后在启动过程结束时注册此自定义类:

public sealed class OwinContextDisposal : IDisposable
{
private readonly List<IDisposable> _disposables = new List<IDisposable>();

public OwinContextDisposal(IOwinContext owinContext)
{
if (HttpContext.Current == null) return;

//TODO: Add all owin context disposable types here
_disposables.Add(owinContext.Get<MyObject1>());
_disposables.Add(owinContext.Get<MyObject2>());

HttpContext.Current.DisposeOnPipelineCompleted(this);
}

public void Dispose()
{
foreach (var disposable in _disposables)
{
disposable.Dispose();
}
}
}

在启动过程的最后注册这个类:

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

现在所有内容都将在请求管道的末尾正确处理。

关于c# - Owin.AppBuilderExtensions 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27617659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com