Exception handling of .NET interoperability code.
zohaibu95@hotmail.com
Member Posts: 223
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:
The response of the webservice which i am getting and using code is as under:
Thanks in advance.
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.
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.
0
Best 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 likeIF 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.0
Answers
-
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 likeIF 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.0
Categories
- All Categories
- 75 General
- 75 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 611 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 253 Dynamics CRM
- 103 Dynamics GP
- 6 Dynamics SL
- 1.5K Other
- 991 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 28 Design Patterns (General & Best Practices)
- Architectural Patterns
- 9 Design Patterns
- 4 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1K General Chat
- 1.6K Website
- 77 Testing
- 1.2K Download section
- 23 How Tos section
- 249 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions