BizTalk: Internals: Mapping: Script functoid: Type of the 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 is defined in the Schema. XML Schema primitive types define how strings should be transformed into other types on the borders, by serialization and deserialization.
 
In our case the string "55" is deserialized into int 55.
Example:

          Script Functoids - Inline C#:   string MyMethod (int param1) { ... }
Xml element which is linked to the 
param1: <Param1>55</Param1> ==>
 
The result will be like this: int param1 = (int)"55";  MyMethod(param1);
 
In this place can be problems!
 
If we had <Param1>55</Param1>, we have no problems.
But we can have also:

 <Param1>ABC</Param1>
 
or <Param1></Param1>
 
or <Param1/>
 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#".
 
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:
{...}
...

Print | posted on Monday, January 8, 2007 12:26 PM

Feedback

# re: BizTalk: Mapping: Script functoid: Type of the Input and output parameters

left by smita at 5/19/2008 2:31 AM Gravatar
hey thanks for the concept i had faced similar problems but now its clarified.

# re: BizTalk: Mapping: Script functoid: Type of the Input and output parameters

left by Raphael C M at 6/30/2008 11:31 PM Gravatar
I have a scenario where in i need to take three inputs to a scripting functoid which is working in conjunction with a looping functoid, because od this the transformed xml comes with this particular node being omited. but if the same scenarior is re created with just one input to the scripting functoid teh result is as desired. Can any one give me a suggestion to overcome this.

thanks
raphael
Post A Comment
Title:
Name:
Email:
Comment:
Verification: