Custom Send Pipeline for Zip File in BizTalk

Hello everyone! In this post, I will be discussing how to write custom send pipeline code for BizTalk to zip a file. By the end of this post, you will be able to create custom send pipeline code and add it to your BizTalk project. I will provide an example that will help you easily convert any file into a zip file using the send pipeline. In the last section of this post, you will also be able to download the complete source code with an example.

Please follow the steps below to learn how to send a zip file using a custom send pipeline component:

1. Open Microsoft Visual Studio as an Administrator user, click on File, then New, then Project. In the Installed Templates section, click on BizTalk Projects, then choose Empty BizTalk Server Project. Name it “BizTalkLive.Zip.Encode” and click OK.

2. Leave everything else as is for now. Right-click on your solution and create a new C# class library project named “BizTalkLive.Zip”. Then, rename class1 as “Zip”.

3. Add a DLL reference of Ionic so that you can use the zip method in the pipeline Encode stage. Also, add a reference to Microsoft.BizTalk.Pipeline so that you can use pipeline method. You can download the Ionic.dll from an open source library website.

using Ionic.Zip;

using Microsoft.BizTalk.Component.Interop;

using Microsoft.BizTalk.Message.Interop;

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

4. To add a unique identifier (GUID) to your project, follow these steps: 1. Open Visual Studio and select the Tools menu. 2. Click on Create GUID to generate a unique identifier. 3. Copy the generated GUID and add it as an attribute in your code. This GUID is unique across all networks and helps identify your project.

 

namespace BizTalkLive.Zip

{

    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]

    [System.Runtime.InteropServices.Guid(“6F31BA70-F87E-4150-B5A5-2803D00C4FA0”)]

    [ComponentCategory(CategoryTypes.CATID_Encoder)]

5. Use the IBaseComponent, IPersistPropertyBag, IComponentUI, IComponent interfaces in the Zip class.

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)

        {

           

            IBaseMessageContext context = inmsg.Context;

            string fileName = string.Empty;

            object obj = context.Read(“ReceivedFileName”“http://schemas.microsoft.com/BizTalk/2003/file-properties”);

            fileName = ((string)obj).Substring(((string)obj).LastIndexOf(“\\”) + 1);

 

            Byte[] TextFileBytes = null;

            IBaseMessagePart msgBodyPart = inmsg.BodyPart;

            //Creating outMessage

            IBaseMessage outMessage;

            outMessage = pc.GetMessageFactory().CreateMessage();

 

            if (msgBodyPart != null)

            {

                outMessage.Context = PipelineUtil.CloneMessageContext(inmsg.Context);

                Stream msgBodyPartStream = msgBodyPart.GetOriginalDataStream();

                TextFileBytes = ReadToEnd(msgBodyPartStream);

                outMessage.AddPart(“Body”, pc.GetMessageFactory().CreateMessagePart(), true);

 

 

                IDictionary<stringByte[]> lst = new Dictionary<stringByte[]>();

 

                lst.Add(fileName, TextFileBytes);

 

                MemoryStream ms;

 

                using (ZipFile zip = new ZipFile())

                {

                    foreach (KeyValuePair<stringByte[]> item in (IDictionary<stringByte[]>)lst)

                    {

                        zip.AddEntry(item.Key, item.Value);

 

                    }

                    ms = new MemoryStream();

                    ms.Seek(0, SeekOrigin.Begin);

                    zip.Save(ms);

                }

 

                ms.Position = 0;

                outMessage.BodyPart.Data = ms;

                outMessage.Context.Promote(“ReceivedFileName”“http://schemas.microsoft.com/BizTalk/2003/file-properties”, fileName.Substring(0,fileName.IndexOf(“.”)));

            }

            return outMessage;

        }

In the above code, I am using the IBaseMessageContext interface to retrieve the name of the received file.

6. To continue, right-click the BizTalkLive.Zip project and select Properties. From there, click on the Signing tab and add a new strong name file. Once this is done, right-click on the same project and select Build.

7. Next, navigate to the bin\Debug folder location and install the Ionic and Zip DLLs into the gac. You can find instructions on how to install a DLL into the gac in this article – Install DLL into GAC.

8. Now, create a new BizTalk project within the same solution and name it “BizTalkLive.Zip.Encode”. Reference the DLL from the gac (BizTalkLive.Zip) in the BizTalk project, as shown in the following image:

9. To add a new send pipeline named “sndZipEncode“, first click on the toolbox of the send pipeline. Next, right-click and select “Choose Items”. From there, select the “BizTalk Pipeline Component” tag and click the “Browse” button located on the bottom right. Choose “BizTalkLive.Zip.dll” from the already installed GAC assembly. Once completed, a new toolbox named “ZipEncoder” will be added to the pipeline toolbox. Select it and place it on the encode stage of the pipeline. Please refer to the following figure for guidance.
10. To proceed, please create a schema for an XML file that is zipped at the send port. You can refer to the image below for guidance, and if needed, you can download the complete source from the last section of this post to help you create the schema easily.
11. Please take a look at the picture below, which shows how I have designed the orchestration flow. The incoming message is received at a receive location, followed by an expression shape where I log a status to the system application event log. Finally, I configure a logical send port to send the XML file to a local directory.

12. To complete the biztalk project zip custom send pipeline, follow these steps: 1. Right-click on the solution and set the same name of the biztalk project as its solution name. 2. Deploy the solution by right-clicking on it. 3. Create a receive port and a receive location. 4. Select PassThruReceive in the receive pipeline and click on the Configure button. 5. Set the receive folder path and click OK. 6. Create two send ports. 7. Use the filter condition of BTS property of receive port name to route the same XML file to the local directory in the first send port. 8. Enlist the second send port in orchestration logic send port. 9. Select the custom send pipeline that was created in the biztalk project to convert the file into a zip format.

Refer to the following picture for more clarity.

To download the Ionic DLL, please click on this link: Download Ionic DLL

Additionally, you can download the complete project source code by clicking on this link: Download Source Code

Post a comment

Leave a Comment

Scroll to Top