我正在学习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",而myservicemetadata元素有httpGetEnabled="true"。你还可以看到我的服务的behaviorConfiguration属性被设置为"MyServiceTypeBehaviors"。
结果应该是我的服务发布元数据,但是通过浏览器和Visual Studio的"添加服务引用"功能,我得到了相同的结果:没有元数据。
另一方面,如果我在默认行为中启用元数据并在"MyServiceTypeBehaviors"中禁用它并继续让我的服务使用MyServiceTypeBehaviors然后我通过浏览器和vs获得元数据
对我来说,这些测试表明,无论我如何设置配置文件,我的服务都使用默认行为…但同时我可以通过web 改变的默认行为。配置我的网页。配置实际上能够影响服务的工作方式。什么好主意吗?
你没有在你的端点中指定合约,所以如果没有合约,端点将不知道它正在使用什么服务。
如果你使用的是。net 4.0或更高版本(根据你所描述的问题听起来像是),你实际上是在连接到一个基于服务地址的默认端点。默认端点由框架提供。
因此,它将使用默认的服务行为。这符合您的问题描述: <>之前-当默认行为的httpGetEnabled设置为false时,您不会获得元数据。-当默认行为的httpGetEnabled设置为true时,您将获得元数据。之前在这种情况下,最简单的解决方案是简单地将契约添加到您试图定义的端点:
<endpoint address="" binding="wsHttpBinding" contract="FullyQualified.IContractName" />
您需要添加"元数据"或"MEX"端点。将配置中的services部分修改为:
<services>
<service name="TableImport" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="wsHttpBinding" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>