My WCF client can connect to several endpoints. But they all have different addresses, bindings and contracts. So my question is : How can I create my WCF client programmatically depending just of the name of my endpoint I want to connect to (which I have in my code)
Asked
Active
Viewed 985 times
1 Answers
1
If I understood your question correctly, I believe this is the answer [using IPC, can be easily converted to other communication types]
Listener:
_host = new ServiceHost(typeof(ContractClass));
_host.AddServiceEndpoint(typeof(IContract), new NetNamedPipeBinding(), new Uri("net.pipe://localhost/" + listenerEndpointName));
_host.Open();
Client:
var factory = new ChannelFactory<IContract>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/" + listenerEndpointName));
IContract proxy = factory.CreateChannel();

Nissim
- 6,395
- 5
- 49
- 74
-
I'm just talking about the client, not the server. On the client code, I do not have the type of contract. It may be different depending of the endpoint – Tristan Djahel Aug 20 '15 at 13:14
-
So simply add a `switch` statement which will determine the `type` that will be delivered to the factory – Nissim Aug 20 '15 at 13:19
-
I thought about this. But there is no answer using wcf or .net? I mean there is no .net class that can do this thing? – Tristan Djahel Aug 20 '15 at 13:21
-
What do you mean? the above is WCF,NET solution – Nissim Aug 20 '15 at 14:36
-
The ChannelFactory class cannot create proxy client just with the endpoint name? – Tristan Djahel Aug 20 '15 at 14:57
-
1ChannelFactory must know interface, binding and address. Take a look at HttpClient class if you're using http binding – Ricardo Pontual Aug 20 '15 at 17:15