Tipically my application’s EF model is consumed by the website itself, so I use the following extension method to grant a context for the duration of the request:
public static class HttpContextExtensions
{
internal const string _contextDataKey = "dataContext";
public static EntityDataModelContext GetDataContext(this HttpContext httpContext)
{
if (httpContext == null) // services
return new EntityDataModelContext();
if (httpContext.Items[_contextDataKey] == null)
httpContext.Items.Add(_contextDataKey, new EntityDataModelContext());
return (EntityDataModelContext)httpContext.Items[_contextDataKey];
}
}
When a newly created WCF service attempted to get a data context, I stumbled upon the fact that a service holds no HttpContext in my application.
Now the solution I provided here “works”, but I’d rather have a similar solution to what was done with the HttpContext.
Where can I store a data context for a service request?
You have to create
IExtensionimplementation forOperationContext. Here is some example how to do that.