WCF,找不到合同名称“IMetadataExchange”

时间:2014-07-21 21:12:08

标签: c# asp.net wcf iis

我只是应用本文中的代码,

http://msdn.microsoft.com/en-us/library/ms733766(v=vs.110).aspx

我没有改变任何东西,但是从IIS浏览后我得到了

The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service CalculatorService.  Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract

可能有什么问题,我只是作为链接。我找了一个答案。我可以通过添加

来处理
  <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

但是我不想添加这个,因为在查看链接msdn时不会添加。错误是什么?

这是我的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Microsoft.ServiceModel.Samples.CalculatorService">

        <!-- This endpoint is exposed at the base address provided by host:                                        http://localhost/servicemodelsamples/service.svc  -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />

        <!-- The mex endpoint is explosed at http://localhost/servicemodelsamples/service.svc/mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

</configuration>

3 个答案:

答案 0 :(得分:5)

要自行学习,请尝试删除以下声明,然后再次检查您的服务。

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

最后,Web.config文件将如下所示完成它。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <!-- This section is optional with the default configuration
        model introduced in .NET Framework 4 -->
      <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="MyServiceTypeBehaviors">

        <!-- This endpoint is exposed at the base address provided by host:                                        http://localhost/servicemodelsamples/service.svc  -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />

        <!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
            <serviceBehaviors>
                <behavior name="MyServiceTypeBehaviors" >
                    <!-- Add the following element to your service behavior configuration. -->
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
  </system.serviceModel>

</configuration>

答案 1 :(得分:2)

我无法找到任何正式文件,说明<serviceMetadata>IMetadataExchange之间的关系。

<serviceMetadata>示例中的这条评论:

<!-- the mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex 
To expose the IMetadataExchange contract, you 
must enable the serviceMetadata behavior as demonstrated below -->

它说&#34;你必须&#34;,但还没有参考。

答案 2 :(得分:1)

您需要公开元数据端点。

这是端点配置的样子:

<endpoint name="MyServiceMex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

这是一个稍微完整的示例,显示了服务配置的外观:

<services>
  <service name="MyProject.Services.MyService">        
    <endpoint name="MyServiceEndpoint"
              contract="MyProject.Contracts.IMyService"
              binding="basicHttpBinding" />
<!-- Expose your Metadata (MEX) endpoint too -->            
<endpoint name="MyServiceMex"
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange" />
  </service>
</services>