调用调用另一个WCF服务的WCF服务时出现问题

调用调用另一个WCF服务的WCF服务时出现问题,wcf,service,call,Wcf,Service,Call,我们需要从另一个WCF服务调用WCF服务。为了测试这一点,我构建了一个示例控制台应用程序来显示一个简单的字符串。设置为: 控制台应用程序->WCF服务1->WCF服务2 控制台应用程序调用服务1的方法,服务1方法最终调用服务2方法以返回字符串。我可以调用控制台->服务1,但服务1->服务2不工作。它抛出一个异常: 在ServiceModel客户端配置节中找不到引用协定“ITestService2”的默认终结点元素。这可能是因为找不到应用程序的配置文件,或者在客户端元素中找不到与此协定匹配的终结点

我们需要从另一个WCF服务调用WCF服务。为了测试这一点,我构建了一个示例控制台应用程序来显示一个简单的字符串。设置为: 控制台应用程序->WCF服务1->WCF服务2 控制台应用程序调用服务1的方法,服务1方法最终调用服务2方法以返回字符串。我可以调用控制台->服务1,但服务1->服务2不工作。它抛出一个异常: 在ServiceModel客户端配置节中找不到引用协定“ITestService2”的默认终结点元素。这可能是因为找不到应用程序的配置文件,或者在客户端元素中找不到与此协定匹配的终结点元素 为了实现这一点,我创建了一个WCF服务2,其中包含一个返回字符串的方法(没有什么特别之处)

然后创建service1-ITestService1.cs和TestService1.cs,它们使用service2方法GetSomething()

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

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

当我直接从控制台应用程序调用Service2时,它可以正常工作。在服务1中使用复制时使用相同的配置和代理类。它抛出错误。配置文件如下所示: 控制台应用程序中服务1的配置文件:

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

service1文件夹中服务2的配置文件:

<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服务。我猜您在dll中定义了一个wcf service1,控制台应用程序加载并尝试调用它。我认为您的问题可能是sice服务1位于dll中,而不是加载您定义了指向服务2的链接的配置文件。尝试通过编程创建端点,看看这是否能彻底解决问题

首先,我想让你们知道我对WCF非常陌生,所以我的理解会受到限制,因为我还在阅读和学习。实际上,我在IIS上本地托管服务1和服务2。然后,我为这两个类以及配置文件(app.config)创建代理类。这些是在scvutil.exe的帮助下自动创建的。然后,我只需将service1.cs(proxy)和app.config复制到console应用程序项目中。现在在代码中,我可以通过实例化service1类来访问service1方法。我没有在conifg中明确定义这两个服务之间的任何链接。这可能是个问题。我只是在service1中创建了一个引用service2方法的实例。有没有在配置中创建链接和programmatcaly创建端点的示例?我认为您必须在这两个服务上启用跟踪,并使用tracutil工具查看发生了什么。重新运行,我能够按照您的建议解决此问题。我创建了endpoint程序Maticaly,它成功了。但是没有别的办法吗?我会仔细检查你的代码,确保你可以解析配置文件中的设置。听起来你好像因为某种原因没有得到这些设置
static void Main(string[] args)
{
    TestService1 client = new TestService1();
    Console.WriteLine(client.GetMessage("Roger Harper"));
    Console.ReadKey();
}
<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>
<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>