Question

I have two separate servers with identical WCF services (let's say, WS1 and WS2) and a C# Mobile CF 2.0 project that need to access both of the services.

Can I do something like this on the C# CF2.0 project?

(...)

if (someCondition == true) 
{
   WS1 aux = new WS1();
}
else
{
   WS2 aux = new WS2();
}

aux.service(parameter1);

(...)

note that I want to have the same variable name, independent of which server I'll access. The problem is: I don't know how to declare it outside the conditional statements and when I just declare it inside the conditional statements they're declared as local variables and I don't know how to make the variable public or global.

Any thoughts or help, please?

Was it helpful?

Solution

Since the WCF Service is exactly the same, just running on different servers, then from your client project simply add a service reference to one of them (WS1 for example). This will generate the client proxy for you. Perhaps give it a generic name too, like "serviceX" (replacing X with something appropriate for your application).

Then, in your client config file, copy the client endpoint it created and add another endpoint with the only difference being the address and the endpoint name. Maybe you want to set the endpoint name property on each endpoint to be "WS1" and "WS2" respectively.

Then, in your code, you should be able to do something like this:

(...)

serviceXClient aux = null;

if (someCondition == true) 
{
   aux = new serviceXClient("WS1");
}
else
{
   aux = new serviceXClient("WS2");
}

aux.service(parameter1);

(...)

OTHER TIPS

If you're using .Net 4.0 or higher you could use dynamic typing.

http://msdn.microsoft.com/en-us/library/dd264736.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top