WCF服务与另一个WCF服务通信

时间:2014-03-21 12:14:06

标签: c# .net web-services wcf visual-studio

我创建了一个新的测试WCF应用程序。当我在Visual Studio中运行解决方案时,使用WCF测试客户端的Simple GetData方法可以正常运行。但是,我现在想要向外部WCF服务添加服务引用,以便我可以从此Web服务与其进行通信。所以我的Service1界面现在看起来像:

public interface IService1 : MyExternalService

然后在我的服务类中,我有简单的GetData,我可以点击实现接口,我看到创建了外部Web服务的所有方法:

     public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }

            public string ExternalMethod1(string a, string b)
            {
                throw new NotImplementedException();
            }

//etc 

但是,如果我尝试使用WCF测试客户端运行此操作 - 我收到以下错误消息 - Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.

在我加入外部服务参考后,它将我的web.config更新为如下(注意一些实际的网址已擦除)

<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IExternalService">
          <security>
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
        <binding name="WSHttpBinding_IExternalService1">
          <security>
            <message clientCredentialType="Certificate" negotiateServiceCredential="false"
              algorithmSuite="Basic128" establishSecurityContext="false" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://service.com/ExternalService/ExternalService.svc"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IExternalService"
        contract="ExternalService.IExternalService" name="WSHttpBinding_IExternalService">
        <identity>
          <dns value="service.com" />
        </identity>
      </endpoint>
      <endpoint address="http://service.com/ExternalService/ExternalService.svc/Java"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IExternalService1"
        contract="ExternalService.IExternalService" name="WSHttpBinding_IExternalService1">
        <identity>
          <dns value="service.com" />
        </identity>
      </endpoint>
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

1 个答案:

答案 0 :(得分:1)

您需要将元数据交换端点添加到第一个服务

<services>
   <service name="MyService.MyService" behaviorConfiguration="metadataBehavior">
      <endpoint 
          address="http://localhost/MyService.svc" 
          binding="customBinding" bindingConfiguration="jsonpBinding" 
          behaviorConfiguration="MyService.MyService"
          contract="MyService.IMyService"/>
      <endpoint 
          address="mex" 
          binding="mexHttpBinding" 
          contract="IMetadataExchange"/>
   </service>
</services>

这个主题似乎讨论了同样的问题WCF Test Client : Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata