0

We have developed an ASP Net MVC application using the Repository pattern. We are creating a db context instance by using a context provider class like:

public class ContextProvider

        public static DBEntities GetContext()
        {
            return HttpContext.Current.Items["_EntityContext"] as DBEntities;
        }
    }

Here we are making sure that the DBEntities db call exists only during the existence of the request - we are putting an instance into a Session map - HttpContext.Current.Items["_EntityContext"] in this example.

We are using this in our entire Asp Net Mvc Project as following:

public class TeamRepository
{
    #region Members

    private DBEntities storeDB = null;

    #endregion Members

    #region Constructors

    public TeamRepository()
    {
        storeDB = ContextProvider.GetContext();

    }

    #endregion Constructors

    #region Methods

...

Now we need to create a WCF service to enable access to some features to other vendors.

Since all the Repository classes are a part of a project - they were not excluded to a separated DLL I made a reference to the entire project in my new WCF project so that I could use already existing DB Repository method calls.

Here I am facing an issue since I am not able to access to the Session variable HttpContext.Current.Items["_EntityContext"] - method call public static DBEntities GetContext() is always returning null when called from WCF Service.

I tried to make HttpContext.Current Available Within a WCF Service available by placing

[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
[AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Required)] 

on my Service class, and tweaking the serviceHostingEnvironment section of web.config, which now looks like this:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
    <baseAddressPrefixFilters>
        <add prefix="http://localhost” />
    </baseAddressPrefixFilters>
</serviceHostingEnvironment>

but with no results.

I am using Windows 10.

Do you know a way I can access HttpContext.Current.Items["_EntityContext"]... contained within Asp Net Mvc project from my WCF Project?

Regards

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
Jovo Krneta
  • 548
  • 1
  • 14
  • 36
  • 2
    The architectural answer would be to use Dependency Injection, and thereby remove any http.* type references in your repositories. – Frank Nielsen Jan 08 '20 at 16:00
  • There is no HTTPContext in WCF, WCF session is different from the HTTP session. Official explanation. https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet?redirectedfrom=MSDN Here are some pertinent discussions. https://stackoverflow.com/questions/59637749/c-sharp-an-object-reference-is-required-for-the-non-static-field-method-or-pro https://stackoverflow.com/questions/5990479/httpcontext-in-wcf – Abraham Qian Jan 09 '20 at 09:26
  • You have this post that gives some instructions how it may be used: https://dylanbeattie.net/2011/02/24/making-httpcontextcurrent-available.html - this says that it can be accessed... – Jovo Krneta Jan 09 '20 at 09:33
  • Here, also says it can be used https://stackoverflow.com/questions/5904313/access-httpcontext-current-from-wcf-web-service/5904345 – Jovo Krneta Jan 09 '20 at 10:01

1 Answers1

0

The issue is resolved using the following steps: I decorated my service implementation with the AspNetCompatibilityRequirements attribute:

[AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Required)]
public class Service : IService {
   . . .
}

The last thing I had to do was necessitated by WCF not supporting multiple host headers; I had to hard-wire the WCF endpoint to listen on a specific hostname. In this case, this involved tweaking the serviceHostingEnvironment section of web.config, which now looks like this:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
    <baseAddressPrefixFilters>
        <add prefix=http://services.mydomain.com” />
    </baseAddressPrefixFilters>
</serviceHostingEnvironment>

And then adding another attribute to the service implementation class and initializing the HttpContext.Current.Items session:

[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
[AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Required)]
public class Service : IService {
HttpContext.Current.Items["_EntityContext"] = new DBEntities();
...
}
Jovo Krneta
  • 548
  • 1
  • 14
  • 36