Home > Articles

Integrating .NET

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Creating a Managed AIC Component

Creating a managed Application Integration Component (AIC) is similar to creating a managed component called from a COM client. Chapter 17, "Application Integration Components," discussed two BizTalk Messaging COM AIC interfaces. The first implements the IBTSAppIntegration interface; the second implements the IPipelineComponent interface. The IBTSAppIntegration AIC consists of a lightweight model that requires a single interface and consists of a single method, ProcessMessage, as its entry point. The ProcessMessage public method accepts a single parameter, the document string. This section discusses and demonstrates the use of the IBTSAppIntegration interface in a managed C# component. In this section we are going to create a simple C# AIC that accepts an XML document and then persists it to a local file directory.

Creating the DotNetAIC C# AIC

Creating a managed .NET AIC isn't difficult; however, a few concepts are useful to know prior to tackling the task of developing one. First, all AIC IBTSAppIntegration components must reference the BTSComponentsLIB.dll COM DLL to implement the interface. Because our AIC is managed by the CLR, it will need to reference the COM interface using a wrapper object called the Runtime Callable Wrapper (RCW). This type of wrapper object was used earlier in the chapter to reference the COM ADO DLL in the BankVB Visual Basic .NET component we created. The wrapper is automatically created for you in Visual Studio .NET when you add a reference to the DLL.

Second, when installing AICs, you must properly register them so that Microsoft BizTalk Server 2002 can identify them. The component must be registered with the category IDs for a pipeline component and for the specific type of pipeline component, in this case application integration. You can register the component manually using a registration file or by creating and installing the AIC in a COM+ application. In the C# AIC example, we register the managed object as a COM+ application using the System.EnterpriseServices ServicedComponent interface as we did earlier in the BankVB example.

The steps required to create, compile, and register the DotNetAIC C# AIC example in this section are listed below.

  1. Create two strong name .snk key files using the sn.exe command-line utility.

  2. Create an assembly for the btscomplib.tlb type library and sign it with a strong name key using the tlbimp.exe command-line utility.

  3. Reference the BTSComponentsLib assembly and System.EnterpriseServices namespace in the project references.

  4. Set the appropriate declarative attributes to assign the EnterpriseServices namespace attributes and COM class GUIDs.

  5. Set the strong name key and register the DotNetAIC component for COM interop using the C# project properties dialog.

  6. Compile or build the strong-named managed class.

  7. Register the DotNetAIC object using the regsvcs.exe command-line utility to update the COM+ catalog.

  8. Configure BizTalk Messaging to test the DotNetAIC.

In the following sections, we will walk through each of the preceding steps. After the component has been created, we will then test it using BizTalk Messaging. The test application will pick up a document from a local file directory and call a channel bound to a port that has a transport of type AIC.

Creating the C# Class Library

To create the new class library, add a new C# project named DotNetAIC to the BTSDotNet Visual Studio .NET solution we've been working with. To accomplish this, select the BTSDotNet solution in the Solution Explorer and right-click. In the pop-up menu that appears, select Add, New Project from the menu and then select the Visual C# Projects Class Library project and name it DotNetAIC. Visual Studio .NET automatically creates a class file named class1.cs for you. This project will consist of a single class file named DotNetAICtoFile.cs. To rename class1.cs, select it in the Solution Explorer and right-click. In the pop-up menu that appears, select the rename menu option and rename the file to DotNetAICtoFile.cs as displayed in Figure 14.24.

Figure 14.24 The BTSDotNET solution with the C# AIC project.

The C# AIC accepts a document from the BizTalk Server Messaging Engine as a string and then saves it as an XML document named Output.xml in a local file directory named C:\btsdotnet\AICDeploy\Reply.

Creating Strong Name Keys

The DotNetAIC class requires the use of two strong name key files. One will be used to sign the BTSComponentsLIB assembly, and the other will be used to sign the DotNetAIC. The DotNetAIC file must be signed to enable registration as a COM+ component. The BTSComponentsLIB assembly must be signed to be referenced from an assembly that is signed, in this case the DotNetAIC assembly. We are going to create the keys using the sn.exe command-line utility located in the \Program Files\Microsoft Visual Studio .NET\FrameworkSDK\bin directory of your local machine.

Note

To create the .snk files using this utility, you must either add this location to your system file path or specify full pathnames.

The two key files will be named btstlb.snk and BTSNetAIC.snk. To create them, type the following at the command prompt sn –k btstlb.snk and sn –k BTSNetAIC.snk. Then add the two files to the DotNetAIC project by dragging them into the Solution Explorer under the root of that project's folder.

Next we need to create and sign the BTSComponentsLIB assembly with the btstlb.snk that's been created.

Creating and Signing the BTSComponentsLIB Assembly with a Strong Name Key

To reference the COM type library from a C# project, we need to first wrap that object in a RCW. To create the wrapper, we will use the tlbimp.exe command-line utility located in the \Program Files\Microsoft Visual Studio .NET\FrameworkSDK\bin directory of your local machine. The tlbimp.exe utility also allows you to sign the assembly when you create it. To create and sign the assembly, open a command-line prompt and navigate to the \Program Files\Microsoft BizTalk Server directory to locate the btscomplib. tlb type library file. Then type the following on the command-line prompt tlbimp btscomplib.tlb /keyfile:(localfile path)\btstlb.snk. This creates an assembly named BTSComponentsLib.dll that will be added in the next section as a reference in the DotNetAIC project.

Note

This line of script assumes that the .NET directory has been added to your file path. The source code on the publisher's Web site has copies of the command-line utilities in a subdirectory named Utils. There are also batch files created to assist with the command-line statements. It is sometimes easier to move these files into a single directory when running the utilities, as was done in the example on the publisher's Web site.

Adding References to the C# Project

The AIC managed component references three external objects: the System.xml and System.EnterpriseServices namespaces and the RCW named BTSComponentsLIB.dll that we just created. To add these references to the project, select the References folder in the DotNETAIC project and right-click. In the pop-up menu that appears, select the Add Reference menu item. Then navigate to the .NET tab and select the System.xml .NET and System.EnterpriseServices namespaces. Next, navigate to the COM tab and select the BTSComponentsLIB.dll file using the Browse button to navigate to and select the DLL from the directory you created in the previous section. Then add references to both in the source code using the Using statement as displayed in Listing 14.12.

Note

In C#, the Using statement is equivalent to the Imports statement and allows you to avoid fully qualifying methods in the namespace.

Creating the C# Business Logic

When creating a managed AIC, you must remember two important items. First, because AICs are instantiated by a COM+ application object named the BizTalk Messaging Engine, the managed AIC must be exposed as a COM object using a COM callable wrapper (CCW). Second, you must also ensure that the managed object is registered as a pipeline component. In the DotNetAIC example, we accomplish this by adding the appropriate COM+ declarative attributes to the DotNetAIC class file. We also implement inheritance from the ServicedComponent interface to enable the registry of the managed component as a COM+ application. In Listing 14.12, notice that the public class AICtoFile inherits from both the ServicedComponent and IBTSAppIntegration interfaces. Also notice a reference to the GUID attribute to ensure its uniqueness when registered with COM. You can create the GUID using the Create GUID tool in Visual Studio .NET.

Listing 14.12 shows the code for the C# AIC.

Listing 14.12 The DotNETAICtoFile.cs File

using System;
using System.Runtime.InteropServices;
using System.EnterpriseServices;
using System.Xml;
using BTSComponentsLib;
hhh
namespace CSharpBTSAIC
{
  // Summary description for AICtoFile.

  // Guid attribute ensures same Guid is used for all 
  // compilations, similar to binary compatibility in VB6
  [Guid("1AA73916-FB97-4049-B1AF-DF6BBB43CDB2")]
  // BTSInterop Inherits ServicedComponent and Implements 
  // IBTSAppIntegration
  public class AICtoFile: ServicedComponent, IBTSAppIntegration
  {
    public AICtoFile()
    {
      //
      // TODO: Add constructor logic here
      //
    }

    // This means that the transaction governing the object 
    // will automatically SetComplete() if the method call 
    // returns normally. If the method call throws an exception,
    // the transaction will be aborted
    string IBTSAppIntegration.ProcessMessage(string strDocument)
    {
      // Write the Input XML to a file
      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.LoadXml(strDocument);
      xmlDoc.SelectSingleNode("BTSAICRoot/MessageText").InnerText = 
_"Message from AIC";
      xmlDoc.Save(@"C:\btsdotnet\AICDeploy\Reply\Output.XML");

      // Simply return the Input as Output
      return strDocument;
    }
  }
}

The ProcessMessage method's business logic creates a new instance of an XML document and then saves the contents of the input string strDocument to a file named output.xml file.

Note

To keep things simple, the pathname is hard-coded into the example.

To add the business logic to the DotNetAIC.toFile.cs files in the DotNetAIC C# project, open the .cs file in Visual Studio and add the source code displayed earlier in Listing 14.12.

Compiling and Registering the Component

To register a managed object with COM+, it must be signed with a strong name.

Earlier we created two .snk key files, one to sign the BTSComponentLib.dll file and the other to sign the DotNetAIC managed component. To accomplish this, we need to tell Visual Studio .NET that we want to compile this project for COM interop with a strong name key. First, select the project in the Visual Studio .NET Solution Explorer and right-click, selecting the Properties menu option. In the dialog that appears, select the Common Properties folder's General option and reference the key filename in the Wrapper Assembly Key File configuration setting as displayed in Figure 14.25.

Figure 14.25 Setting the C# .NET strong name key file properties.

Next, navigate to the Configuration folder's Build menu option and set the Register for COM Interop value equal to True as displayed in Figure 14.26.

The COM Interop property creates the CCW and type library automatically for us when we compile the project. After the component is successfully compiled, run the .NET Framework command-line utility regsvcs.exe to create and register the object with COM+. To accomplish this, open the command window, navigate to the \BTSDotNET\ CSharpBTSAIC\bin\Debug directory, and type regsvcs dotnetaic.dll. Then verify its registration by opening the COM+ MMC. If an application named CSharpBTSAIC is present, it has been successfully registered.

Figure 14.26 Setting the C# .NET COM Interop property.

Note

The .NET command-line utilities location is not usually found in your local system path. The utilities are located in the c:\Program Files\Microsoft.NET\ Framework SDK\bin directory. You might need to include their location in your local path or fully qualify your references to the utilities.

BizTalk Messaging AIC Setup

To test the managed AIC, we are going to create a simple BizTalk Messaging example. The messaging example processes an XML document named BTSAICInstance.xml using a BizTalk Server file receive function. In the DotNetAIC example, we are going to create a new BizTalk Messaging organization named DotNETAIC and a new document named BTSAIC. The document specification will be stored in a Web Dav directory named Bank. The BTSAIC.xml document specification is displayed in Figure 14.27 in the BizTalk Editor.

To configure BizTalk Messaging, we need to complete the following steps. Each of these steps is described in detail later in this section.

  1. Create an XDR schema using the BizTalk Editor and save it to a WebDav repository named Bank.

  2. Create a new Organization named DotNetAIC.

  3. Create a new Port to an Organization named To BTS AIC using the BizTalk Messaging Manager.

  4. Bind the Port to a Channel from an Organization named From AIC Test that has inbound and outbound documents set to the BTSAIC document. The Port will have a transport of AIC that will reference the C# DotNetAIC.dll AIC created earlier.

  5. Create a file receive function using the BizTalk Administration console that will pass .xml files to the From AIC Test Channel.

  6. Test the AIC by dropping an instance of the BTSAIC document into the receive directory.

Figure 14.27 The BTSAIC.XML BizTalk document.

To configure BizTalk messaging, begin by creating the BTSAIC.xml BizTalk XDR schema using the BizTalk Editor and save it to a new WebDav folder named Bank. The schema can be created by hand or downloaded from the publisher's Web site. After you have created and saved the schema, open the BizTalk Messaging Manager and create a new document definition that references this schema in the repository and name the document BTSAIC. Then create a new Organization and name it AIC Test.

Next, create a new Port to an Organization and name it To BTS AIC. In the Messaging Port Wizard Destination Organization dialog that appears, select the AIC Test organization using the Browse button next to the Organization option. Then set the Primary transport type to AIC using the Browse button next to the Primary Transport Address option. In the dialog that appears, select AIC and then use the Browse button to select the CSharpBTSAIC.AICtoFile component. In the final wizard step, select Create a Channel for This Messaging Port and specify the From an Organization channel type.

Note

When you select the transport type of AIC, a dialog appears asking you to select a component from a list of registered AICs. If the C# component is not listed in the dialog, it has not been properly registered. To verify that it is registered, first check to see that the COM+ application exists. If it does, delete it manually using the COM+ MMC, recompile the managed object, and rerun the regsvcs.exe command-line utility.

The port will be bound to a channel named From Test AIC with the inbound and outbound documents set to the BTSAIC XML document created earlier.

In the New Channel Wizard that appears, name the Channel From DOTNETAIC and click Next. In the Source Organization dialog that appears, select the AIC Test organization and click Next. In the Inbound Document dialog that appears, select the BTSAIC document using the Browse button and click Next. Then select the BTSAIC document for the outbound document as well. The remainder of the wizard steps should keep the default settings.

The BizTalk file receive function will be configured to process all .xml files in the \BTSDotNet\AICdeploy\Submit directory. The channel also will be exclusively specified to From DOTNET AIC channel. To create the receive function, open the BizTalk Server Administration console and select the Receive Functions folder under the BizTalk Server Group and right-click. In the pop-up menu that appears, select the New File Receive Function menu option. In the Add a File Receive Function dialog that appears, name the function AIC Test, set the File Types to Poll for to *.xml, the Polling Location to \BtsDotNet\AICDeploy\Submit, and then click the Advanced button. In the Advanced Receive Function Options dialog that appears, select the From AIC Test channel in the Channel Name drop-down list and save the function.

To run the example, copy the BTSAICInstance.xml file from the \BTSDotNet\Docs directory to the \BTSDotNET\AICDeploy\Submit directory. The file receive function should pick up the file, executes the managed AIC, and then places the Output.xml file in the \BTSDotNet\AICdeploy\Reply subdirectory.

Note

The BTSAICInstance.xml document can be downloaded from the publisher's Web site or created using the BizTalk Editor.

Note

This example assumes a basic understanding of BizTalk Messaging. To learn more about BizTalk Messaging, refer to the chapters on BizTalk Messaging and/or the appropriate BizTalk Server documentation.

  • + Share This
  • 🔖 Save To Your Account