Saturday, January 8, 2011

Deploying Business Rules Programmatically

static void Main(string[] args)
{
if (args.Length < 1)
Console.WriteLine("Format: DeployPolicies.exe ");
else if (args[0] == "/u")
{
Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver rdd = new Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver();

//Undeploy specified policies
for (int i = 1; i < args.Length; i++)
{
string policyName = args[i];
RuleSetInfo rsi = new RuleSetInfo(policyName, 1, 0);
rdd.Undeploy(rsi);
RuleStore rs = rdd.GetRuleStore();
rs.Remove(rsi);
}
}
else
{
//import the BRL file and publish policies in the XML file
Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver rdd = new Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver();
rdd.ImportAndPublishFileRuleStore(args[0]);

//deploy specified policies
for (int i = 1; i < args.Length; i++)
{
string policyName = args[i];
RuleSetInfo rsi = new RuleSetInfo(policyName, 1, 0);
rdd.Deploy(rsi);
}
System.Threading.Thread.Sleep(60000);
}
}

Ref:http://blogs.msdn.com/b/biztalkbre/archive/2007/02/16/sample-deploying-business-rules-programmatically.aspx

Friday, January 7, 2011

Typed XML Facts

http://msdn.microsoft.com/en-us/library/aa561096%28v=BTS.70%29.aspx

Typed Facts

Typed facts are classes that implement the ITypedFact interface: TypedXmlDocument, DataConnection, TypedDataTable, and TypedDataRow.

TypedXmlDocument

The TypedXmlDocument class represents the XML document type in the Business Rules Framework. When you use a node of an XML document as an argument in a rule, two XPath expressions are created: the Selector and Field bindings.

If the node has no child nodes, a Selector binding (also known as an XmlDocument binding) is created to the node's parent node and a Field binding (also known as an XmlDocumentMember binding) is created to the node itself. This Field binding is relative to the Selector binding. If the node has child nodes, a Selector binding is created to the node and no Field binding is created.

Suppose that you have the following schema.

Case.xsd
Sample schema displayed in Facts Explorer

If the Income node is selected, only a Selector binding is created, because the node has child nodes. The default XPath expression in the XPath Selector property of the Property pane contains:

/*[local-name()='Root' and namespace-uri()='http://LoansProcessor.Case']/*[local-name()='Income' and namespace-uri()='']

However, if the Name node is selected, both a Selector binding and a Field binding are created. The binding information looks like.

Property Value

XPath Field

*[local-name()='Name' and namespace-uri()='']

XPath Selector

/*[local-name()='Root' and namespace-uri()='http://LoansProcessor.Case']

You can change the default XPath expressions for the XML nodes before you drag the node into a rule argument, and the new binding information is placed in the policy. Note, however, that any edits that are made to the XPath expressions must be re-entered in the Business Rule Composer when the schema is reloaded.

When vocabulary definitions are created for XML nodes, the XPath expressions for the bindings have similar defaults based on the rules described earlier, but can be edited in the Vocabulary Definition Wizard. Changes to the expressions are placed in the vocabulary definition and are reflected in any rule arguments built from the definitions.

DataConnection

DataConnection is a .NET class provided in the RuleEngine library. It contains a .NET SqlConnection instance and a DataSet name. The DataSet name enables you to create a unique identifier for the SqlConnection and is used in defining the resulting type.

The DataConnection class provides a performance optimization to the Business Rule engine. Rather than asserting into the engine very large database tables (TypedDataTable class) that may contain many database rows (TypedDataRow class) that are not relevant to the policy, you can assert a lightweight DataConnection. When the engine evaluates a policy, it dynamically builds a SELECT query based on the rule predicates/actions and queries the DataConnection at execution. For example, suppose you have the following rule:

IF NorthWind.Products.UnitPrice >= 0
THEN

The following SQL query is generated by from the rule:

Select * From [Product] Where [UnitPrice] >= 0

The results of the query are asserted back into the engine as data rows.

Aa561096.note(en-us,BTS.70).gifNote
The use of an OleDbConnection in a DataConnection is not currently supported.

When you select a database table/column to use in a rule condition or action, you can choose to bind to the object using either DataConnection or TypedDataTable by selecting "Data connection" or "Database table/row" from the Database binding type drop-down box in the Property Window for the Databases tab of Fact Explorer.

Aa561096.note(en-us,BTS.70).gifNote
The DataConnection binding is used by default.
TypedDataTable

You can assert an ADO.NET DataTable object into the engine, but it will be treated like any other .NET object. In most cases you will instead want to assert the rule engine class TypedDataTable.

TypedDataTable is a wrapper class that contains an ADO.NET DataTable. The constructor simply takes a DataTable. Any time a table or table column is used as a rule argument, the expression is evaluated against the individual TypedDataRow wrappers, and not against the TypedDataTable.

TypedDataRow

This is a typed fact wrapper for an ADO DataRow object. Dragging a table or column to a rule argument in the Business Rule Composer results in rules built against the returned TypedDataRow wrappers.

BizTalk Server Business Rules and Static Methods

The Business Rules Engine that ships with BizTalk Server 2006 now supports the usage of static objects without passing that particular object into the rule as a "fact." However, a registry change is needed to get it working. Why is this valuable? Now you can build business rules that only require stateful objects to be passed in as facts, and leave helper functions, lookups and the like as static objects.

I was recently working on setting a timestamp on my XML document from within a business rule. With BizTalk Server 2006's changes for static object support, I wanted to use the standard "DateTime.Now" function contained in the mscorlib assembly, without passing an instance of that object into the rule as a fact. My business rule looked like this:

However, each time I ran it, I got nothing back in my XML node. After a request on our internal distribution list for some guidance, I got some help from the grand poo-bah of business rules, Jurgen Willis. Apparently the following key is required in the registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\BusinessRules\3.0\StaticSupport (DWORD)



There are three valid values for this key:

  • 0 - This is the default key, and pretty much mimics the behavior of BizTalk Server 2004 where an instance of an object is always required as an input fact, and the method is only called when the rule is evaluated or executed.
  • 1 - An instance of the object is NOT required, and the static method is called whenever the rule is evaluated or executed
  • 2 - An instance of the object is NOT required, but the static method will be called at rule translation time (only if the parameters are constants). This is primarily meant as a performance optimization. However, note that static members used as actions will NOT be executed at translation time, but static methods used as parameters may be.
So obviously in my case, I needed to use keys 1 or 2, with 1 being the only legit choice since for a static method like DateTime.Now, I'd want it evaluated during execution, not at rule translation!

The key wasn't present on my machine, so I'm not sure if this is something we're going to include in the RTM bits. But now you know, and knowing is half the battle.

Wednesday, December 29, 2010

Orchestration Debugger

Working with the Orchestration Debugger

The following screenshot shows a typical debugging session in the Orchestration Debugger.

Orchestration Debugger with break point hit.

The Orchestration Debugger consists of three panes that are always present and two optional panes that appear when attached to an orchestration instance:

  • The Service pane displays the instance service name and GUID that uniquely identifies the orchestration instance, debug mode, orchestration state, whether the Orchestration Debugger is attached, and service options.

  • The Tracked Events pane lists the status of every action performed in the orchestration, such as whether it started or completed. As you select each of the rows in this pane, the corresponding shape in the Orchestration pane appears highlighted in green when the shape starts and blue when the shape finishes.

  • The Orchestration pane is where a visual representation of the orchestration is rendered with all of its shapes.

  • The Variable List pane appears when attached to an orchestration instance and displays the name, value, and type of the variable. The value indicates if the variable is null or, if not, then what kind of object it contains. Type is the Assembly.Namespace.Name of the object.

  • The Variable Properties pane appears when attached to an orchestration instance and displays properties for the variable that vary according to the type of object. For example, for messages this includes Message Parts (including Name, Properties, Size, Type, and Value) and Message Properties (including Context, Name, PartCount, Scope, Type, and Value).

You will likely focus on the Orchestration, Variable List, and Variable Properties panes when debugging orchestration instances. Using the Orchestration pane, you can determine how a message is flowing through an orchestration, while the Variable List and Variable Properties panes show current variables and their properties, values, and types. This is useful for debugging decision shape errors, determining what content is reaching a port, seeing the "before" and "after" messages in a transform operation, and other tasks.

Resuming an Orchestration Instance

To resume the execution of an orchestration in the Orchestration Debugger, click Debug on the menu and choose Continue. The instance will continue execution until either another breakpoint (either class-level or instance-level) is hit or the orchestration completes.

Aa953746.note(en-us,BTS.20).gifNote
Tracked events will not be refreshed until you click Refresh on the File menu.
Debugging a Called Orchestration

If the orchestration calls another orchestration, you can debug the called orchestration by right-clicking the call orchestration action in the Tracked Events pane and then clicking View Called Orchestration. In the orchestration below, the Call Orchestration shape is named CallOrchestration_1.

Viewing a called Orchestration in debug mode.

Right-clicking the corresponding Call event in the Tracked Event pane and then clicking View Called Orchestration causees the Orchestration Debugger to load the called orchestration. In the above orchestration, notice that the title bar displays the instance's GUID as well as #0. This number represents the orchestration being called; the original orchestration is always #0. When the called orchestration is brought up, it will be #1 and so on.

You can return to the calling orchestration by clicking Debug and choosing View Calling Orchestration in the Orchestration Debugger. This works recursively so if your original orchestration (#0) calls another orchestration (#1) that in turn calls another orchestration (#2), you would need to choose Debug | View Calling Orchestration twice.

Debugging an Orchestration Instance That is Suspended (Resumable)

Orchestration instances that are in the Suspended (Resumable) state can be debugged by the Orchestration Debugger by resuming the instance in debug mode.

To debug a Suspended (Resumable) orchestration instance
  1. Click Start, point to Programs, point to Microsoft BizTalk Server 2006, and then click Health and Activity Tracking.

  2. On the HAT tool menu, click Queries, and then choose Most recent 100 service instances or one of the other query choices as appropriate to your situation.

  3. In the search results, right-click an orchestration service instance that is in the Suspended (Resumable) state and then click Orchestration Debugger.

  4. From the Orchestration Debugger, click Debug and choose Resume in Debug Mode.

    The Orchestration Debugger opens the orchestration instance in debug mode as if a breakpoint had been reached.

  5. Debug the orchestration.

Debugging Orchestrations on a Remote Computer

You can use the Health and Activity Tracking tool to debug a remote computer provided the following conditions are met:

  • The BizTalk Server Administration tools have been installed.

  • You are a member of the BizTalk Server Administrators group on the computer where the BizTalkMsgDb exists.

  • The SQL Server instance has been configured to allow inbound network connections.

Aa953746.Caution(en-us,BTS.20).gifWarning
Grant access only to individuals that require access. For more information about HAT and security, see Security Considerations for Health and Activity Tracking.
To connect HAT to a remote computer
  1. Click Start, point to Programs, point to Microsoft BizTalk Server 2006, and then click Health and Activity Tracking.

  2. On the HAT tool menu, click Tools and choose Preferences. This will bring up the Preferences screen.

  3. Click Live Data, and then specify the Management SQL Server name and database name. In the example below, the HAT tool is configured to connect to the remote Management SQL Server BTS_MGMNT_SVR01 using the default management database named BizTalkMgmtDb.

    HAT Preferences screen
  4. Click OK.

Limitations of the Debugger

The Orchestration Debugger does not support the following scenarios:

  • Debugging inside of a Message Construction shape. You can set a breakpoint on the Message Construction shape.

  • Debugging inside of an atomic scope. You cannot set a breakpoint on shapes inside of an atomic scope or an orchestration that is defined as Atomic.

  • Setting a breakpoint on a Group shape. You can set a breakpoint on individual orchestration shapes inside of the Group shape.

  • Setting a breakpoint on a compensation block. You can set breakpoints on the actions inside of the compensation block.

  • Setting a breakpoint on a catch block. You can set breakpoints on the actions inside of the catch block.

You should also be aware of the following considerations when using the Orchestration Debugger:

  • If you track an orchestration modified without changing the version number, you must restart all the host instances to which the orchestration is enlisted. This ensures that any shape change in the newly deployed version displays correctly as you step through the Orchestration Debugger

  • When you attach to an instance in the Orchestration Debugger, any atomic scopes in the orchestration instance will cause gaps to appear in the tracked events list. This happens because events for the shapes inside atomic transactions do not get persisted until the scope commits and because the debugger reloads events onto the end of the list, so any gaps remain unfilled during the live session.

    Aa953746.note(en-us,BTS.20).gifNote
    You can eliminate gaps in the tracked events list by refreshing the view.

If you need to capture detailed information about program flow, variable contents, versions and other diagnostics you may want to use Debug and Trace statements as explained below.

Tuesday, December 28, 2010

BizTalk 2009 admin console is not working all of a sudden while deploying the release

BizTalk 2009 admin console is not working all of a sudden while deploying the release
  1. Full stop the application first.
  2. If an assembly is previously deployed there is no need to add it as a resource to the deployed application.
  3. First remove the previous version of the assembly from GAC (After stopping the application). Place the new assembly in the GAC.
  4. Then in the resource node select the assembly and click refresh.
  5. Browse the new version of the assembly.
  6. Then restart the application and the host instances.
  7. The new version of the assembly will execute.

BizTalk 2009 + SharePoint/WSS 3.0 integration

http://kentweare.blogspot.com/2009/10/biztalk-2009-sharepointwss-30.html

BizTalk Flat File Schema Wizard

I have found the Flat File Schema Wizard in BizTalk Server 2006 and BizTalk Server 2006 R2 to be extremely useful and figured that I would blog about it for anyone looking for an introduction to this feature.

Inside of BizTalk, the preferred message type is XML. If messages are in an XML format, BizTalk can do intelligent things with the messages. These "intelligent things" include routing the message based upon a promoted property, tracking elements in BAM or making logic decisions inside of an orchestration based on a particular value of a node.

Generally the way Flat Files are processed at Run-time are:

  1. Flat file is received by adapter configured to watch the receive location's URI
  2. A custom pipeline is required to disassemble Flat File into its XML equivalent
  3. BizTalk Orchestration(if non-pure messaging scenario chosen) would then pick up this XML equivalent version of the flat file. The message would then move through the business process.
  4. If/when BizTalk needed to send out a version of this XML message as a Flat file, a custom Send Pipeline would be required that would take the XML message and assemble the Flat File.
  5. In the Send Port adapter, this custom Send Pipeline would need to be configured in order for the Flat File to be delivered to the end point.

I will now walk you through what is required at design time in order to enable the runtime to support the processing of Flat Files:

1. In your Schema and Maps project, "Add a New Item"



2. On the left hand side, select "Schema Files" and then select "Flat File Schema Wizard" from the right hand side. In the bottom centre of the wizard, provide a name for this Flat File Schema.

3. The Wizard based interface will now appear which will walk you through defining your Flat File structure. Click on the "Next" button to continue.



4. You will need an instance, or sample, of the flat file that you are about to create a schema for. Browse for this file, provide a "Record" name and update the "Target namespace" as required.




5. The wizard will then load up your instance file to further break it down. In this step, you need to define how records are differentiated. Depending upon the complexity of your flat file, your record definition may be different from mine.
For my sample, the structure of my record is as follows:

CustomerID,DiscountGroup,Address,City,EmailAddress,PhoneNumber{CR}{LF}. Since each record is contained within one line I want to select the entire first line.


6. I then need to indicate how the record is delimited. In my scenario it is based upon a Carriage Return/Line Feed. Something to note here is that my source file is based upon a Windows File structure. Unix indicates the end of a line differently than Windows.

7. In the next step, I am providing exactly how the child delimiter is defined. Since this is a Windows based file, I will select "{CR}{LF}" . Other options include {CR}, {LF}, {TAB}, {SPACE}, {0x1A}, {}, {.}, {;} or you can provide your own symbol.

8. So at this point, I have defined what a Record looks like but I have not broken down the various elements that make up a record. By default "Repeating record" is not selected, so you will want to pull down the "Element Type" drop down to select it. If you do not do this step, you will not have the ability to break down your records into individual elements/attributes.

Also, if you want, rename the parent node for each record, modify the "Element Name" field to your desired name.



9. Select "Next" to continue to the next screen where you will be able to further define how each record is broken up.



10. So here is the start of breaking down the record into individual elements. You want to ensure that the {CR}{LF} symbols are not selected and click "Next".



11. Here is where you tell BizTalk how to parse the record. The two options are by delimiter symbol or relative. Since I am using a CSV file, I selected "By delimiter symbol". If you have a fixed width file, then you would want to select "By relative positions".



12. By default, the comma symbol is populated in the Child delimiter drop down. If you use a different like a pipe '' or a period '.' you can change this here.



13. In this screen, you are able to define each of the Elements/Attributes for your data. Much like any XSD, you are able to define the data types as you see fit. Also note, in the far right hand column, BizTalk is loading the contents of you sample row into the wizard for you to verify whether BizTalk has parsed it correctly.



14 BizTalk will then show you a break down of what your XML tree will look like. Once you click Finish the Schema will be available to you in your BizTalk solution.




15 You now have a Flat File Schema for use within BizTalk!



Since the Disassembling and Assembling of Flat Files occur within pipelines, having this Flat File schema will not do us much good until we create the pipelines to provide the conversion between Flat Files and XML.

I will now walk you through the process of creating a pipeline that will use the Flat File Schema that we just created.

In the sample project that I am using, I have 1 "master" solution and 4 projects that belong to the solution. I have broken it down based upon a popular project structure:

  • MapsAndSchemas
  • Orchestrations
  • Pipelines
  • Helper (.Net Assembly used for any helper/utility methods)

So within my Pipelines project, I need to add a reference to my Schemas project. This will allow me to use this schema in my Disassembly/Assembly stages of my pipeline.

1. Add a "New Item" to your Pipeline project(or applicable project)



2. On the left portion of the dialog box, select "Pipeline Files", In the top centre of the dialog box, select "Receive Pipeline" and then provide a meaningful name in the bottom of the dialog box.



3. The Pipeline component designer will load. You will then want to drag a "Flat file disassembler" control from your toolbox and release it in the "Disassemble" stage.



4. With the "Flat file disassembler" control selected, select your Flat File schema that you previously created from the "Document schema" drop down.


5. You would follow the same steps when creating your Send pipeline. The difference being that you would be dropping a "Flat file assembler" control onto the "Assemble" stage in the pipeline. You would select the same Flat File schema from the "Document schema" drop down.


Once you have deployed your solution, you will want to ensure that you select your custom pipelines in the Receive Locations/Send Ports that you expect to receive/send these flat files.