Connected Thoughts – Thiago Almeida

April 25, 2008

BizTalk Server Tutorials Updated

Filed under: BizTalk — Tags: — Thiago Almeida @ 6:14 pm

 Microsoft released some great tutorials after BizTalk Server 2006 was released, and also updated them with BizTalk R2. At the beginning of this month they seem to have reviewed these tutorials, so what better time to go over them if you want to learn it in step-by-step instructions? They implement the most common BizTalk solutions, and the download page is here. The tutorials source files were shipped with BizTalk Server R2 in the SDK, so make sure you read the “Before You Begin” instructions first to unpack them into c:\Tutorials. The files in “updated tutorial files.exe” are updates to some of the files that were shipped with R2.

 Here is an excerpt of the tutorials from the download page:

Tutorial 1: Enterprise Application Integration.
Provides step-by-step instructions for implementing a BizTalk solution that receives inventory replacement request messages from a warehouse and evaluates the request messages. If the solution denies a request, it sends a denied message to the warehouse. If the solution approves a request, it forwards the message to an Enterprise Resource Planning (ERP) system.

Tutorial 2: Purchase Order Process.
Provides step-by-step instructions for implementing a BizTalk solution for the ERP system that generates purchase orders from the request messages and sends them to an external partner.

Tutorial 3: Invoice and Payment Process
Provides step-by-step instructions for extending the solution you create in Tutorial 2 to incorporate a payment process that includes verifying that the partner received the purchase order, receiving the invoice from the partner, constructing a payment voucher for the partner using rules to determine the payment policy, sending the payment and receiving confirmation that the partner received the payment, and forwarding the payment acknowledgment internally.

Tutorial 4: Trading Partner Management.
Provides step-by-step instructions for updating an existing BizTalk solution to incorporate trading partner management. You use the solution to implement a trading partner relationship.

Tutorial 5: Business Activity Monitoring.
Provides step-by-step instructions for defining and viewing data of interest.

April 21, 2008

Get the size of an XLANG message

Filed under: BizTalk — Tags: — Thiago Almeida @ 3:48 pm

Here’s a little snippet of code I use to get the size of an XLANG message in bytes. Very efficient and always correct, it loops through each message part and adds up each part’s Size property. This is useful for when you need to know the size of the message inside an orchestration.

 

 //Returns the sum of the size of the message parts
 public static int GetMessageSize(Microsoft.XLANGs.BaseTypes.XLANGMessage msg)
 {
   int msgSize = 0;
   try
   {
     foreach (Microsoft.XLANGs.BaseTypes.XLANGPart xp in msg)
     {
       msgSize += Convert.ToInt32(xp.GetPartProperty(typeof(Microsoft.XLANGs.BaseTypes.Size)));
     }
   }
   catch (Exception ex)
   {
     System.Diagnostics.EventLog.WriteEntry("XMLUtils GetMessageSize",
     ex.Message, System.Diagnostics.EventLogEntryType.Error);
   }
 
   return msgSize;
 }

April 18, 2008

Microsoft BizTalk RFID on Mobile Devices

Filed under: BizTalk, RFID — Tags: — Thiago Almeida @ 2:56 pm

 BizTalk RFID v1.1 (i.e. BizTalk RFID Mobility and Standards Pack) is currently on Technology Adoption Program on the connect website.
 The Microsoft RFID team have been working on this for quite a few months and it is the perfect addition to the current RFID solution. Mobile devices and RFID go hand in hand with applications like asset tracking and many others.
 Have a look at this post by the Connected Systems Chilled Out Blog for more details on what it has to offer.

April 17, 2008

Ellerslie .NET Usergroup MIX ‘08 Overview with Nigel Parker

Filed under: BizTalk — Tags: , , , , , , , — Thiago Almeida @ 1:35 pm

 The MIX conference happens every year and its latest installment was this March at the Venitian (NICE!) in Las Vegas. It’s hosted by Microsoft and they go over their latest and soon to be released web technologies.
 I attended a great Ellerslie .NET Usergroup meeting two days ago in Auckland, where Nigel Parker (Microsot NZ’s User Experience Development Advisor) went over some of the new stuff being release by Microsoft over the next few months.
 I rarely develop web applications, but it was very interesting to see how advanced some of these technologies are getting. While he went through some interesting tools such as side by side Silverlight development with Visual Studio 2008 and Expression Studio (version 2 of which to be released at the end of this month), the technology he spent a lot of time on and which was very interesting to me given my photography interest was Deep Zoom. Deep Zoom allows you to create a collection or montage of images of any resolution, and users can zoom in and out to them. It streams the parts of the image you’re currently seeing, much like Google Earth. You can see how useful this can be when a Deep Zoom solution is exposed by Silverlight 2.0 over the web, and its uses are endless. If you want to see how cool this is check out this post from Nigel for a quick sample and links to other sites. This will be a really interesting technology when it becomes possible to dynamically load the pictures you want to use. Imagine, an application with Deep Zoom capabilites that loads some dynamic photos from Flickr using the Flickr API?

Also, follow this link to get all the technologies presented at MIX ‘08.

By the way, if you live in New Zealand and work with .NET or other Microsoft technologies, make sure you’re part of the .NET User Group mailing lists and keep track of the events.
Also, in Auckland, subscribe to the mailing list of the very active Ellerslie .NET Usergroup to be informed of future meetings.
Now back to BizTalk and WCF!

April 2, 2008

3 ways of programmatically extracting a message body from the BizTalk tracking database

Filed under: BizTalk — Tags: — Thiago Almeida @ 10:19 am

 I know often people run into the need to extract a message from the BizTalk tracking database. I thought I’d write a post on the 3 methods I’ve found so far over time.
 As some of you know the body and context of messages in BizTalk are compressed, and rightfully so. The actual compression and decompression code is hidden inside a dll called BTSDBAccessor.dll. This DLL, the “BizTalk Database Acessor”, is unmanaged and does a lot of work for BizTalk including accessing the BizTalk databases to send and receive messages.
 Based on blog sites and forums I’ve run into over time I know of three ways we can programmatically get a message body out of the tracking database: Operations DLL, SQL, and WMI. I’ve created a little test C# Windows application that uses all three.
 
  The application has only one form (print-screen below), and expects the following parameters:
   – The message guid of the message you want to extract.
   – The extraction type (Use Operations DLL, Use SQL, Use WMI)
   – Tracking DB server (the BizTalk server name)
   – Tracking DB name (the BizTalk Tracking database name)
  

 Get BizTalk Tracked Message

  Here is the code for the “Get Message” button:

        private void btnGetMessage_Click(object sender, EventArgs e)
        {
            txtMessage.Clear();
            txtMessage.Refresh();
            GuidmessageGuid;
            try
            {
                messageGuid = new Guid(txtGuid.Text);
            }
            catch (ExceptionexGuid)
            {
                txtMessage.Text = "Please enter a valid Guid. Error: "+ exGuid.Message;
                return;
            }
 
            switch(cboGetType.SelectedIndex)
            {
                case 0:
                    txtMessage.Text = GetMessageWithOperations(messageGuid);
                    break;
                case 1:
                    txtMessage.Text = GetMessageWithSQL(messageGuid);
                    break;
                case 2:
                    txtMessage.Text = GetMessageWithWMI(messageGuid);
                    break;
                default:
                    break;
            }
        }

  Now, let’s finally see the three ways of getting to the message body, shall we?

  1. My favourite, use the Microsoft.BizTalk.Operations dll. This is pretty straight forward, you add a reference to Microsoft.BizTalk.Operations.dll and use the GetTrackedMessage of the BizTalkOperations class. You can also get to the message context using this method. This is only for BizTalk 2006 and later. Here is the code:

        //Retrieves the message using the operations DLL - Add Microsofr.BizTalk.Operations.dll to references
        public string GetMessageWithOperations(GuidMessageInstanceId)
        {
            try
            {
                TrackingDatabasedta = new TrackingDatabase(txtTrackingDBServer.Text, txtTrackingDBName.Text);
 
                BizTalkOperations operations = new BizTalkOperations();
                IBaseMessagemessage = operations.GetTrackedMessage(MessageInstanceId, dta);
                string body = string.Empty;
                using (StreamReaderstreamReader = new StreamReader(message.BodyPart.Data))
                {
                    body = streamReader.ReadToEnd();
                }
 
                return body;
 
            }
            catch (ExceptionexOp)
            {
                return "Failed to get message with id "+ MessageInstanceId.ToString() + " from tracking database: "+ exOp.Message;
            }
        }

Here’s the great post mentioning this method by Richard Hallgren:
http://www.richardhallgren.com/reading-the-message-body-and-context-from-the-biztalkdtadb-using-operations-library-in-biztalk-2006/
2. Use the WMI MSBTS_TrackedMessageInstance.SaveToFile method to save the instance to disk. This was the popular method in BizTalk 2004 since there was no operations dll then. Here is the code:

        //Uses WMI to save the tracked message out to a file folder using MSBTS_TrackedMessageInstance class
        public stringGetMessageWithWMI(GuidMessageInstanceId)
        {
            try
            {
 
                // Construct full WMI path to the MSBTS_TrackedMessageInstance using the message guid (NOTE: MessageInstanceID string value must be enclosed in {} curly brackets)
                string strInstanceFullPath = "\\\\.\\root\\MicrosoftBizTalkServer:MSBTS_TrackedMessageInstance.MessageInstanceID='{"+ MessageInstanceId.ToString() + "}'";
 
                // Load the MSBTS_TrackedMessageInstance
                ManagementObject objTrackedSvcInst = new ManagementObject(strInstanceFullPath);
 
                // Invoke "SaveToFile" method to save the message out into the specified folder
                objTrackedSvcInst.InvokeMethod("SaveToFile", new object[] {Application.StartupPath});
 
                //Get all files in the directory starting with this messageid
                string[] files = Directory.GetFiles(Application.StartupPath, "{"+ MessageInstanceId.ToString() + "*.*");
 
                string message = "";
                foreach (string file in files)
                {
                    if(file.EndsWith(".out"))
                    {
                        using (StreamReadersr = new StreamReader(file))
                        {
                            message = sr.ReadToEnd();
                        }
                    }
                }
 
                foreach (string file in files)
                {
                    System.IO.File.Delete(file);
                }
 
                if (files.Length == 0)
                {
                    throw new Exception("No files found on folder that match the GUID");
                }
 
                return message;
 
            }
            catch (ExceptionexWMI)
            {
                return "Failed to save tracked message with id "+ MessageInstanceId.ToString() + " into folder " + Application.StartupPath + ": "+ exWMI.Message;
            }
        }

http://kentweare.blogspot.com/2007/05/biztalk-retrieving-tracked-messages.html

  3. The bts_GetTrackedMessageParts stored procedure inside the tracking database expects the message GUID and will return the compressed message data back. We can then use reflection to invoke the Decompress method of the Microsoft.BizTalk.Message.Interop.CompressionStreams class inside Microsoft.BizTalk.Pipeline.dll to decompress the data returned from SQL. Here is the code:

        //Calls BizTalk stored procedure to retrieve compressed message and decompresses it
        public stringGetMessageWithSQL(GuidMessageInstanceId)
        {
            try
            {
                //Connection to DTA database on localhost
                SqlConnection con = new SqlConnection("Data Source=" + txtTrackingDBServer.Text + ";Initial Catalog=" + txtTrackingDBName.Text + ";Integrated Security=True");
                string message = "";
 
                try
                {
 
                    SqlCommandcmd = new SqlCommand();
                    SqlDataReader reader;
 
                    //Build execution of stored procedure bts_GetTrackedMessageParts
                    cmd.CommandText = "bts_GetTrackedMessageParts";
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameterguidParameter = new SqlParameter("@uidMsgID", SqlDbType.UniqueIdentifier);
                    guidParameter.Value = MessageInstanceId;
                    cmd.Parameters.Add(guidParameter);
                    cmd.Connection = con;
 
                    con.Open();
 
                    reader = cmd.ExecuteReader();
 
                    //Get the reader to retrieve the data
                    while (reader.Read())
                    {
                        //Use memory stream and reflection to get the data
                        SqlBinarybinData = new SqlBinary((byte[])reader["imgPart"]);
                        MemoryStream stream = new MemoryStream(binData.Value);
                        AssemblypipelineAssembly = Assembly.LoadFrom(string.Concat(@"C:\Program Files\Microsoft BizTalk Server 2006", @"\Microsoft.BizTalk.Pipeline.dll"));
                        TypecompressionStreamsType = pipelineAssembly.GetType("Microsoft.BizTalk.Message.Interop.CompressionStreams", true);
                        StreamReader st = new StreamReader((Stream)compressionStreamsType.InvokeMember("Decompress", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new object[] { (object)stream }));
                        message = st.ReadToEnd();
                    }
                }
                finally
                {
                    con.Close();
                }
 
                return message;
 
            }
            catch (ExceptionexSQL)
            {
                return "Failed to get message with id "+ MessageInstanceId.ToString() + " from tracking database: "+ exSQL.Message;
            }
        }

  This is the post that I ran into that helped me with this method:
  http://www.tech-archive.net/Archive/BizTalk/microsoft.public.biztalk.general/2007-05/msg00124.html
 
  Hope this helps someone! Post a comment (and fill out the email address field) or use the “Contact” page if you want the source code, but it’s pretty much all here in this post.

Dowload the sample here.

Blog at WordPress.com.