2011-03-09 36 views
1

我非常新的Silverlight的,所以請假設我已經做了一些愚蠢的很....的Silverlight 4個HtmlPage.Document.Cookies總是空

我試圖從silverlight的撥打電話應用程序添加到WCF服務並檢查會話中的值。該值將通過一個aspx頁面放在那裏。這有點複雜,但那就是我們的位置。

我的服務是這樣的:

[ServiceContract] 
public interface IExportStatus 
{ 
    [OperationContract] 
    ExportState RequestExportComplete(); 
} 

public enum ExportState 
{ 
    Running, 
    Complete 
} 

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class ExportStatus : IExportStatus 
{ 
    ExportState IExportStatus.RequestExportComplete() 
    { 
     // check value of session variable here. 
    } 
} 

承載Silverlight應用程序還舉辦WCF服務的網站。它的Web配置是這樣的:

<configuration> 
    <system.serviceModel> 
     <services> 
      <service name="SUV_MVVM.Web.Services.ExportStatus" behaviorConfiguration="MyBehavior"> 
       <endpoint binding="basicHttpBinding" 
          bindingConfiguration="MyHttpBinding" 
          contract="SUV_MVVM.Web.Services.IExportStatus" 
          address="" /> 
      </service> 
     </services> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="MyHttpBinding" /> 
      </basicHttpBinding> 
     </bindings> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="MyBehavior"> 
        <serviceMetadata httpGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
     <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

我使用VS工具acepting默認值(除了用於命名空間)

起初,我只是想呼籲像服務添加服務引用到我的Silverlight應用程序這樣的:

var proxy = new ExportStatusClient(); 
proxy.RequestExportCompleteCompleted += (s, e) => 
             { 
              //Handle result 
             }; 

proxy.RequestExportCompleteAsync(); 

但在服務的會話總是空的(不是null,只是空的),所以我想手動設置會話ID到這樣的請求:

var proxy = new ExportStatusClient(); 
using (new OperationContextScope(proxy.InnerChannel)) 
{ 
    var request = new HttpRequestMessageProperty(); 
    //this might chnage if we alter the cookie name in the web config. 
    request.Headers["ASP.NET_SessionId"] = GetCookie("ASP.NET_SessionId"); 
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = request; 
    proxy.RequestExportCompleteCompleted += (s, e) => 
              { 
               //Handle result 
              }; 

    proxy.RequestExportCompleteAsync(); 
    } 

    private string GetCookie(string key) 
    { 
     var cookies = HtmlPage.Document.Cookies.Split(';'); 

     return (from cookie in cookies 
       select cookie.Split('=') 
       into keyValue 
       where keyValue.Length == 2 && keyValue[0] == key 
       select keyValue[1]).FirstOrDefault(); 
    } 

但我發現的是HtmlPage.Document.Cookies屬性總是空的。

所以我只是錯過了一些非常基本的東西,或者是否還有其他需要更改或測試的東西?

回答

0

我剛剛做了一個Silverlight 4應用程序的測試。

System.Windows.Browser.HtmlPage.Document.Cookies =「KeyName1 = KeyValue1; expires =」+ DateTime.UtcNow.AddSeconds(10).ToString(「R」); System.Windows.Browser.HtmlPage.Document.Cookies =「KeyName2 = KeyValue2; expires =」+ DateTime.UtcNow.AddSeconds(60).ToString(「R」);

每個cookie都按預期過期。

System.Windows.Browser.HtmlPage.Document.Cookies的值...

立即設定Cookie後:;

30秒後 「KeyName1 = KeyValue1 KeyName2 = KeyValue2」:「KeyName2 = KeyValue2「

60+秒後:」「