简体   繁体   中英

I consume a web service on server1 how can i change to server2 if server1 fails (using C#)

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM