【问题标题】:Asp.net session in wcf via channel factory in silverlight通过 Silverlight 中的通道工厂在 wcf 中的 Asp.net 会话
【发布时间】:2010-12-30 22:46:03
【问题描述】:

我的问题是 WCF 不会在 Silverlight 调用之间保持会话。每个调用都是不同的 asp.net 会话。我知道它可以工作,因为我有一个在 Silverlight 中使用自动生成的 WCF 代理的示例,但我使用的是 Channel Factory。我在网上搜索过,但遗憾的是大多数人似乎都使用代理生成器。

我的 web.config

<bindings>
  <basicHttpBinding>
    <binding name="databaseServiceBasicHttp" allowCookies="true" />
  </basicHttpBinding>
</bindings>

<services>
  <service name="databaseService"
           behaviorConfiguration="Debug">
    <endpoint address=""
      binding="basicHttpBinding" bindingConfiguration="databaseServiceBasicHttp" contract="BlueGazelle.DatabaseServiceContracts.IDatabaseService" />
  </service>

</services>

Silverlight 绑定配置

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding >
        <binding name="databaseService" enableHttpCookieContainer="true"/>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:53392/Services/DatabaseService.svc"
                binding="basicHttpBinding" bindingConfiguration="databaseService" contract="BlueGazelle.DatabaseServiceContracts.IDatabaseService"
                name="BlueGazelle.DatabaseService.Code.DatabaseService"/>
    </client>
  </system.serviceModel>
</configuration>

我如何创建频道

var service = new ChannelFactory<IDatabaseService>("BlueGazelle.DatabaseService.Code.DatabaseService").CreateChannel();

ChannelFactory 有什么问题吗?我应该启用一些东西让它携带cookie吗?

【问题讨论】:

    标签: .net asp.net silverlight wcf


    【解决方案1】:

    basicHttpBinding 绑定不提供任何 WCF 会话功能。但是您仍然可以让 ASP.NET 会话处理稍加小心。检查您是否已完成以下操作...

    1、在你的服务接口中添加以下属性...

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    

    2,在您的 web.config 中添加以下内容...

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    

    3,在您的 web.config 中添加以下内容...

    <sessionState mode="InProc" cookieless="false" regenerateExpiredSessionId="true" timeout="20"/>
    

    Silverlight 与使用上述 cookie 方法传递会话信息的 ASP.NET 会话一起工作没有问题。让它与无 cookie 会话一起工作会很棘手,因为它需要将信息作为 URL 的一部分传回。

    有关更多信息,我发现了这个blog entry

    【讨论】:

      【解决方案2】:

      感谢您的回复,Phil,但我已经完成了您之前提到的事情。这当然是需要的,但对我来说这不是问题。

      我需要向我生成的代理添加一个 cookie 容器。我在一个网页上找到了答案,但现在找不到。

      如果有人好奇,这里就是代码。

       var service = new ChannelFactory<IDatabaseService>("BlueGazelle.DatabaseService.Code.DatabaseService").CreateChannel();
              var cookieManager = ((IChannel)service).GetProperty<IHttpCookieContainerManager>();
              if (cookieManager.CookieContainer == null)
              {
                  cookieManager.CookieContainer = new CookieContainer();
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-12
        • 1970-01-01
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多