1. Problem
You need to access logic
contained within an external assembly while transforming a message via a
BizTalk Server map. The logic contained within the assembly cannot and
should not be embedded within the map.
2. Solution
Create a map that uses the
BizTalk Server Scripting functoid's ability to call external assemblies.
The following steps outline the procedure:
Click
the toolbox, and click the Advanced Functoids tab. On the map surface,
between the source and destination schemas, drag and drop a Scripting
functoid. This functoid's input and output values are dependent on the
logic contained within the Scripting functoid.
Connect
the left side of the Scripting functoid to the appropriate source data
elements or records. In this solution, no source data elements are used.
Configure the Scripting functoid.
While the Scripting functoid is highlighted on the mapping grid, right-click the functoid, and select Configure Functoid Inputs.
In
the Configure Scripting Functoid dialog box, on the Script Functoid
Configuration tab, select External Assembly for Script Type and the
appropriate assembly, class, and method for Script Assembly, Script
Class, and Script Method, as shown in Figure 1. Then click OK.
NOTE
You must add a
reference to the external assembly to the Visual Studio project
containing the map for it to be available in the Configure Functoid
Script dialog box. The external assembly must also be placed in the GAC.
To pass in a parameter, click the ellipsis to the right of Input Parameters in the Properties window.
In
the Configure Functoid Inputs dialog box, on the Functoid Inputs tab,
create or order the appropriate input parameters for the method
specified in step 2, as shown in Figure 2, and then click OK.
Connect
the right side of the Scripting functoid to the appropriate destination
data element or additional functoid (if further logic needs to be
applied to the data).
3. How It Works
In this example, the functoid in
the map determine whether there is enough time before the event for the
tickets to be mailed to the purchaser. The XML snippet shown in Listing 1 represents one possible instance of the source schema.
Example 1. Sample Source Instance for the Scripting Functoid Example
<ns0:TicketRequest xmlns:ns0="http://Mapping.TicketRequest"> <EventName>Chelsea vs. Arsenal</EventName> <EventDate>08/06/2010</EventDate> <Venue>Stamford Bridge</Venue> <NumberOfTickets>2</NumberOfTickets> <PurchasedBy>George Murphy</PurchasedBy> <MailTicketFlag>True</MailTicketFlag> </ns0:TicketRequest>
|
Based on this source XML, the
map will produce the XML document (assuming that the current date is
prior to August 4, 2010) shown in Listing 2, with the MailedTicketFlag appropriately set.
Example 2. Sample Output Instance for the Scripting Functoid Example
<ns0:TicketOrder xmlns:ns0="http://Mapping.TicketOrder"> <Title>Chelsea vs. Arsenal</Title> <Date>08/06/2010</Date> <Venue>Stamford Bridge</Venue> <NumOfTickets>2</NumOfTickets> <TicketHolder>George Murphy</TicketHolder> <MailedTicketFlag>True</MailedTicketFlag> </ns0:TicketOrder>
|
This example demonstrates
one of the many reasons why external assemblies may need to be called
from a BizTalk Server map. Here, the external assembly provides access
to a configuration value. This value is held in a configuration file
(but alternatively could be held in a database or other data store),
which is updated over time by system administrators and business
analysts. For the purposes of this example, the following configuration
value was used.
<add key="MinimumDaysAdvanceForMailedTickets" value="-2" />
The value of −2, which represents the minimum number of days required to mail tickets to a purchaser, is added to the EventDate
element (by the Add Days functoid), resulting in a date value two days
prior to the event (08/04/2006 in this example). The current date
(supplied by the Date functoid) is then compared to the calculated date
value (by the Less Than logical functoid) to determine if there is
enough time to mail the purchased tickets. Finally, if there is ample
time to mail the tickets, and the source document indicates the
purchaser requested tickets to be mailed, the MailedTicketFlag element in the destination message is set to true.
The benefit of having the MinimumDaysAdvanceForMailedTickets
value stored in a file external to BizTalk, as opposed to being
hard-coded within the actual map, is that a change to the value does not
require a redeployment of BizTalk Server artifacts for the modification
to be applied. Additionally, by encapsulating the custom logic in an
external assembly, any changes to that logic will require only that the
external assembly is rebuilt and redeployed to the GAC. No changes to
the BizTalk Server environment (aside from a BizTalk service restart to
immediately apply the changes in the redeployed custom assembly) are
required. Implementing this logic in an external assembly has the
additional benefits of allowing reuse of the logic, minimizing code
maintenance, and providing access to the debugging utilities within
Visual Studio.
NOTE
You can step into the
external assembly in debug mode by running the external assembly
solution in Visual Studio in debug mode, attaching to the process of the
Visual Studio solution containing the BizTalk Server map, and testing
the map.
In addition to using a
generic external assembly, a custom functoid could also have been used
to implement the configuration value retrieval logic. It is important to
consider the differences between the two options prior to selecting
your design. The main benefit of using a custom functoid is that the
assembly is hosted within the BizTalk Server environment. The actual
assembly file is located within the BizTalk Server program file
directory, and the functoid can be added to the Functoid Toolbox within
the development environment. Using generic external assemblies is
sometimes a requirement, however, such as when the existing logic
contained within them needs to be accessed directly (without
modification to the source code or assembly location). This may be the
case when using third-party or proprietary assemblies, where you do not
have access to the source code.
Other common examples
of logic contained in external assemblies are complex database-retrieval
mechanisms, calculations above and beyond what is possible with the
out-of-the-box mathematical functoids, and complex date and string
formatting.
Care should be taken with
regard to data types for values passed into and out of external
assemblies. For example, if a source document element having a data type
of xs:int is used as an input parameter
for a method contained in an external assembly, it will be passed as an
integer. Likewise, if the output from an external assembly is used to
populate a destination document element having a data type of xs:int, it should be returned as an integer.
|
|