WCF单一服务实现,多种行为.可能的

  • 本文关键字:单一 服务 实现 WCF wcf
  • 更新时间 :
  • 英文 :


我有一个IIS7托管服务,需要向两个不同的客户端公开。对于其中一个客户,我需要执行比另一个更严格的节流行为。

这意味着我需要定义两个标签,因为这些标签只能从一个标签中引用,所以我也需要其中两个?

我已经定义了以下web.config。问题是,当我试图浏览到任一服务以便提取元数据时,我会得到以下错误:

Parser Error Message: A child element named 'service' with same key already exists at the same configuration scope.
Collection elements must be unique within the same configuration scope (e.g. the same application.config file). Duplicate key value:  'WCFTwoEndpoints.Calculate'.

我这样做对吗?

<system.serviceModel>
    <services>
      <service name="WCFTwoEndpoints.Calculate" behaviorConfiguration ="NotThrottled">
        <endpoint address="http://localhost/WCFTwoEndpoints/Calculate.svc"
          binding="wsHttpBinding" bindingConfiguration="" name="Calculator"
          contract="WCFTwoEndpoints.ICalculate" />
        <endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      </service>
      <service name="WCFTwoEndpoints.Calculate" behaviorConfiguration ="Throttled">
        <endpoint address="http://localhost/WCFTwoEndpoints/ThrottledCalculate.svc"
          binding="wsHttpBinding" bindingConfiguration="" name="ThrottledCalculator"
          contract="WCFTwoEndpoints.ICalculate" />
        <endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="NotThrottled">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="Throttled">
          <serviceMetadata httpGetEnabled="true" />
          <serviceThrottling maxConcurrentCalls="19" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

您有点卡住了,因为键的名称必须与服务类的名称完全匹配。

我能想到的唯一方法就是创建一个继承自WCFTwoEndpoints.Calculate的新类。那么你就有了一个不同的名字。不过不太好。

 

我想,如果你问WCF设计师为什么要这样设计,他们会说服务应该是独立于客户端的。你想要的并不是一项服务;但是两个不同的独立服务恰好有一些共同的实现。从客户的角度来看,他们的行为不会像一个单一的服务。

最新更新