112417_1021_Authenticat1.png

Using the bitwise operator for subscriptions (part 2)

In part 1 I defined what the bitwise operator is and how you can use it in your .net code. Now, let us see how we can use the same functionality in BizTalk to route messages.

How to use  the bitwise operator in BizTalk

One of the great things about my job is that I get to move around a lot. Different customers has different uses for BizTalk and this leads to new ways to use it. This scenario is not unique, but was new to me anyway.

BizTalk receives a message and needs to route it to different receivers. The receivers will be assigned using some form of “lookup” based on the message type. Message type A must be sent to system 1, 2 and 4. Message type B must be sent to system 3 and 4, and so on.

As usual, the preferred solution makes no use of an orchestration, should be reconfigured without redeployment and adding a new receiver should be very easy and should only impact the current deployment in a minor way.

This is a good time to use the bitwise operator!

Setup in BizTalk

This is just a simple proof of concept and, as such not complete. Keep that in mind and please provide feedback if anything is missing.

Schema_thumb1The schema: The bitwise operator will solve the task. To use that, and also avoid using an orchestration, BizTalk has to be able to route messages based on a value in the file. In order to do that, a promoted property is used. In this case a field called “ReceiverField”.

It is important to remember that in this particular case, the promoted property must be an unsignedInt. This makes sense since you cannot use bitwise on a combination of positive and negative numbers. If you use Int, the operator will not show up in the administration console.

Receive port: Now set up the ports. First we need a receive location to pick up the incoming data. In this a simple file receive location is used, but there is nothing stopping you form using other protocols. The bitwise handling will still work. The important thing to remember is to use the xmlreceive pipeline (as opposed to the passthru).

Sendport for different systems: Now for the magic part. Add a sendport for each of the systems you need to send the information to. Make sure to configure filters to reflect the particular number you have assigned to that system.

FilterSendPort2_thumb

In the picture above, the filter is configured to send files received on the previously configured receive port and then the bitwise operator is used to look for the number two in the receiver field in the previously created schema.

Add another port for another system:

FilterSendPort_system3_thumb

The important thing to notice here is that there really is not any major difference. The only important difference is the number for the receiver field, which is not set to “4”.

Start all the ports create a testfile to be dropped in the receive folder. Set the value of the receiver field to “2”. Drop it in the receive folder. Only the send port marked with a “2” will pick it up.

Try again and change the value to “4”, only the second port marked with a “4” will pick it up. Lastly set the value to “6”, now both ports will pick it up!

The upsides

The most important gain is of course the routing but there are some other benefits as well. Firstly, you can put different format mapping for different systems in their respective ports. This is also true for pipelines, so if one of the systems is using custom formatting, that is not a problem.

Next, adding another system is simple as all you have to do is add another send port, and configure it to look for the next number (double the last).

The downsides

This logic cannot be used in orchestrations. You cannot use the bitwise operator as a part of an activation filter in the orchestration, sadly.

Another thing that might be a problem is the code, or handling, by which you assign the number in the “RecievierField”. If BizTalk is responsible for assigning this value, you have to make sure that adding another system is just as simple as adding another send port. Maybe you have a database that matches message types and assign the number. BizTalk can then simply use a database functiod in a map to look it up.

112417_1021_Authenticat1.png

Using the bitwise operator for subscriptions (part 1)

Did you know that there is a “bitwise” operator that you can use when you define subscriptions in BizTalk? I surely did not! I just stumbled upon it an since I could not find any really good article on it I thought I would give it a try:

What is the Bitwise operator?

This is certainly best answered by others; like wikipedia or this guy, or Microsoft of course.

If you want the short, short version; it is a good way to let one number represent a combination of other numbers, so that you know if a particular number is present… well that did not explain a lot. Let me try a longer version.

What problem does bitwise solve?

Bitwise is often used with enums in .net and if you want to use bitwise on an enum you mark it with the [Flags] attribute. The bitwise operator is a single ampersand (&), not to be confused with another logical operator (&&).

Imagine you are designing role based access management in a web application. You decide to have three different kinds of users (or roles); standard user, project managers and administrators.
The standard user has access to information about his/her current projects. The project manager can add/remove users to projects, and lastly the administrator can add projects and users.

When a web page is accessed, the user membership is checked and the information (perhaps in tabs) is displayed to the user depending on the user’s role.

How would you solve the fact that there might be users that are both standard users and project managers (depending on project)? Or, how about a user being administrator, project manager and standard user? How do you assign one number that combines different roles and give the user a particular access number?

Of course there are a lot of ways to do this but that does not prove my point, so I will assume that the way to do it is using [Flags] enum and the bitwise operator.

The enum values

To make this work you have to assign values to your enum in a specific way: The value of the enum is double that of the last value in the series, except for the second one, which is the number one.

enum MyRoles : uint
 {
   Anon = 0,
   StdUsr = 1,
   ProjectMgr = 2,
   Admin = 4
 }

So, a standard user has an access number of 1 and a project manager has the number 2. A user that is both a standard user and a project manager; the user gets an access number of 3 (1+2 = 3). This number is unique in the series and this will hold true for all combinations, even if you would have hundreds of roles.

Based on this logic, there is an easy way to find out if a user is in a particular role by using the &-operator. If you want to know if the user is a project manager, the code is simple:

if ((MyUser.AccessNumber &
      MyRoles.ProjectMgr) == MyRoles.ProjectMgr)

If the the expression returns true, the user is a project manager.

To be clear; there is a much simpler way of defining the same expression in .Net 4.0. Here; the bitwise operator is used under the covers.

if ( MyUser.AccessNumber.HasFlag(
     MyRoles.ProjectMgr))

In part 2 I will show you how to use Bitwise operations in BizTalk.