Access HttpContext.Current from WCF

Wcf doesnt have an HttpContext. if you want to store data in a user session all you need to do is set teh InstanceContextMode of the service class to Session, then use a channel that supports sessions such as wsHttpBinding. What this causes is a new instnace of teh service to be created per session. By default a new instance of the service is created with each and every call. So if a new instance of the service is created per session, then that means that all of the local varaibles in the service are implicitly per session. So you can just store the values there.

You can get access to HttpContext.Current by enabling AspNetCompatibility, preferably via configuration:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

You can even enforce AspNetCompatibility by decorating your service class with an additional attribute
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: