-1

Im writing a program, where I use a webservice client. 24-7-365. With a default frequency the program runs on a server.

My problem is how do I handle when the webservice (webservice delivered by one of our business suppliers) is down and my object fails in being created?

Abstract:

static void Main()
{
  // This object is based on a webservice supplied to us by 
  // a business supplier.
  var Plants = new foreignSupl.ExtWebServiceClient().GetPlants();

  // My problems is that - when the business suppliers system is down.
  // Then my program dumps.

  // Code in program that depends on: Plants.
  if (insertPlants(Plant, out Plants) == true)
  {
    insert = true;
  }
}

// Approach with try catch:

// I have tried to use try catch but then I get another problem.
//
// Visual studio says: 
// "The name 'Plant' does not exist in the current context.
//
static void Main()
{
  try
  {
    var Plant= new foreignSupl.ExtWebServiceClient().GetPlants();
  }
  catch
  {
    //handling the exception.
  }

  // Code in program that depends on: Plants.

  // Visual studio says: 
  // "The name 'Plant' does not exist in the current context.
  // Therefore I can't compile... 

  if(Plants != null)
  {
    if (insertPlants(Plant, out Plants) == true)
    {
      insert = true;
    }
  }
}

Correct approach I sense that my approach is wrong, but how am I supposed to do this? Thanks in advance.

Lars Hansen
  • 155
  • 1
  • 2
  • 16
  • 1
    if the service is down... then you dont.. the client must be able handle. – Seabizkit Feb 11 '16 at 13:38
  • But I must take action on this, else my program dumps. But how? – Lars Hansen Feb 11 '16 at 13:41
  • 1
    Show code and what you want to handle. – Seabizkit Feb 11 '16 at 13:46
  • 1
    What kind of program? ASP.NET/MVC? WinForms/WPF? Windows Service? Console Application? Does it run automated or is there a user that must be notified? – Mark Feb 11 '16 at 13:51
  • 1
    @LarsHansen `try` and `catch` can be used to handle exceptions. If you post the code that is failing as well as what the behavior is when it fails and what you would like to happen you might get a better answer. – Gilgamesh Feb 11 '16 at 13:57
  • 1
    I think you do not only need code. You need a strategy. Failover, Retry-Management, Service Recovery, etc. This can get rather complex depending on your case - and is not only handled in a few lines of code. – silverfighter Feb 11 '16 at 14:20
  • 1
    If an exception is bad enough that your program cannot recover (I.E. necessary resources are unavailable) it may make sense to simply terminate the program. If you don't want to terminate the program perhaps you want some sort of timer to have the program try again. In any case, we can't tell you what you want the program to do. It is your program. You must decide. – Daniel Feb 11 '16 at 14:27
  • @silverfighter Can you refer to some information regarding such strategy? – Lars Hansen Feb 11 '16 at 14:27
  • @Daniel Cook I think you are the one, who almost realize what my problem is. I have a Windows service that runs 24/7. If the suppliers webservice is down is is supposed to try later on. I would like to measure if the Plant object is created properly before running the related code. My plan was to use exceptions. Do you have any suggestions? – Lars Hansen Feb 11 '16 at 14:31
  • The try-catch looks good, maybe think about putting all the code in the try block. Remember to actually log the exceptions so that you can investigate them later... and/or prove that xyz was down at x time. – Seabizkit Feb 11 '16 at 14:54

1 Answers1

1

Like I said this heavily realize on your SLA's that you want to allow or deliver. People who work on tagling such topic often refer to this book that explain some concepts technology agnostic.

https://pragprog.com/book/mnee/release-it

"n Release It!, Michael T. Nygard shows you how to design and architect your application for the harsh realities it will face. You’ll learn how to design your application for maximum uptime, performance, and return on investment..."

It also depends on your choice of deployment as well as of your choice of clients. The Microservice community has a lot of interesting concepts. Like disposable service, etc. So failing is ok, but fail and expected to have another instance taking over the work load. Google for Chaos Monkey which came out of netflix technoligy team. So take your time... and research... take your risk and budget in account ... and make a technology choice. There is plenty of information on that topic out there. HTH

silverfighter
  • 6,762
  • 10
  • 46
  • 73