web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Plugin: How to catch exception of web api request for not existing url

(0) ShareShare
ReportReport
Posted on by 115

How can i catch an exception which is caused by an post (HttpClient) to an url which wont respond?


I tried with 
 

catch (FaultException ex)
{
FcTracingService.Trace("The application terminated with an error."   ex.Message);
}


pastedimage1668764001528v1.png

I have the same question (0)
  • Guido Preite Profile Picture
    54,084 Moderator on at
    RE: Plugin: How to catch exception of web api request for not existing url

    if you are doing a direct HttpClient call you are not using the SDK, afaik catching a Microsoft.Xrm.Sdk exception will not work, that error message is from a generic exception catch? Can you share the relevant part of your code?

  • AndreasB83 Profile Picture
    115 on at
    RE: Plugin: How to catch exception of web api request for not existing url

    using System.ServiceModel;
    
    ...
    HttpClient client = new HttpClient();
    ...
    
    string jsonPayload = SerializerWrapper.Serialize(Payload);
    
    try
    {
    StringContent stringContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
    HttpResponseMessage response = client.PostAsync(url, stringContent).Result;
    
    FcTracingService.Trace("CockpitAPIRequest response: "   response.ToString());
    
    if (response.IsSuccessStatusCode)
    {
    string jsonresponse = response.Content.ReadAsStringAsync().Result;
    return SerializerWrapper.Deserialize(jsonresponse);
    }
    else
    {
    FcTracingService.Trace("Error in CockpitAPIRequest: "   response.ReasonPhrase);
    if (!this.silent)
    {
    throw new InvalidPluginExecutionException(response.ReasonPhrase);
    }
    return default(T);
    }
    }
    catch (FaultException ex)
    {
    FcTracingService.Trace("The application terminated with an error."   ex.Message);
    }
    return default(T);

    i am using an HttpClient object and also tried to catch System.ServiceModel.HttpClient. Does not work it seems - still bubbles through and i get an exception message in frontend.

  • Guido Preite Profile Picture
    54,084 Moderator on at
    RE: Plugin: How to catch exception of web api request for not existing url

    can you share additional details? from the trace maybe you are calling a custom api? are you sure the payload is correct? clearly you are getting an error somewhere, instead of FaultException I will catch just Exception (but that is my preference) however don't expect to find there the reason why your call is failing.

  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: Plugin: How to catch exception of web api request for not existing url

    Hi,

    Just a thought...

    Do you really want to use sync plugin for external api ?

    I would recommend to use async plugin so that any error occurs will not rollback the entire plugin pipeline.

    Async plugin failure can be checked from system jobs

  • AndreasB83 Profile Picture
    115 on at
    RE: Plugin: How to catch exception of web api request for not existing url

    You're right. Dont know why i didnt think of that before. In this case it is ok if the plugin is async and cannot send any message back to the frontend. It just lets the external api know that some record has been deleted. Wheter the api endpoint still exists or not doesnt matter for the crm but with a synconous plugin i would have had to catch that FaultException caused by the httpclient sending to a non existing url. I was able to switch to async registration, added an pre image for some information i need and rewrote the code. Works now! Thanks!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 193

#2
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 70

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 69 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans