views:

91

answers:

2

I want to be able to use server2 if server1 fails, so I have two web references but this leads to an "Ambiguous reference" error because it is the same service on both servers.

This is the C# code:

using App.org.weccrc.www; // Main Server using App.org.weccrc.www2; // Backup Server

Is there a way to check if the service is available on server1 before the "using" statement"?

can this be a conditional statement?

Or there is another approach to solve this issue.

Thanks in advance

Raúl

+1  A: 

On your web reference, set the URL Behavior to Dynamic. The generated code for the web reference will now have a configuration setting that controls the location. When you detect that the primary service has failed, you can set the Url property to the fallback location.

How you detect that the primary service has failed will of course depend on the service you're consuming.

Joseph Anderson
+3  A: 

I would do something like this

public R CallWebservice<T,R>(T service, IEnumerable<string> urls, Func<T,R> serviceCall)
    where T : SoapHttpClientProtocol, IDisposable
{
    foreach (var url in urls)
    {
        try {
            service.Url = url;
            return serviceCall(service);
        } catch (Exception ex) {
            // Log Error
            continue;
        } finally {
            service.Dispose();
        }
    }

    // throw exception here which means that all url's failed
}

and you could call it doing something like this

Employee[] employees = CallWebService(
    new DownloadService(), 
    new string[] { "http://site1/service.asmx","http://site2/service.asmx" }, 
    service => service.DownloadEmployees()
);

this would loop through each URL specified and call the webservice. if it fails, then it just attempts to do the next URL until it executes successfully.

Darren Kopp
Nice, beats the heck out of the example I just deleted :)
RSolberg
heh, that happens to me a lot also. i just happened to have working code right in front of me to utilize ;)
Darren Kopp
Thanks Darren and Joseph, both answers were useful for meThanks
Raúl Castellanos