We implement a cluster solution for wcf services. Tell me if there is any proven solution to check the availability of another service and select an alternative, if the main one fails. For example. Messages come from the client to the wcf service and it puts them in a queue. The queue implemented on a separate server and a separate service. If this queued service fails, you need to send messages to a backup service defined in advance. Are there any beautiful solutions for automatic implementation by means of wcf or check the availability of one service in the code, and so on?

  • If you are given an exhaustive answer, mark it as correct (tick the selected answer). - andreycha

1 answer 1

I did not hear about the standard tools for such checks in WCF (more precisely, I heard about WS-Discovery , starting with .NET 4.0, but I did not try it and I’m not sure that it is suitable at all). Therefore I will tell about bicycles.

Need to quickly learn about the fall of a third-party service

The essence of the periodic "ping" service. Start a timer / separate thread that checks the availability of the service. If the service is not available, it makes sense to make an additional 2-3 attempts with increasing interval between them (as is the case with all other methods), since there may be short-term interruptions in the network. If, as a result, the service does not respond, perform a switching operation (change the address of the service to an additional one and do all other things that may be related to this). Repeated attempts, by itself, need to be done when receiving certain exceptions (for example, CommunicationException ).

There is no control over the third-party service.

If the service does not offer a transaction specifically designed to check the status (such as Ping() , Heartbeat() , CheckStatus() , etc.), then you can request its metadata:

 bool isServiceUp = true; try { string address = "http://server/Service.svc?wsdl"; var mexClient = new MetadataExchangeClient( new Uri(address), MetadataExchangeClientMode.HttpGet); mexClient.GetMetadata(); } catch (Exception e) { // если сервис недоступен, получим исключение isServiceUp = false; } 

Control over the third-party service is

A “verification” method is added to the service ( Ping() / Heartbeat() / CheckStatus() ). In its simplest form, this method is empty, but it can also return data on its state (especially if it uses several different systems inside itself - a database, another service, etc.).

It is enough to find out about the fall of the service at the time of its call.

In this case, everything is somewhat simpler, because you do not need to specifically check the service. If during the next call to the service he did not answer, try another 2-3 times. If the service did not respond after that, perform a switch operation and try again to make the call.


Whichever option you choose, you will need the following blocks:

  • code switching services (at least one substitution of one address to another)
  • generalized repeat operation code so that any method can be called as InvokeWithRetry(() => SomeMethod())
  • generic code that connects these two blocks together and causes services to be switched if necessary
  • 3
    WS-Discovery is a messaging protocol for discovering services, similar to SMTP and the like, and will not solve the problem by itself. But it can be adapted for this purpose, because In part, he describes the models you have proposed. More details here specs.xmlsoap.org/ws/2005/04/discovery/ws-discovery.pdf - rdorn