Silverlight 4 HtmlPage.Document.Cookies始终为空

时间:2011-03-09 14:51:17

标签: silverlight wcf session silverlight-4.0

我对silverlight非常陌生,所以请假设我做了一件非常糟糕的事情......

我正在尝试从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工具添加了对我的silverlight应用程序的服务引用,并使用了默认值(命名空间除外)

最初我只是试图像这样调用服务:

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

proxy.RequestExportCompleteAsync();

但是服务中的会话总是空的(不是空,只是空),所以我尝试手动将会话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属性始终为空。

所以我只是遗漏了一些非常基本的东西,还是还有其他需要改变或测试的东西?

1 个答案:

答案 0 :(得分: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后立即:“KeyName1 = KeyValue1; KeyName2 = KeyValue2”

30秒后:“KeyName2 = KeyValue2”

60多秒后:“”