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.
The FormatDateString method below returns null when the value of the OrderDate is empty because the default value of the result variable is null.
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.


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.




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