Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi all,
I have a question concerning Windows Services.
I created a WCF service, hosted as a Windows Service, that communicates with a client application. All works pretty well.
Now I want to install the same service again on a different server. The client should communicate with both services. So it should open, subscribe, receive callbacks and send requests to both services (that are actually the same service) on both servers.
Is this even possible? Or is it only possible when having two client applications open (one for each server)?
I tried Googling, but couldn't find anything (or I'm not sure what to look for).
Thanks.
Posted
Comments
wikus70 5-May-14 4:31am    
You can have multiple connections in your client application. Then use a config file for the client application to list all the remote servers it needs to connect to. This will help save you the trouble of editing the application every time you need to add or change a server. I hope this helps?
Sander Rossel 5-May-14 5:59am    
Thanks. That was helpgul :-)

1 solution

yes, it is quite possible. actually they are NOT the same services. they are different services running on different machines, having the same interface and data types (service contract+data contract). service, as the name implies, a process listening for other processes (local or remote, changes according to configuration) to do something. therefore, if the configuration (service config, firewall config, etc) allows, you can connect them to get a service without any limitation in the context. two different service instances may provide the same data (if you request is to get some data), or not. it depends on your design. for example, they may share the same data source (database), therefore you get the same data for the same service method call. if their underlying data sources are different, you get different data from those instances even if you call the same service method, naturally.

Machine1.AServiceClient _clientMachine1 = new Machine1.AServiceClient();
Machine2.AServiceClient _clientMachine2 = new Machine2.AServiceClient();

Machine1.Data _data1 = _clientMachine1.GetData(); 
Machine2.Data _data2 = _clientMachine2.GetData();


In this example, _data1 and _data2 may have the same data, but their types are different. the first one is under Machine1 namespace, and the other is under Machine2 namespace.

It doesn't have to be in this way. you can use the same client code with different connection configuration:

AServiceClient _clientMachine1 = new AServiceClient(config1); // for machine1
AServiceClient _clientMachine2 = new AServiceClient(config2); // for machine1

Data _data1 = _clientMachine1.GetData();
Data _data2 = _clientMachine2.GetData();


In this case, _data1 and _data2 have the same type but they are still different instances since they are different :)

actually, the client part of your question doesn't make much sense. because service is something and service-client is another thing. you can even create multiple service clients for the same service in the same code. such as:
AServiceClient _aclient1 = new AServiceClient(); 
AServiceClient _aclient2 = new AServiceClient(); 

_aclient1.DoSomething();
_aclient2.DoSomething();



I think my solution doesn't make much sense. because, every example goes to 'everything is possible' :)
 
Share this answer
 
v2
Comments
Sander Rossel 5-May-14 6:20am    
Right, so I can get the endpoints from my configuration and create a new client instance with the specified endpoint. Very nice! I had never used it like this before.
My current code looks like this:
_client = new MyServiceClient(new InstanceContext(this)); // Grabs the default/only endpoint I guess?

Should be something like:
_client1 = new MyServiceClient(new InstanceContext(this), "MyEndpoint1");
_client2 = new MyServiceClient(new InstanceContext(this), "MyEndpoint2");

And I'll probably be doing that in a loop. The specification states that the service needs to be installed on x servers. Starting with one, but in the future possibly two or three (or even more).
Many thanks for your answer! :-)
Vedat Ozan Oner 5-May-14 7:25am    
You are welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900