c# - Access the current InstanceContext in a WCF UsernamePasswordValidator -
i have wcf service using custom usernamepasswordvalidator. validator needs access entity framework context.
i create 1 objectcontext entire service call , destroy/dispose @ end of call. created singleton static class provided functionality, however, what's happening if 2 service calls happen concurrently, 1 of calls disposes singleton.
i either keep local reference objectcontext, in case second service use sees disposed , throws , error, or, put wrapper property around singleton class wherever need , changes thrown away because i'm getting new instance of object if call has disposed it.
so question how instantiate objectcontext per service call?
note: instance needs accesible in both service code , custom usernamepasswordvalidator code.
i can't in constructor or use using statement because custom usernamepasswordvalidator doesn't have access it. there way have static class per call? sound impossible, what's way around this? should caching object in session?
my service hosted in iis.
update:
i've nailed down storing state in instancecontext using iextension object. how access current instancecontext in usernamepasswordvalidator?
ok, in end solved using following static class , relying on asp.net cache context me.
i'm not sure if best way things, allows me use 1 objectcontext per request i'm not spinning many , means don't have use lock on object become nightmare if many users using service.
public static class mycontextprovider { public static mymodel context { { if (httpcontext.current.items["context"].isnull()) { httpcontext.current.items["context"] = new mymodel(); } return httpcontext.current.items["context"] mymodel; } } }
then wherever need objectcontext in app call
var context = mycontextprovider.context;
Comments
Post a Comment