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

3
我们需要从另一个WCF服务调用WCF服务。为了测试这一点,我构建了一个示例控制台应用程序来显示一个简单的字符串。设置如下: 控制台应用程序-> WCF服务1-> WCF服务2 控制台应用程序调用服务1的方法,服务1的方法最终调用服务2的方法返回一个字符串。我能够调用Console-> Service 1,但Service 1-> Service 2不起作用。它抛出一个异常: “在ServiceModel客户端配置部分中找不到引用合同'ITestService2'的默认终结点元素。这可能是因为没有找到应用程序的配置文件,或者因为在客户端元素中找不到与此合同匹配的终结点元素。” 为了实现这一点,我创建了一个具有一个返回字符串的方法的WCF服务2(没什么花头)。
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在控制台应用程序中的配置文件:

<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文件夹中服务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>

感谢有人能帮我解决这个问题。我也尝试在合同名称前加上命名空间,但没有起作用。不确定为什么相同的配置/代理可以直接从控制台工作,而在另一个服务中却无法工作。请帮帮我!提前感谢。
1个回答

1
据我理解,您有一个控制台应用程序,它自主托管了一个WCF服务,该服务调用了第二个WCF服务。我猜您在DLL中定义了WCF Service1,控制台应用程序加载并尝试调用它。我认为您的问题可能是由于Service 1在DLL中,因此它没有加载配置文件,在其中定义了到Service 2的链接。尝试以编程方式创建终结点,看看是否可以解决问题。

首先,我想让您知道我对WCF非常不熟悉,因此我的理解会比较有限,因为我仍在阅读和学习中。实际上,我将服务1和服务2本地托管在IIS上。然后,我使用scvutil.exe自动创建了两者的代理类以及配置文件(app.config)。然后,我只需将service1.cs(代理)和app.config复制到控制台应用程序项目中。现在,在代码中,我可以通过实例化service1类来访问service1方法。我没有在config中明确定义任何两个服务之间的链接。也许这是一个问题。 - Sri Reddy
我刚刚在service1中创建了一个实例来引用service2的方法。有没有示例可以在配置文件中创建链接并编程方式创建端点? - Sri Reddy
重新运行,根据您的建议,我已经解决了这个问题。我通过编程方式创建了端点并且它起作用了。但是没有其他方法可以做到吗? - Sri Reddy
我会逐步检查您的代码,并确保您可以解析配置文件中的设置,因为听起来您由于某种原因无法获取它们。 - rerun
哎呀..当然我很乐意做这件事 :) 谢谢你的帮助。 - Sri Reddy
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接