2008年9月5日 星期五

WCF 使用ASP.NET 的Session

http://silverlight.net/forums/p/23080/81722.aspx

Hello, session is a server side concept.
Silverlight runs on the client, so it has no
knowledge of the session. But you can pass some
variable from the Silverlight application to the
server via a web service. Then in the web
service, you store it as a session variable.
Later when you call another web service, you can
check the session variable and return it to the
client.

While WCF also has built-in session
support, BasicHttpBinding doesn't support that.
So you need to use ASP.NET session instead. To
enable ASP.NET session on WCF, you need to turn
on ASP.NET Compatibility. Do the following:



  • In web.config, under system.serviceModel
    tag, add this line:


<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>



  • On your service class (not interface), add this attribute:


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]



  • Now you can use ASP.NET session with the
    following syntax:


HttpContext.Current.Session["YourName"] = something;


object something = HttpContext.Current.Session["YourName"];

沒有留言: