Search code examples
biztalkbiztalk-2016

WCF-WebHttp Error Handling For Rest API BizTalk 2016


I have a rest API with POST Method where I am posting and receiving success response as JSON format which works absolutely fine. During the failure I receive

Error details: System.Net.WebException: The remote server returned an unexpected response: (400) Bad Request The creation failed because the name or email address of the is invalid

Which I see in Application log and send port gets suspended. I need to capture this in the BizTalk Orchestration where I need to send email if I get any error response.

As the Error Response is not in JSON format don't know how it can be handled. Any idea?


Solution

    1. Enable Routing for failed messages on the Send Port to stop the suspended messages. You might have to update the filter in the send port that subscribes to ErrorReport.FailureCode Exists to exclude your API send port if you only want to handle these in the Orchestration. If you don't have such a port, you might have to create one, and send the failed messages to a NULL adapter or similar.
    2. Have your Orchestration have an scope with an exception block to catch System Exceptions, in there have Exception = SystemException.ToString(); in an Expression shape where Exception is a System.String Variable, and SystemException is the Exception Object Name you defined in the Catch block.
    3. Have logic in your catch block that decides what sort of exception you got, I use a If shape with Exception.Contains("XXXX") where XXXX is text in the exception, or handle it however you want.