wcf对session的支持

We are hosting WCF in ASPNET compatible mode and utilize ASPNET session handling feature, it works very well. You can get more than session handling, I mean many of ASPNET built in features, when hosting your WCF in ASPNET compatible mode.

To host WCF in ASPNET compatible mode:

1. Put this attribute before your implementation class for your interface: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
2. Add this to <system.serviceModel> in your Web.config: <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
To add session handling feature, add <sessionState> attribute to your Web.config under <system.web>. You do have 2 ways to handle your session, the cookie way and the cookieless way:
* <sessionState mode="InProc" cookieless="false" regenerateExpiredSessionId="true" timeout="20"/> for cookie way
* <sessionState mode="InProc" cookieless="true" regenerateExpiredSessionId="false" timeout="20"/> for cookieless way

If you do enable cookie for your site, a client session will be grant as soon as you write something to HttpContext.Current.Session dictionary, and the session will expire when timeout or when you call to HttpContext.Current.Session.Abandon(). If you use cookieless, the client session will be grant anyway per client connect.

Silverlight has no problem with ASPNET cookie session, but cookieless session. When you use cookieless session, the sessionID is passed via URL, and ASPNET will return a HTTP 302 response to inform new address (which sessionID embedded), normally browser will divert the call to this new address, but Silverlight does not. So you have to manually divert it by making a pre-HttpRequest to get the address:

// Creating a proxy to WCF
private void CreateProxy()
{
try
{
resultText.Text = "Please wait while we are connecting to the service ...";
// Get the current URI on the fly, then modify the html file name to our service name, then
// use this to make web request
string[] URIparts = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri.Split('/');
string serviceURIString = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri.Replace(URIparts[URIparts.Length - 1], "Service.svc");
// For cookieless problem, we first make a HttpWebRequest to the service,
// then use the URI return from the response to create proxy
Uri serviceUri = new Uri(serviceURIString);
HttpWebRequest HttpRequest = (HttpWebRequest)HttpWebRequest.Create(serviceUri);
HttpRequest.BeginGetResponse(new AsyncCallback(EndCreateProxy), HttpRequest);
}
catch (Exception ex)
{
exceptionText.Text = String.Format("{0}", ex.Message);
}
}

void EndCreateProxy(IAsyncResult asyncResult)
{
try
{
HttpWebRequest HttpRequest = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse HttpResponse = (HttpWebResponse)HttpRequest.EndGetResponse(asyncResult);
// Create a proxy to WCF
// Enable the following line to call to an SSL service
// Binding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
// Enable the following line to call to an nonSSL service
Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress(HttpResponse.ResponseUri);
proxy = new ServiceClient(binding, address);

resultText.Text = "Result goes here";
exceptionText.Text = "Exception goes here";
}
catch (Exception ex)
{
exceptionText.Text = String.Format("{0}", ex.Message);
}
}

Remember that you do not need to make this pre-HttpRequest if you use cookie session. But this schema also works with cookie session.


from: http://forums.silverlight.net/forums/t/14175.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值