我们需要从另一个WCF服务调用WCF服务。为了测试这一点,我构建了一个示例控制台应用程序来显示一个简单的字符串。设置如下:
控制台应用程序-> WCF服务1-> WCF服务2
控制台应用程序调用服务1的方法,服务1的方法最终调用服务2的方法返回一个字符串。我能够调用Console-> Service 1,但Service 1-> Service 2不起作用。它抛出一个异常:
“在ServiceModel客户端配置部分中找不到引用合同'ITestService2'的默认终结点元素。这可能是因为没有找到应用程序的配置文件,或者因为在客户端元素中找不到与此合同匹配的终结点元素。”
为了实现这一点,我创建了一个具有一个返回字符串的方法的WCF服务2(没什么花头)。
注意:我使用svcutil.exe为Service2创建了一个代理。它创建了一个app.config和TestService2.cs文件,我将其复制到TestService1项目文件夹中以进行引用。
最后,我创建了一个控制台应用程序,只需创建Service1的实例并调用GetMessage()方法即可。
感谢有人能帮我解决这个问题。我也尝试在合同名称前加上命名空间,但没有起作用。不确定为什么相同的配置/代理可以直接从控制台工作,而在另一个服务中却无法工作。请帮帮我!提前感谢。
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>
感谢有人能帮我解决这个问题。我也尝试在合同名称前加上命名空间,但没有起作用。不确定为什么相同的配置/代理可以直接从控制台工作,而在另一个服务中却无法工作。请帮帮我!提前感谢。