【问题标题】:Adding service/endpoint behaviors to multiple services through config通过 config 向多个服务添加服务/端点行为
【发布时间】:2015-08-20 13:54:49
【问题描述】:

在我的项目中,公开了多个 WCF 服务。我想为所有这些服务添加服务和端点行为。 我已经创建了这些行为,如下所示。

<behaviors>
      <endpointBehaviors>
        <behavior name="RequestInspectorBehavior">
          <RequestInspectorBehaviorExtension />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="webby">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <RequestInspectorBehaviorExtension />
        </behavior>
      </serviceBehaviors>
</behaviors>

现在一种选择是,我可以将这些添加到我的个人服务中,如下所示。

<service name="Service1" behaviorConfiguration="webby">
<endpoint address="" behaviorConfiguration="RequestInspectorBehavior" binding="webHttpBinding" contract="CONTRACT_GOES_HERE" />

但我想知道的是,有没有一种方法可以为我的所有服务提供此服务和端点行为,而无需通过每个服务的名称显式添加这些。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    使用 WCF 4.0+,您可以通过在配置的相应行为部分中省略 name 属性来将行为设置为所有服务和/或端点的默认值。绑定也是如此。

    在您的情况下,以下应该有效:

    <behaviors>
      <endpointBehaviors>
        <behavior>
          <RequestInspectorBehaviorExtension />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <RequestInspectorBehaviorExtension />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    

    由于端点和服务行为都没有name 属性,它们将被用作所有服务和端点行为的默认值。

    所以您的服务和端点定义如下所示:

    <service name="Service1">
      <endpoint address="" 
                binding="webHttpBinding" 
                contract="CONTRACT_GOES_HERE" />
    

    如果您的服务或端点需要您定义的默认值以外的其他内容,您只需添加定义,然后在服务或端点元素的相应属性中引用它们。

    默认端点、绑定和行为非常强大和有用(尽管它们也引起了他们的麻烦)。欲了解更多信息,您可以查看A Developer's Introduction To Windows Communication Foundation 4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多