locked
how to properly close/dispose of a client object that's in a Fault state RRS feed

  • Question

  • Hi,

    I'm submitting SOA jobs to the HPC scheduler, everything is working fine, I'm getting call back returns, no problem.

    I run some tests to see what happens when the client code throws exceptions, and that works fine too, I catch the exceptions in the call back method. However, after all the calls backs are received, I attempt to do a client.Close( ), and I get this exception: "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state."

    What do I need to do to properly close/dispose of the client object, if any of the client calls threw exceptions?

    thanks!

    Damian

     

    P.s .the code is simple, based on MS examples

     

     int j = 0;
     foreach (string job in jobdetails)
     {
      client.BeginExecuteJobRequest(job, SimJobCallBack, new RequestSimState(client, j++));
     }

     AsyncResultsDone.WaitOne();
     client.Close();
    }
    catch (Exception e)
    {
     string msg = e.Message;
    }

    call back method:

    static void SimJobCallBack(IAsyncResult result)
    {
     RequestSimState state=null;
     int startPath=-1;
     try
     {
      state = result.AsyncState as RequestSimState;
      startPath = state.StartingPath;
      string rc = state.GetResult(result);
      Console.WriteLine(string.Format("Returned for startPath {0} = {1}", startPath, rc));
     }
     catch (Exception e)
     {
      string errorMsg = e.Message;
      Console.WriteLine(string.Format("Exception for startPath {0} = {1}", startPath, errorMsg));

     }
     finally
     {
      if (Interlocked.Decrement(ref jobCount) <= 0)
       AsyncResultsDone.Set();
     }
    }

    • Edited by DamianRK Friday, April 10, 2015 3:14 PM
    Friday, April 10, 2015 3:12 PM

Answers

  • Hi Damian,

    In the V2 (HPC Pack 2008) style APIs, the client is derived from System.ServiceModel.ClientBase<TChannel> so you may use the common way in WCF to handle the exception when calling Close(). Meanwhile, I recommend you the SOA HelloWorldR2 sample code @ http://www.microsoft.com/zh-cn/download/details.aspx?id=41633  for V3/V4 (2008 R2/2012, 2012 R2) style APIs which use request/response message contracts and BrokerClient as the enhanced programmng model.

    Best,

    Yutong

    • Marked as answer by DamianRK Friday, April 10, 2015 5:46 PM
    Friday, April 10, 2015 4:47 PM

All replies

  • Hi Damian,

    In the V2 (HPC Pack 2008) style APIs, the client is derived from System.ServiceModel.ClientBase<TChannel> so you may use the common way in WCF to handle the exception when calling Close(). Meanwhile, I recommend you the SOA HelloWorldR2 sample code @ http://www.microsoft.com/zh-cn/download/details.aspx?id=41633  for V3/V4 (2008 R2/2012, 2012 R2) style APIs which use request/response message contracts and BrokerClient as the enhanced programmng model.

    Best,

    Yutong

    • Marked as answer by DamianRK Friday, April 10, 2015 5:46 PM
    Friday, April 10, 2015 4:47 PM
  • Great, found that online (Abort() vs Close())... 

    I did see the new model, I couldn't see any great benefit for me, for the more complicated code, so I think I'll just stay with the simple model... 

    Thanks!

    Damian

    Friday, April 10, 2015 5:46 PM