views:

245

answers:

2

I need to use or stimulate a very simple session object inside my WCF app.

I simply need to store some values at the beginning of a call and I need access to these values while I go through some different methods of my service.

Asp.NET session would be very ideal to use for this so I need to find out what is available on a WCF app for storing such values.

Note: this is just a per call session, I don't need to retain this session between different calls from the client to service and such..

A: 

You have to do a couple of things.

Set aspNetCompatibilityEnabled = true on your host (inside system.ServiceModel | serviceHostingEnvironment)

The service (not the contract) - should be attributed with:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

Finally you have you have to set allowCookies on your binding to true.

<basicHttpBinding>
 <binding name="SessionBinding" allowCookies="true">
</basicHttpBinding>

Just curious, are you certain you need session?

RandomNoob
+2  A: 

What your describing (custom context that is retained for the life of a single request) is more like HttpContext.Items than ASP.NET Session.

WCF does have a class OperationContext that is similar to HttpContext. It doesn't have an Items property for custom context, but this blog post describes a technique you can use to achieve what you want.

Joe
This worked great for me, thanks!
kaivalya