WCF 服务未连接到测试客户端

WCF Service is not connecting to test client

当我 运行 测试客户端

时,我不断收到此错误

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.

Cannot obtain Metadata from http://localhost:50507/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:50507/Service1.svc Metadata contains a reference that cannot be resolved: 'http://localhost:50507/Service1.svc'. The requested service, 'http://localhost:50507/Service1.svc' could not be activated. See the server's diagnostic trace logs for more information.HTTP GET Error URI: http://localhost:50507/Service1.svc There was an error downloading 'http://localhost:50507/Service1.svc'. The request failed with the error message:-- The type 'AgeCalculator.Service1', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

我已经在 web.config 文件

中使用以下代码公开了服务的元数据
<system.serviceModel>
<services>
  <service name="AgeCalculator.CalculateAge" behaviorConfiguration="MetadataBehavior">
    <endpoint address=""
              binding="basicHttpBinding" contract="AgeCalculator.IService1">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    <endpoint address="http://localhost/Service1.svc" binding="basicHttpBinding" contract="AgeCalculator.IService1"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MetadataBehavior">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
    <add binding="basicHttpsBinding" scheme="http" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

我查看了所有当前文档,但不知道问题出在哪里。可能是命名约定,因为我是 WCF 的新手,但没有看到这一点。我已经搜索了互联网并公开了元数据文章以应用修复程序,但迄今为止没有任何效果

由于您似乎没有使用默认端口,因此最好在 service 绑定中指定端口。像这样更改您的 service 定义:

<services>
  <service name="AgeCalculator.CalculateAge" behaviorConfiguration="MetadataBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:50507/" />
      </baseAddresses>
    </host>
    <endpoint address="" 
        binding="basicHttpBinding" contract="AgeCalculator.IService1" />
    <endpoint address="mex" 
        binding="mexHttpBinding" contract="IMetadataExchange"/>
    <endpoint address="http://localhost:50507/Service1.svc" 
        binding="basicHttpBinding" contract="AgeCalculator.IService1"/>
  </service>
</services>