views:

106

answers:

2

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

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.

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.

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

NOTE: The instance needs to be accesible in both the service code AND the custom UsernamePasswordValidator code.

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?

My service is hosted in IIS.

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?

A: 

For your service, you can specify a service behaviour which details the instance mode of the service:

[ServiceBehaviour(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService {
    ObjectContext context;
}
Matthew Abbott
I already have that attribute. The problem I'm having is sharing the context between the actual service code and the UsernamePasswordValidator code.
robertFall
A: 

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.

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;
            }
        }    
    }

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

var context = MyContextProvider.Context;
robertFall