【发布时间】:2014-12-23 06:09:18
【问题描述】:
由于害怕提出一个在 Stack Overflow 上已经饱和的问题,我一直在浏览可能的解决方案,但对于如何实现我想要实现的目标(如果确实有可能,或者我是否有)一无所知我的系统设计不正确。
这个问题不仅仅是寻找解决方案,而是解释 WCF 在配置/自定义不同端点时的一些复杂性(和误解)。
典型场景:一个基于网络的结账系统,客户可以通过向服务器调用 javascript 来更改其购物篮中产品的数量或大小等属性。服务器必须知道用户是谁,所以有一个可用的会话似乎是一个理想的解决方案。
WCF 显然是可行的方法,因为这种方案可以在未来扩展以支持另一个端点,例如移动应用程序或其他服务。 (超出了我的问题范围,但要验证我是否想针对旧版 .Net Web 服务使用 WCF)
我上面的问题是配置端点/绑定。
- webHttpBinding – 用于使 web 脚本可以访问服务(即来自网页的 Javascript)。但是它不支持会话
- wsHttpBinding - 这支持会话但不支持 Web 脚本。
我玩过各种配置。似乎我需要上述绑定的组合,或者可能创建一个支持这些元素的自定义绑定?如果是这样,是否有任何关于如何做到这一点的好资源?我试过创建自定义绑定,但失败得很惨!
我已经阅读了有关其他问题的各种 cmets,这些问题建议您不应该通过 Web 脚本使用 Sessions、WCF 不支持它,或者正在实施的系统设计不正确。首先,WebServices 支持这一点,所以我很难相信 WCF 不支持,特别是因为它单独支持 web 脚本和会话(但不是一起支持?开箱即用可能......)。如果我要使用会话以外的东西,它必须是一个基于令牌的系统,以便可以识别用户,但肯定这实际上是会话吗?我可以建造这个,但它似乎是在重新创造轮子。
这是我的 Web.Config 中的配置。我已经设置了 3 个端点来玩;一个基本的,一个支持的会话和其他支持的网页脚本。 (webHttpBinding 端点的地址是空白的,因为服务在调试模式下会返回错误——我也看到过很多人这样说)
<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() 返回当前会话 ID,如果会话不可用,则返回 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();
}
所以我的冗长问题是这样的.. 我可以配置一个端点以启用 webscript,以便我可以通过 javascript 访问服务,同时还启用会话?我已经通过 WCF 测试客户端单独测试了端点,以查看“ws”端点确实有会话,并且使用 webHttpBinding 的空白端点没有会话但可以从 Javascript 调用。
我知道可能没有“开箱即用”的绑定,所以我需要创建一个自定义绑定,还是我可以通过使用一些配置魔法以某种方式改变上述两个端点?
【问题讨论】:
标签: c# asp.net web-services wcf session