【问题标题】:IIS WCF Config: service only uses default behaviorIIS WCF 配置:服务仅使用默认行为
【发布时间】:2023-03-31 00:29:02
【问题描述】:

我正在学习 IIS 中的 WCF 部署,我发现了一些奇怪的东西。基本上,无论我如何在 web.config 中设置元素的 behaviorConfiguration 属性,我的服务都只使用默认行为。

这是我的 web.config 的相关部分:

<system.serviceModel>
<services>
  <service name="TableImport" behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="" binding="wsHttpBinding" />
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="false" />
    </behavior>
    <behavior name="MyServiceTypeBehaviors" >
      <serviceMetadata httpGetEnabled="true" policyVersion="Policy15" />
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

如您所见,默认 serviceMetadata 元素具有 httpGetEnabled="false",而 MyServiceTypeBehaviors serviceMetadata 元素具有 httpGetEnabled="true"。您还可以看到我的服务的 behaviorConfiguration 属性设置为“MyServiceTypeBehaviors”。

结果应该是我的服务发布了元数据,但是通过浏览器和 Visual Studio“添加服务引用”功能我得到了相同的结果:没有元数据。

另一方面,如果我在默认行为中启用元数据并在“MyServiceTypeBehaviors”中禁用它并继续让我的服务使用 MyServiceTypeBehaviors,那么我会通过浏览器和 VS 获取元数据。

对我来说,这些测试表明我的服务使用默认行为,无论我如何设置我的配置文件......但同时我可以通过 web.config 更改默认行为,所以我的 web.config 实际上能够影响服务的工作方式。有什么想法吗?

【问题讨论】:

  • 如果删除默认行为会发生什么?你的具体行为是否正确?
  • 如果我删除默认行为并且只有 MyServiceTypeBehaviors 和 MyServiceTypeBehaviors 启用元数据,结果是没有元数据。所以我的评估是删除默认行为并不能使我的特定行为正常工作。
  • 我会尝试删除默认的 behsvior 和策略版本。

标签: wcf iis web-config servicebehavior


【解决方案1】:

您没有在端点中指定合约,因此如果没有合约,端点就不会知道它正在使用什么服务。

如果您使用的是 .NET 4.0 或更高版本(并且根据您所描述的问题,这听起来像是),您实际上是在连接到基于服务地址的默认端点。默认端点由框架提供。

因此,它将使用默认服务行为。这符合您的问题描述:

- 当默认行为的 httpGetEnabled 设置为 false 时,您将不会获得任何元数据。 - 当默认行为的 httpGetEnabled 设置为 true 时,您将获得元数据。

在这种情况下,最简单的解决方案是简单地将合约添加到您尝试定义的端点:

<endpoint address="" binding="wsHttpBinding" contract="FullyQualified.IContractName" />

【讨论】:

  • 没有骰子...我添加了带有完全限定合同名称的合同属性作为值,测试结果与以前相同。
【解决方案2】:

您需要添加“元数据”或“MEX”端点。将配置的服务部分更改为如下所示:

     <services>
     <service name="TableImport" behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="wsHttpBinding" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
     </service>
   </services>

【讨论】:

  • 看来端点并不重要,因为我 am 能够使元数据按照问题中的描述工作。问题是我只能通过在 default 行为而不是命名行为中启用元数据来使元数据工作。无论如何,我在测试中添加了端点以查看,它没有任何区别。
  • @jeff - 您可以在没有 mex 端点的情况下公开元数据。 Mex - 它只是另一个端点。
猜你喜欢
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
相关资源
最近更新 更多