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
分享到:
相关推荐
代码中实现了在 WCF 中实现可靠 Session 的测试用例,其中也涉及到如何将一个 WCF 服务发布到 IIS 中,目前唯一不能确定的就是在 NLB 的环境中是否也可以实现可靠 Session ,如果哪位网友有这样的资料,也希望共享...
wcf 通信模型 percall persession single 经过整理的WCF通信模型范例,强烈推荐
演示了wcf中怎样用IsInitiating,IsTerminating创建会话和销毁会话。
WCF同时支持AJAX和SOAP进行调用
wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例wcf经典实例
1、基于WCF框架对REST架构的WEB(支持SOAP)服务实现; 2、支持javascript的多种方式跨域访问(GET/POST/PUT/DELETE); 3、解决服务调用参数传递的动态设计数据格式(XML/JSON); 4、同时支持AJAX和SOAP进行调用...
这是一个基于WCF的一个轻量级但很实用的模块服务框架及服务托管程序MSH,目前版本支持的主要功能有: 1、支持WCF的路由中转服务2、支持自定义通道上下文。利用通道上下文可以方便地在调用 WCF服务时传递额外数据,使...
wcf 代理 wcf开发示例 wcf 代理 wcf开发示例
一套简单的 WCF 宿主 !一套简单的 WCF 宿主 !一套简单的 WCF 宿主 !一套简单的 WCF 宿主 !一套简单的 WCF 宿主 !一套简单的 WCF 宿主 !
非常不错的Wcf!
利用WCF对数据库的数据进行操作。这是我觉得还不错的一个例子。
WCF服务,配置可同时支持web调用和WCF调用,可以用于C#程序服务访问,也支持java等程序访问。java通过web调用,C#可以通过wcf访问。
wcf教程,基础,事件,高级,扩展,每章都有代码,由浅入深,很好的WCF编程教材。
上册的前四章在对WCF进行总体介绍的基础上,对构成终结点的三要素(地址、绑定和契约)进行了系统说明;随后的两章则着重剖析序列化和消息编码在WCF中的实现;第7、8章讲述了在服务寄宿和操作调用过程中,WCF的...
WCF服务学习1,2WCF服务学习1,2WCF服务学习1,2WCF服务学习1,2WCF服务学习1,2WCF服务学习1,2
wcf对数据库的增删改查的综合实例,里面描述了常见的错误处理方式,是学习WCF入门的很不错的实例
金旭亮WCF课程系列之WCF概述 仅供下载参考使用
WCF第一步 WCF第一步 WCF第一步 WCF第一步 WCF第一步 WCF第一步
WCF4高级编程Professional.WCF.4.rar
如何利用WCF对事务的支持将分布式事务引入服务(第3章);如何利用并发与限流机制提高服务的吞吐量和可用性(第4章);如何利用可靠会话机制确保消息的“使命必达”(第5章);如何利用队列服务提供离线通信的支持...