images

The BAM API is used to write data into BAM programmatically. There are two main reasons you would use the BAM API:

  • The BAM API is used to store BAM data from a .NET application that’s not part of BizTalk, such as an ASP.NET page or a SharePoint event handler. It’s more common to use BAM from code running on a server, but it can even be used on the client in a Windows Forms application.
  • Some BAM needs within BizTalk can’t be addressed using the TPE. Sometimes you might find it convenient to write to BAM by using code from within an orchestration or a pipeline component, and special classes exist to support these requirements. One case where this is useful is dealing with repeating data. The TPE can’t intercept data from repeated elements, such as line items within an order. The TPE also can’t intercept data that doesn’t have a schema, such as binary data or any message in an orchestration of type string or type System.Xml.XmlDocument. If you want to capture these types of data, you need to use the API.

This chapter starts by describing the major objects of the BAM API, followed by three exercises:

  • Capturing data from a console application using the BAM API
  • Adding a second BAM API application and implementing a continuation between the two applications
  • Capturing data within an orchestration using the BAM API

The chapter concludes with discussions of working with the MessagingEventStream object and the BAM Management API.

The BAM API Objects

The BAM API defines four main classes: DirectEventStream, BufferedEventStream, OrchestrationEventStream, and MessagingEventStream. All of these classes, which are described in Table 10-1, are derived from the base class EventStream, and most of the BAM API consists of methods inherited from EventStream.

images

You’re probably wondering when you should use each of these classes. DirectEventStream and BufferedEventStream can be used in any application that supports .NET, no matter whether the application is running in BizTalk. DirectEventStream is a synchronous call; when you execute an update on a DirectEventStream, the call won’t return until after the database write is committed. BufferedEventStream is asynchronous. When you update a BufferedEventStream, the update is cached and written later. If your goal is to maximize throughput, use BufferedEventStream and take advantage of the cache. If your application can’t ever afford to lose data, even if the server crashes immediately after an update completes, use DirectEventStream.

OrchestrationEventStream is available only when executing in the context of the orchestration engine. It has two advantages:

  • Performance: It can piggyback its database writes on the orchestration persistence points for maximum performance.
  • Consistency: Because it writes during orchestration persistence points, even in the event of a server crash, the state of the orchestration recorded by BAM is guaranteed to be consistent with the last orchestration persistence point.

MessagingEventStream is available only when executing in a BizTalk pipeline. It has similar performance and consistency advantages as OrchestrationEventStream.

Table 10-2 lists the members of the EventStream class. DirectEventStream, BufferedEventStream, OrchestrationEventStream, and MessagingEventStream all implement each of these methods.

images

EXERCISE 10-1. USING THE BAM API

EXERCISE 10-2. ADDING A CONTINUATION

EXERCISE 10-3. USING ORCHESTRATIONEVENTSTREAM

Working with MessagingEventStream

MessagingEventStream is used inside a pipeline component to write to BAM as part of the messaging transactions. It shares the EventStream base class with the other classes you’ve already learned, so the code will look quite familiar.

If you examine a BizTalk pipeline component, it usually does its work in the Execute method of the IComponent interface. Execute has two parameters: an IPipelineContext that contains the pipeline context and an IBaseMessage that contains the message being processed. The pipeline context has a GetEventStream method that returns a MessagingEventStream. So you might see the following code inside a pipeline component:

public IBaseMessage Execute(IPipelineContext context, IBaseMessage baseMessage)
{
    //Other pipeline behavior omitted for clarity

    //Get the Messaging Event Stream
    MessagingEventStream eventStream = context.GetEventStream();

    //This code uses the event stream to write to BAM
    string activityGuid = Guid.NewGuid().ToString();
    eventStream.BeginActivity("PipelineTracking",activityGuid);
    eventStream.UpdateActivity("PipelineTracking ",activityGuid,
       "MessageTime", System.DateTime.Now().ToString(),
       "MessageID", baseMessage.MessageID);
    eventStream.EndActivity("PipelineTracking",activityGuid);
    eventStream.Flush();
}

As you can see, once you’ve used the context to get a MessagingEventStream object, the code is the same as in the previous exercises.

The BAM Management API

BAM provides a management web service available at http://<your server>/BAM/BamManagementService/BamManagementService.asmx. It supplies the methods listed in Table 10-4.

These methods would be useful to a developer building a clone of the BAM Portal. Also, if your application needed to create alerts or subscriptions on behalf of the user, this API would be useful. As an example, consider a Sales department where each state is associated with a different sales representative. Sales representative allocations change frequently and are managed in an ERP application. Whenever an order is received for a state, the salesperson for that state could be notified. A BizTalk application that monitors the ERP system could use the BAM Management Web Service to ensure the sales order alerts were set correctly whenever the salesperson allocations were changed in the ERP system.

BAM provides another web service available at http://<your server>/BAM/BamQueryService/BamQueryService.asmx. It supplies only one method: GetInstanceData(). Unlike the BAM Management Web Service, the BAM Query Service is undocumented, and Microsoft recommends that application developers don’t use it. We agree with this recommendation. The syntax for this web service is bizarre; it’s hard to use and unsupported. Instead, we recommend querying the SQL Server views directly. Although it’s counterintuitive, going directly to the database is more supportable and flexible, and we recommend connecting directly to SQL Server in your applications. If you must have a web service for interoperability, don’t use the Query Service. Instead, create your own web service, or use ADO.NET Data Services to provide a Representational State Transfer (REST) interface to BAM.

images

Summary

In this chapter, you learned about four classes: DirectEventStream, BufferedEventStream, OrchestrationEventStream, and MessagingEventStream. Each of these classes allows you to write to BAM programmatically. DirectEventStream and BufferedEventStream allow you to write to BAM from a .NET application that is not written in WCF, WF, or BizTalk. OrchestrationEventStream and MessagingEventStream allow you to write code in BizTalk to provide capabilities that are not available in the TPE. You also learned how to implement continuations using the API and about the BAM Management Web Service.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.65.1