BizTalk: Mapping: Script Functoid: Type of Input and Output Parameters

Rule of thumb:

In the "Script Functoids - Inline C#" use only string types as the input and output parameters.

 

How it works:
If the C# method in the Script Functoids - Inline C#  has non-string parameter, it will be converted to the string on the "border" of this functoid!
Take in mind that the "real" type for Xml elements is "string", no matter how it was defined in the Schema. Xml only mimics the other types.

The Xml holds the values serialized to string and vice versa.

 

In our case the string "1" is desiarilazed to int 1.

Example:

***          ***Script Functoids - Inline C#:  ** **string MyMethod (int param1) { ... }

Xml element: <Element1>55</Element1> ==>

 

The result will be like this: int param1 = (int)"1";  MyMethod(param1);

 

Problems are possible in this place!

 

If we had <Element1>55</Element1>, we have no problems.
But we can also get:

* <Element1>ABC</Element1>
 *or *<Element1></Element1>
 *or *<Element1/>
* or node = null (that means there is not this node in the Xml document instance at all.

 

Those cases give us an exception:

"Exception: Error encountered while executing the transform MyProject.MyMap.
Error:Transformation failed..: System.Xml.Xsl.XsltException: Function
'ScriptNS0:MyMethod()' has failed. ---> System.ArgumentException : Cannot
widen from target type to primitive type."

 

We don't need this result. The exceptions in the map are unwelcome!

 

Rule of thumb:

Use ONLY string types for input parameters in the "Script Functoids - Inline C#". Move the type-conversion INSIDE the method.

 

If we HAVE TO work with non-string types, move the type-conversion INSIDE the method. It prevents these exceptions. We could handle the type errors.

 

string MyMethod (string param1)
{

string returnValue = "0";
* if (param1 ==  null || param1 == "") return returnValue;*
* try*
{ // if we need to work with param1 as int!

* int param1_int;*
* param1_int = Convert.ToInt32(param1);*

}
catch(InvalidCastException ex) // to process the cases with "nonInt" param1:
{...}
...



See Also

Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.