`
wumingdlz
  • 浏览: 31373 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
最近访客 更多访客>>
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics