[英]WCF: The contract 'X' in client configuration does not match the name in service contract
[英]Create WCF service client with specified address without specifying configuration name
有没有办法在C#中使用指定的端点地址创建WCF服务客户端的实例而不指定配置名称?
默认情况下,客户端具有以下构造函数:
public ServiceClient()
public ServiceClient(string endpointConfigurationName)
public ServiceClient(string endpointConfigurationName, string remoteAddress)
显然,由于第一个构造函数,有一个默认配置。 我想要的是只指定最终构造函数的第二个参数。 现在,我正在努力阅读使用ConfigurationManager来解决它的配置元素,但它看起来非常麻烦。 有更干净的方式吗?
我不想在.config文件中使用端点配置。 我通常做这样的事情:
BasicHttpBinding basicbinding = new BasicHttpBinding();
basicbinding.SendTimeout = TIMEOUT;
basicbinding.OpenTimeout = TIMEOUT;
ServiceClient client = new ServiceClient(basicbinding, new EndpointAddress(new Uri("http://xxxxx")));
您生成的客户端也应该有一个如下所示的构造函数:
public ServiceClient(
System.ServiceModel.Channels.Binding binding,
System.ServiceModel.EndpointAddress remoteAddress)
: base(binding, remoteAddress) {
}
您可以在没有端点配置的情况下调用此方法。
如果你想要实际上只是想要调用服务而不必了解有关WCF服务和配置处理的所有信息,那么在C#中你可以做...
String url = "http:\\somehost:someport\\pathToSomeService";
EndpointAddress address = new EndpointAddress(url);
Binding binding = new BasicHttpBinding();
YourClient client = new YourClient(binding, address);
// Call your client methods
client.SomeMethod(parm1, parm2);
以上假设您生成了服务引用,并且不要求配置信息存在于任何位置,不存在于生成的服务引用中,不存在于DLL中,也不存在于可执行文件中。 没有配置。 没有。
我在一个真正的独立服务代理DLL中使用上面的代码。 它是最独立的,因为它完全可配置,不依赖于调用可执行文件来提供任何东西。
好吧,您可以使用默认构造函数,但是您必须在所有配置设置中手动编程。 通过指定配置名称,服务客户端将自动从.config文件加载配置,您需要知道的是要使用的配置(您可以有多个,例如一个用于HTTP而另一个用于Net.Tcp)。 当然,remoteAddress只是告诉WCF在哪里建立连接。
如果您自己配置客户端设置时遇到问题,请确保使用WCF服务配置工具。 它适用于服务配置和客户端配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.