“Extension functions cannot return null values.” Exception when running a map in BizTalk 2013

Upgrading a BizTalk project to a new version is always a smooth ride so when I upgraded a BizTalk 2010 application to BizTalk 2013 I wasn’t expecting many difficulties. ;-) Everything went as expected when I migrated to the new version in Visual Studio but when I tested the maps I got the Exception: “Exception has been thrown by the target of an invocation.” with the InnerException: “Extension functions cannot return null values.”
Nick Hauenstein from Quicklearn Training wrote the blog post What the BizTalk Server 2013 Mapper Updates Mean for You that pointed me in the right direction!  My custom Scripting functoid in the map returned null and in BizTalk 2013 it always must return something. 

 

Scripting Functoid  Error example

In this sample I’m going to map a SalesOrder message to a PurchaseOrder. I’ve used a Scripting functoid that calls the MapHelper class in an external assembly to format the Date. Also the Logical Existence functoid is used to test if the OrderDate field exists in the source message.
Map
 
The FormatDateString method below returns null when the value of the OrderDate is empty because the default value of the result variable is null.
CodeExternalAssembly
 
When the OrderDate field in the source message is empty, the FormatDateString method in the MapTester class is called because the Logical Existence functoid returns true.
Now you get the “Exception has been thrown by the target of an invocation.” exception because in a BizTalk 2013 map a method must return something when it is invoked and can not return null.
 

Testing

I’ve used the MapTester tool that I have created to test the map but I‘d noticed that you couldn't see much error information when an exception occurs.  
I’ve added the InnerException and StackTrace information from the exception so now you can see detailed error information in the info screen.
MapTesterException
 

 

Conclusion

It took me a while to find out which functoid caused the exception because I was searching in the wrong direction but as always are Google and Bing in those cases your best friends!
The new version of the MapTester tool can be a great help when you want to test your maps because you can quickly see what the result message is or you get a red box with detailed error information in the info screen when an exception occurs.

You can download the new version of the MapTester tool with the source code here: http://code.msdn.microsoft.com/Execute-BizTalk-2013-maps-e8db7f9e

About the author

Tomasso Groenendijk lives in Netherlands and is a senior integration consultant at Motion10. He has over 15 years’ experience in software development and software design. Tomasso is specialized in application integration with a strong interest in the Windows Azure cloud platform. In 2014, 2015 and 2016 Tomasso was awarded with the Microsoft Azure MVP award. He is an active contributor to the integration community through his work on his blog, MSDN samples, TechNet Wiki and also speaking on events.

 
 
 
MVP profile Tomasso Groenendijk
 
 
Sentinet Product Specialist
 
 
BizTalk360 Product Specialist

Month List