mexHttpBinding - 在配置文件或ServiceHost中添加ServiceMetadataBehavior以启用对此契约的支持。

13

我知道这个问题已经被提出并回答了很多次,但是所有应该有效的样例在今天似乎都不能用。

当我尝试启动主机时,我一直收到以下错误:

"在服务TraceService实现的合同列表中找不到合同名称“IMetadataExchange”。请向配置文件或ServiceHost直接添加ServiceMetadataBehavior以启用对此合同的支持。"

按照微软的示例,我的服务正在托管在托管 Windows 服务主机中:http://msdn.microsoft.com/en-us/library/ms733069%28v=vs.90%29.aspx

这是我的简单配置:

  <system.serviceModel>
    <services>
      <service name="Daff.Lae.Service.TraceService">
        <endpoint address="" binding="wsHttpBinding" name="TraceService" contract="Contracts.Service.ITraceService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/TraceService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

当然,如果我删除这行代码没有出现错误时,问题就变得更加有趣了。

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
任何帮助都将不胜感激 :)
3个回答

25

请确保在配置文件的service元素中指定behaviorConfiguration,以允许使用httpGethttpsGet

我看到您已经定义了名为DefaultBehavior的服务行为-现在您所需要做的就是将behaviorConfiguration="DefaultBehavior"添加到service元素中,使该行变为:

<service name="Daff.Lae.Service.TraceService" behaviorConfiguration="DefaultBehavior">
如果您未明确指定服务的行为,HTTP GET和HTTPS GET默认情况下均被禁止,并且您的元数据将不会被公开。

@agAus - 是的,但是您没有指定您的服务要使用默认行为。您需要在配置文件中的“service”元素中添加“behaviorConfiguration”属性,如我的答案所示。 - RoccoC5
为了更清晰,我编辑了我的回答。 - RoccoC5
谢谢。已经修好了 :) - agAus

2

由于您正在使用WS-Http,因此您将绑定到HTTPS协议,因此您需要使用正确的MEX绑定;

<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" /> 

将基地址更改为https地址。

或者(相反)将您的wsHttp绑定转换为basicHttp绑定,事情就会开始为您工作。


啊...我没意识到 wsHttpBinding 表示它是 https?今天我被生产发布绑住了,但我一定想尝试并回复你。我已经将我的问题加为书签,这样我就可以相应地更新帖子。谢谢 :) - agAus

0
`<services>
  <service  name="MyService.Service1" behaviorConfiguration="Service1" >

</services>
 `

 where MyService is the application name , Service1 is the default implementation class for IService1
 `
 <protocolMapping>
  //Remove any http or https bindings provided  
</protocolMapping>   
 `
It should help when you use WCF Application Project

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接