简体   繁体   English

使用后处理 ObjectContext(按请求)

[英]Disposing ObjectContext (per-request) after use

I am using creating one ObjectContext per-request concept.我正在使用为每个请求创建一个 ObjectContext 概念。 Technically, I am adding ObjectContext instance to HttpContext.Current.Items.从技术上讲,我将 ObjectContext 实例添加到 HttpContext.Current.Items。 But I do not know how to kill this instance properly.但我不知道如何正确杀死这个实例。 Is it safe to use HttpModule and disposing ObjectContext within?使用 HttpModule 并在其中处理 ObjectContext 是否安全? I mean, HttpModule might be called for any kind of request.我的意思是,任何类型的请求都可能调用 HttpModule。 I do not want to use DI/IoC matters, because the project need to lightweight (no third party libs allowed).我不想使用 DI/IoC 事务,因为项目需要轻量级(不允许第三方库)。

UPDATE: here the simple code: Created a per-request ObjectContext (Entities class)更新:这里是简单的代码:创建了一个按请求的 ObjectContext(实体类)

public static class ObjectContextPerRequest
{
    public const string ObjectKey = "_per_request_context_key";

    public static Entities PerRequest
    {
        get
        {
            if (HttpContext.Current.Items[ObjectKey] != null)
            {
                var eContext = new Entities();
                HttpContext.Current.Items.Add(ObjectKey, eContext);

                return eContext;
            }

            return HttpContext.Current.Items[ObjectKey] as Entities;
        }
    }
}

and a disposer module:和一个处理器模块:

class ObjectContextManagerModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += (s, e) => { Dispose(); };
    }

    public void Dispose()
    {
        if (HttpContext.Current.Items[ObjectContextPerRequest.ObjectKey] != null)
        {
            var edmx = (ObjectContext)HttpContext.Current.Items[ObjectContextPerRequest.ObjectKey];
            edmx.Dispose();
            edmx = null;
        }
    }
}

It'll be safer to create the context during the BeginRequest and then dispose of it during either EndRequest or ReleaseRequestState (probably EndRequest).在 BeginRequest 期间创建上下文,然后在 EndRequest 或 ReleaseRequestState(可能是 EndRequest)期间处理它会更安全。 Init is when the module is fired up, Dispose is when the module itself is disposed, and modules don't get created and disposed on every request. Init 是模块被启动的时候,Dispose 是模块本身被释放的时候,并且模块不会在每个请求时被创建和释放。

* UPDATE for comment * *更新评论*

The module should use its Init method to attach to application events, like so:该模块应使用其 Init 方法附加到应用程序事件,如下所示:

public void Init(HttpApplication app)
{
   app.BeginRequest += new EventHandler(OnBeginRequest);
   app.EndRequest += new EventHandler(OnEndRequest);
}

Note that there are other syntaxes available, but that's the one that's documented on MSDN.请注意,还有其他可用的语法,但这是 MSDN 上记录的语法。

This will fire on every request that hits your application.这将在命中您的应用程序的每个请求上触发。 So if your IIS setup routes static file requests (eg images and css files) through the app (which is true for IIS 7 in integrated pipeline mode), then your event handlers need to account for that by not spinning up an ObjectContext instance in cases you don't need one. So if your IIS setup routes static file requests (eg images and css files) through the app (which is true for IIS 7 in integrated pipeline mode), then your event handlers need to account for that by not spinning up an ObjectContext instance in cases你不需要一个。

* UPDATE for MVC * * MVC 更新 *

Since you're using an MVC app, you could also consider doing this in a controller base class or in an actionfilter, using the OnActionExecuting and OnActionExecuted calls.由于您使用的是 MVC 应用程序,因此您还可以考虑在 controller 基础 class 或操作过滤器中执行此操作,使用 OnActionExecuting 和 OnActionExecuted 调用。

As an ActionFilter, you can ensure you apply it only to controllers that need the data context.作为 ActionFilter,您可以确保仅将其应用于需要数据上下文的控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NHibernate Windsor每个请求的实现 - NHibernate windsor per-request implementaion 在 Asp.net 内核中根据每个请求决定是否使用授权 - Deciding to use Authorization or not on a per-request basis in Asp.net Core Unity DI:从每个请求到全局单例再到每个请求 - Unity DI: Resolving from per-request to global singleton to per-request 在StartUp中使用Autofac解决每用户/每请求的依赖性 - Resolving per-user/per-request dependency with Autofac in StartUp 处置ObjectContext后,实体框架检查虚拟列表 - Entity Framework Check virtual Lists after disposing of ObjectContext 具有每个请求生存期范围的ServiceStack自托管应用程序 - ServiceStack self-hosted application with per-request lifetime scope 从Autofac函数工厂获取每个请求的依赖关系 - Get per-request dependency from an Autofac func factory 实体框架每个请求上下文-如何处理错误? - Entity Framework per-request context - how are errors handled? 具有依赖注入的Filters和FilterProvider; 管理每个请求? - Filters and FilterProvider with Dependency Injection; Manage Per-request? ASP.NET中的Per-Request静态数据 - Per-Request static data in ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM