JSON null response REST API from Biztalk Server

Calling a a REST API from Biztalk is a straightforward operation with WCF-Webhttp adapter. but when we are facing a scenario where the API is returnin an unexpeted response essentially from non maintained application. To deal with this cenario we have 2 options:

-Create a custom component piepline

-Custom behavior with message inspector

in my case,  the API return a message body containing « null » string with a 200 http status. I choose to create a custom behavior to handle the message too early on the adapter level.

My custom behavior project contains:

-a message inspector implementing IClientMessageInspector interface

the AfterReceiveReply method is fired when the response is returned from the API :

public void AfterReceiveReply(ref Message reply, object correlationState)
{
try
{
var replyMsg = reply.ToString();
var mystr = replyMsg.Substring(replyMsg.IndexOf(‘>’) + 1, 8);
byte[] data = Convert.FromBase64String(mystr);
string decodedString = Encoding.UTF8.GetString(data);

if (decodedString == « null »)
{

string response = «  »;
Message newReply = Message.CreateMessage(MessageVersion.None, null, new TextBodyWriter(response));
// very important – forces WCF to serialize the message body contents as
// « Raw » which effectively means no serialization
newReply.Properties[WebBodyFormatMessageProperty.Name] = new WebBodyFormatMessageProperty(WebContentFormat.Raw);
var responseProperty = new HttpResponseMessageProperty();
responseProperty.Headers[« Content-Type »] = « text/plain »;
newReply.Properties[HttpResponseMessageProperty.Name] = responseProperty;

reply = newReply;

}
}
catch { }
}

the response is received  in 64 base format : <Binary>xxxxxxxxx</Binary> »

after a converting the content and if the message is « null », with a custom writer I create a new empty message. But why an empty one?  To take benefit of the new JSON decoder « AddMessageBodyForEmptyMessag » property and to create an empty JSON message « {} »

by this way, i can avoid to receive a null reference exception.

-endpoint behavior implementing IEndpointBehavior interface

After build and GAC the dll, we have to configure this new extension:

1-C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config and C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config.

behav_conf

2-JSON receive pipeline conf:

behav_conf2

3-add the extension on the send port behavior section

behav_conf3

 

You can find the source code here.

if you liked this tip, please share.

Publicités

About hichamveo

I am a Microsoft certified solution developer with more than 10 years of I.T. experience working in different industry sectors(Group Life Insurance, Retail, E-Learning, E-Commerce,Infrastructure & Urban Development ) on projects ranging from small bespoke applications to multi-million Euros enterprise solutions. I am proactive in learning new technologies and like to take the responsibilities in work as it comes. Currently focused on Microsoft Integration technologies, especially BizTalk server, .NET solutions and Windows Azure.

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s

%d blogueurs aiment cette page :