【问题标题】:Silverlight 4 HtmlPage.Document.Cookies is always emptySilverlight 4 HtmlPage.Document.Cookies 始终为空
【发布时间】:2011-03-09 14:51:16
【问题描述】:

我对 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 服务。它的网络配置如下所示:

<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 属性始终为空。

所以我只是缺少一些非常基本的东西,还是我需要更改或测试其他任何东西?

【问题讨论】:

    标签: silverlight wcf session silverlight-4.0


    【解决方案1】:

    我刚刚通过 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 多秒后:“”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 2017-07-31
      • 2019-07-18
      • 2019-08-18
      相关资源
      最近更新 更多