Exception handling of .NET interoperability code.

zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
edited 2017-02-04 in NAV Three Tier
I have a small function which basically send a request to the webservice and return the response. But if the server is not available then it generates the run time exception. I want to handle this exception. Please tell me how to do this? I have two options in my mind
1. Try Function (as it is Dynamics NAV 2016 version)
2. Use codeunit 1291. DotNet Exception Handling.

If i make this function as TRY function (approach 1) then i am not able to return the response. I want to get the response because if the response is successful, i want to DELETE the row from my table. If the response is not successful then I have to send an email. Everything is working fine but i have to cover that exception so that it does not halt my job.

Please tell me which approach is good and how can i handle it. How to use approach 2 if it is good?


My Webservice code is as under:
CallWebService(BaseURL : Text;Method : Text;RestMethod : Text) : Text
HttpClient := HttpClient.HttpClient();
HttpClient.BaseAddress := Uri.Uri(BaseURL);
CASE RestMethod OF
  'GET':  
    BEGIN  
      HttpResponseMessage := HttpClient.GetAsync(Method).Result;  
    END;
  'POST': 
    BEGIN  
      HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result;
    END;
END;
EXIT(FORMAT(HttpResponseMessage.Content.ReadAsStringAsync.Result));


The response of the webservice which i am getting and using code is as under:
Result := CallWebService(IntegrationSetup."URL", 
STRSUBSTNO('api/Books/UpdateSales?bookId=%1&sellingQuantity=%2',"Item No.","Qty. in stock",
"Selling Source"),'POST');
IF UPPERCASE(Result) = API_RESPONSE THEN
  DELETE
ELSE
  IF IntegrationSetup."Support Email" <> '' THEN
    BEGIN
      Email.CreateMessage('Sender Name',SMTPSetup."User ID",IntegrationSetup."Support Email",'Error in webservice', Result,FALSE);
      Email.Send;
    END;


Thanks in advance.
Best Regards
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.

please like / agree / verify my answer, if it was helpful for you. thanks.

Best Answer

  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    Answer ✓
    Fixed myself by using approach 1 i.e. making the function as TRY function.
    This is how I solved it.

    Instead of returing the response from the function i am processing it in that function.

    and in the main i do like
    IF NOT CALLWEBSERVICE() THEN
     BEGIN
        MESSGE(GETLASTOBJECTERROR);
      END;
    
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.

Answers

  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    Answer ✓
    Fixed myself by using approach 1 i.e. making the function as TRY function.
    This is how I solved it.

    Instead of returing the response from the function i am processing it in that function.

    and in the main i do like
    IF NOT CALLWEBSERVICE() THEN
     BEGIN
        MESSGE(GETLASTOBJECTERROR);
      END;
    
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
Sign In or Register to comment.