Home > Uncategorized > Access HttpContext.Current from WCF

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:

<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>

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

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

%d bloggers like this: