c# - 配置 WCF 端点以启用 WebScript 和 session

标签 c# asp.net web-services wcf session

由于担心提出一个已经在 Stack Overflow 上饱和的问题,我一直在滚动浏览可能的解决方案,但对于如何实现我想要实现的目标(如果确实可能)或我是否有更明智的了解我的系统设计不正确。

这个问题不仅仅是寻找解决方案,而是解释 WCF 在配置/自定义不同端点时的一些复杂性(和误解)。

典型场景:基于网络的结帐系统,客户可以通过向服务器发出 JavaScript 调用来更改购物篮中产品的数量或尺寸等属性。服务器必须知道用户是谁,因此拥有可用的 session 似乎是一个理想的解决方案。

WCF 显然是可行的方法,因为这种方案可以在未来扩展以支持另一个端点,例如移动应用程序或其他服务。 (超出了我的问题范围,但验证我是否想针对旧版 .Net Web 服务使用 WCF)

我的上述问题是配置端点/绑定(bind)。

  • webHttpBinding – 用于 Web 脚本可以访问服务(即网页中的 Javascript)。但是它不支持 session
  • wsHttpBinding – 这支持 session ,但不支持 Web 脚本。

我尝试过各种配置。看来我需要上述绑定(bind)的组合,或者可能创建一个支持这些元素的自定义绑定(bind)?如果是这样,有什么关于如何做到这一点的好资源吗?我尝试过创建自定义绑定(bind),但惨败!

我读过关于其他问题的各种评论,这些评论建议您不应通过 Web 脚本使用 session 、WCF 不支持它,或者正在实现的系统设计不正确。首先,WebServices 支持这一点,所以我很难相信 WCF 不支持这一点,特别是因为它单独支持 Web 脚本和 session (但不能同时支持?可能是开箱即用的……)。如果我要使用 session 以外的其他东西,它必须是基于 token 的系统,以便可以识别用户,但这实际上就是 session 吗?我可以构建这个,但这看起来像是重新造轮子。

这是我的 Web.Config 中的配置。我设置了 3 个端点来使用;一个是基本的,一个是支持 session ,另一个是支持网页脚本。 (webHttpBinding 端点的地址为空,因为该服务在 Debug模式下返回错误 - 我也见过几个人这么说)

<system.serviceModel>
     <behaviors>
       <endpointBehaviors>
        <behavior name="CustomerServiceAspNetAjaxBehavior">
          <enableWebScript />
          <!--<webHttp />-->
        </behavior>
        <behavior name="WebScript">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service behaviorConfiguration="serviceBehavior" name="CustomerService">
        <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="basicBinding"
          contract="CustomerService" />
        <endpoint address="ws" binding="wsHttpBinding" bindingConfiguration="wsConfig"
          contract="CustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" behaviorConfiguration="CustomerServiceAspNetAjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="webConfig" contract="CustomerService" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="basicBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="None"></security>
          <readerQuotas maxStringContentLength="2147483647  "/>
        </binding>
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="wsConfig" transactionFlow="true">
          <security mode="None" />
          <reliableSession enabled="true" ordered="true"/>
        </binding>
      </wsHttpBinding>
      <webHttpBinding>
        <binding name="webConfig">
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <client/>
  </system.serviceModel>

这是我的服务界面,显示我已将 SessionMode 设置为“Allow”,并且 GetSession() 返回当前 session id,如果 session 不可用,则返回 null。

using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

[ServiceContract(Namespace = "", ConfigurationName = "CustomerService", SessionMode = SessionMode.Allowed)]
public interface ICustomerService
{
  [OperationContract()]
  [WebGet()]
  bool UpdateBasketItem(int index, int productId, int qty, int attribSize);

  [OperationContract()]
  [WebGet()]
  string GetSession();
}

所以我冗长的问题是这样的.. 我可以配置端点以启用网页脚本,以便我可以通过 JavaScript 访问服务,并同时启用 session 吗?我已通过 WCF 测试客户端单独测试了端点,以查看“ws”端点确实有 session ,并且使用 webHttpBinding 的空白端点没有 session ,但可以从 Javascript 调用。

我知道可能没有“开箱即用”的绑定(bind),所以我是否需要创建一个自定义绑定(bind),或者我可以通过使用一些配置魔法以某种方式改变上述两个端点吗?

最佳答案

如果您正在寻找一种可以通过 JavaScript 轻松访问的服务技术,我会强烈考虑使用 Asp.Net WEB API。这是创建 Web api 的一个很棒的框架。

从客户端角度来看,它比 wcf 更易于访问,并且您可以利用标准的 ASP.NET 概念。 Cookie、 session 、缓存等

我相信 Web API 的创建甚至是为了解决在 WCF 中执行此操作的困难。

如果您需要 SOAP 支持,我只会考虑 wcf。

关于c# - 配置 WCF 端点以启用 WebScript 和 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26598147/

相关文章:

c# - Windows Phone 8 slider 绑定(bind)仅在单击后起作用

c# - 反序列化 XML 文件而不将其全部加载到内存中

javascript - 用户控件内的 RegisterStartupScript

c# - 每次启动 ASP.NET Core MVC 应用程序时出现 IIS 错误

c# - 我有一个为 ViewState 赋值的控件。它有效,但 ViewState 被禁用。如何?

java - soap 对象应该使用构造函数还是 setter 来创建?

c# - 在 ajax-pro 中,我可以使用自定义类作为代理而不是 system.web.ui.page 吗?如何?

java - Jersey/JAX-RS 2 AsyncResponse - 如何跟踪当前的长轮询调用者

java - 从 JAVA 7 迁移到 8 时,JAX WS 的 DII(例如 Dispatch)是否会受到影响

c# - 在 NUnit 测试中使用 WPF 组件 - 如何使用 STA?