bevacqua
bevacqua

Reputation: 48476

Give a consuming service a data context for the duration of the request?

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?

Upvotes: 0

Views: 144

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

You have to create IExtension implementation for OperationContext. Here is some example how to do that.

Upvotes: 1

Related Questions