调用WCF服务调用另一个WCF服务的问题

问题描述:

我们需要从另一个WCF服务调用WCF服务。为了测试这个,我构建了一个示例控制台应用程序来显示简单的字符串设置为: 控制台应用程序 - > WCF服务1 - > WCF服务2 控制台应用程序调用服务1的方法,服务1方法最终调用服务2方法返回字符串。我可以调用控制台 - >服务1,但服务1 - >服务2不起作用。它会抛出一个异常: “在ServiceModel客户端配置部分找不到引用合同'ITestService2'的默认端点元素,这可能是因为没有为您的应用程序找到配置文件,或者因为找不到匹配此合同的端点元素在客户端元素中“。 为了实现这一点,我创建了一个WCF服务2,其中一个方法返回一个字符串(没有什么奇怪的)。调用WCF服务调用另一个WCF服务的问题

namespace TestServices 
{ 
    [ServiceContract] 
    public interface ITestService2 
    { 
     [OperationContract] 
     string GetSomething(string s); 
    } 
} 

然后,我创建服务1 - ITestService1.cs和TestService1.cs消耗服务2方法GetSomething()。

namespace TestServices 
{ 
    [ServiceContract] 
    public interface ITestService1 
    { 
     [OperationContract] 
     string GetMessage(string s); 
    }   
} 

namespace TestServices 
{ 
    class TestService1 : ITestService1 
    { 
     public string GetMessage(string s) 
     { 
      TestService2 client = new TestService2(); 
      return client.GetSomething("WELCOME " + s); 
     } 
    } 
} 

注意:我使用svcutil.exe为Service2创建代理。它创建一个app.config和TestService2.cs文件,我将其复制到TestService1项目文件夹中以供参考。

最后,我创建了一个控制台应用程序,它只创建Service1的一个实例并调用GetMessage()方法。

static void Main(string[] args) 
{ 
    TestService1 client = new TestService1(); 
    Console.WriteLine(client.GetMessage("Roger Harper")); 
    Console.ReadKey(); 
} 

当我直接从控制台应用程序调用服务2时,它没有任何问题。在服务中复制时使用相同的配置和代理类1.它引发错误。该配置文件是这样的:在服务1文件夹服务2

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="WSHttpBinding_ITestService1" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" establishSecurityContext="true" /> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:3227/WCFTestSite/TestService1.svc" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITestService1" 
       contract="ITestService1" name="WSHttpBinding_ITestService1"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
     </client> 
    </system.serviceModel> 
</configuration> 

配置文件:服务1控制台应用程序 配置文件

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="WSHttpBinding_ITestService2" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" establishSecurityContext="true" /> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:3227/WCFTestSite/TestService2.svc" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISriWCFTestService2" 
       contract="ITestService2" name="WSHttpBinding_ITestService2"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
     </client> 
    </system.serviceModel> 
</configuration> 

感激,如果有人能帮助我解决这个问题。我也尝试在命名空间前加上合同名称,但它没有奏效。不知道相同的配置/代理如何直接从控制台,而不是在另一个服务。请帮忙!!!提前致谢。

从我的低估你有一个控制台应用程序是自我托管一个wcf服务,该服务正在调用第二个wcf服务。我猜你有一个wcf service1定义在控制台应用程序加载,然后尝试调用dll。我认为你的问题可能是sice服务1在一个dll中,它没有加载配置文件,你已经定义了到服务2的链接。尝试以编程方式创建端点,看看是否让你彻底解决了这个问题。

+0

起初,我想让你知道我对WCF很新,所以我的理解会受到限制,因为我仍在阅读和学习。我实际上在IIS上本地托管服务1和服务2。然后,我创建代理类以及配置文件(app.config)。这些是在scvutil.exe的帮助下自动创建的。然后,我只需将service1.cs(代理)和app.config复制到控制台应用程序项目中即可。现在在代码中,我可以通过实例化service1类来访问service1方法。我没有在conifg中明确定义两个服务之间的任何链接。可能是这个问题。 – 2010-10-04 15:15:15

+0

我只是在service1中创建一个实例来引用service2方法。有没有一个例子可以在config中创建链接,并通过programmatcaly创建端点? – 2010-10-04 15:15:52

+0

我认为你将不得不在这两个服务上启用跟踪并使用tracutil工具来查看发生了什么。 – rerun 2010-10-04 15:28:59