在WCF UsernamePasswordValidator中访问当前的InstanceContext [英] Access the current InstanceContext in a WCF UsernamePasswordValidator

查看:118
本文介绍了在WCF UsernamePasswordValidator中访问当前的InstanceContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用自定义UsernamePasswordValidator的WCF服务.验证者需要访问我的实体框架上下文.

I have a WCF service that is using a custom UsernamePasswordValidator. The validator needs to access my entity framework context.

我想为整个服务调用创建一个ObjectContext,然后在调用结束时销毁/处置它.因此,我创建了一个提供此功能的单例静态类,但是,现在发生的是,如果同时发生两个服务调用,则其中一个调用将处理单例.

I would like to create one ObjectContext for the entire service call and then destroy/dispose it at the end of the call. So I created a singleton static class that provided this functionality, however, what's happening now is that if two service calls happen concurrently, one of the calls disposes the singleton.

我要么保留对ObjectContext的本地引用,在这种情况下,使用它的第二个服务将其视为已处置并抛出并出错,或者在需要的地方将包装器属性放置在Singleton类周围,然后将所有更改被丢弃,因为如果另一个调用将其释放,我将获得该对象的新实例.

I either keep a local reference to the ObjectContext, in which case the second service to use it sees it as disposed and throws and error, or, I put a wrapper property around the Singleton class wherever I need it and then all my changes get thrown away because I'm getting a new instance of the object if another call has disposed it.

所以基本上我的问题是如何在每个服务调用中实例化ObjectContext?

So basically my question is how do I instantiate an ObjectContext per service call?

注意:该实例必须在服务代码和自定义UsernamePasswordValidator代码中均可访问.

我不能只在构造函数中使用它,也不能使用using语句,因为这样自定义UsernamePasswordValidator无法访问它.有没有办法让每个调用都有一个静态类?听起来确实不可能,但是如何解决呢?我应该在会话中缓存对象吗?

I can't just do it in the constructor or use a using statement because then the custom UsernamePasswordValidator doesn't have access to it. Is there a way to have a static class per call? It does sound impossible, but what's the way around this? Should I be caching the object in a session?

我的服务托管在IIS中.

My service is hosted in IIS.

更新:
因此,我已将其确定为使用IExtension对象将状态存储在InstanceContext中.但是,如何在UsernamePasswordValidator中访问当前的InstanceContext?

UPDATE:
So I've nailed this down to storing state in the InstanceContext using an IExtension object. But How do I access the current InstanceContext in a UsernamePasswordValidator?

推荐答案

好,所以最终我通过使用以下静态类并依靠ASP.NET为我缓存上下文解决了该问题.

Ok, so in the end I solved it by using the following static class and relying on ASP.NET to cache the context for me.

我不确定这是否是最好的处理方式,但是这允许我为每个请求使用一个ObjectContext,因此我不必花太多时间,这也意味着我不必使用锁如果很多用户正在使用该服务,它将变成一场噩梦.

I'm not sure if this is the best way to do things, but this allows me to use one ObjectContext per request so I'm not spinning up too many and this also means I don't have to use a lock on the object which would become a nightmare if many users were using the service.

public static class MyContextProvider
    {
        public static MyModel Context
        {
            get
            {
                if (HttpContext.Current.Items["context"].IsNull())
                {
                    HttpContext.Current.Items["context"] = new MyModel();
                }

                return HttpContext.Current.Items["context"] as MyModel;
            }
        }    
    }

然后只要我在应用程序中需要ObjectContext的地方调用

Then wherever I need an ObjectContext in the app I just call

var context = MyContextProvider.Context;

这篇关于在WCF UsernamePasswordValidator中访问当前的InstanceContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆