WCF,找不到合同名称"IMetadataExchange"

Ome*_*r K 4 c# asp.net iis wcf

我只是应用这篇文章的代码,

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
Run Code Online (Sandbox Code Playgroud)

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

  <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
Run Code Online (Sandbox Code Playgroud)

但我不想添加这个,因为在查看链接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>
Run Code Online (Sandbox Code Playgroud)

小智 5

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

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
Run Code Online (Sandbox Code Playgroud)

最后,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>
Run Code Online (Sandbox Code Playgroud)