繁体   English   中英

WCF服务与另一个WCF服务进行对话

[英]WCF Service to talk to another WCF Service

我创建了一个新的Test WCF Application。 当我在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. Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.

在添加外部服务引用后,它会将我的web.config更新为如下所示(请注意清除了一些实际的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>

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM