[英]Having WCF service proxy configurable
I am writing a basic WPF GUI to connect to a WCF service and consume an interface. 我正在编写一个基本的WPF GUI,以连接到WCF服务并使用一个接口。 So far I have connected to the test system by creating a service reference, putting in the URI for the test service I want to consume, it finds the interface and creates the proxy via service reference for me. 到目前为止,我已经通过创建服务引用连接到测试系统,并输入要使用的测试服务的URI,它找到接口并通过服务引用为我创建代理。
What I want this to do when you run the GUI app is for the user to be able to pick an environment - development, test or production and for the GUI to then connect to the appropriate WCF service depending on the environment selected. 运行GUI应用程序时,我希望用户可以选择一个环境-开发,测试或生产,然后GUI可以根据所选环境连接到适当的WCF服务。
How can I do this? 我怎样才能做到这一点?
You can overwrite the Endpoint
like this: 您可以像这样覆盖Endpoint
:
client.Endpoint.Address = new EndpointAddress(GetAddressForCurrentMode())
The other way you could to it, is to write a method, maybe an extension method, that accepts the service contract and the implementation class. 您可以使用的另一种方法是编写一种方法,可能是扩展方法,该方法接受服务协定和实现类。 Further more it either accepts a configuration name, or an endpoint: 此外,它要么接受配置名称,要么接受端点:
public static TClient GetServiceClient<TClient, TContract>(string endpoint)
where TClient : ClientBase<TContract>
{
// Construct client
}
To construct the client, use one of BaseClient<T>
overloads ( from MSDN ). 要构造客户端,请使用BaseClient<T>
重载之一( 来自MSDN )。
To then consume the client, just use the method above as normal: 要使用客户端,只需正常使用上述方法即可:
using(var client = ServiceInterop.
GetServiceClient<MyClient, IMyContract>("http://foo.bar"))
{
// Consume client
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.