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

本文关键字:WCF 服务 通信 另一个 | 更新日期: 2023-09-27 17:51:21

我创建了一个新的测试WCF应用程序。当我在Visual Studio中运行解决方案时,它与WCF测试客户端一起使用简单的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.

在我包含外部服务Ref后,它更新了我的web。如下所示配置to(注意一些实际的url被删除了)

<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>

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

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

<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测试客户端:添加服务失败。服务元数据可能无法访问。确保您的服务正在运行并公开元数据