1. Problem
You need to call a method
contained in a .NET class library assembly. You want to understand how
to reference this assembly within your BizTalk project and how to call
it from an Expression shape.
2. Solution
This solution will walk you
through referencing an external assembly and calling the assembly from
code within an Expression shape. Assume that the assembly is called SampleClass.dll, which contains a class called Helper. The Helper class has a function to set a string of characters to uppercase, as shown in Listing 1.
Example 1. Sample C# Assembly
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace SampleClass { [Serializable] public class SampleClass { public string strToUpperCase(string input) { input = input.ToUpper(); return input; } } }
|
Once the C# assembly has been built and the DLL is available, use the following steps to reference the assembly.
Open a BizTalk project in Visual Studio.
In the Solution Explorer, right-click References under the project header, and select Add Reference.
In the Add Reference dialog box, click the Browse tab, find the SampleClass.dll assembly, and then click OK.
Now
that the assembly is available in the BizTalk project, create a new
orchestration variable. With an orchestration open, click the
Orchestration View window. Right-click the Variables folder, and select New Variable from the drop-down menu.
In the Properties window of the variable, set the following properties (see Figure 1):
Name the variable objSample.
Add a description, such as Demonstration Object.
Specify the variable's .NET type by clicking the drop-down box for the Type property and selecting <.NET Class>. In the Select Artifact Type dialog box, select the SampleClass class, as shown in Figure 2.
Drop an Expression shape in the orchestration, and enter the following code to invoke the external assembly, as shown in Figure 3.
3. How It Works
When creating a .NET class variable in BizTalk, an option exists to Use Default Constructor.
This property causes an instance of the object to be constructed when
the orchestration instantiates. If this property is set to False, the variable will need to be instantiated in an Expression shape through the new keyword (as in step 6 of the solution).
Another property that should be noted is on the referenced assembly itself: Copy Local. This property indicates whether the assembly referenced should be copied into the local bin
directory when a project is built. Several factors will help decide
whether the assembly should be copied locally; if a reference is to a
dynamic assembly (such as another BizTalk project that is built at the
same time as the project in which it is referenced), you probably will
not want to copy the assembly locally.
It is important to note that
when calling a .NET assembly that is not marked as serializable, it must
be called from an atomic scope. When creating an assembly that will be
used primarily by BizTalk, it is appropriate to mark all classes as
serializable. The following example demonstrates doing this in C#.
[Serializable]
public class Example { }