Zip File in a Custom Send Pipeline Component Skip to main content

Zip File in a Custom Send Pipeline Component

Hello everyone !! I am going to write BizTalk custom send pipeline code to zip a file. What will you achieve after read out complete content of this post, you can create a custom send pipeline code and how add it in biztalk project with an project example so that you can easily able to convert any file into zip file by using send pipeline. In the last of this post you can download complete source code with an example.

Kindly follows all below following steps to achieve to send a zip file by using custom send pipeline component-
Before follows all steps, please read- BizTalk Pipeline
1. Open Microsoft Visual Studio as Administrator user and Click File>New>Project… In Installed Templates click BizTalk Projects then Empty BizTalk Server Project and Name as “BizTalkLive.Zip.Encode” and click OK.

2. Now leave every thing as it, we will do later, Now right click on your solution and a new C# class library project name as "BizTalkLive.Zip" then rename class1 as "Zip".

3. Add dll reference of Ionic for use zip method in pipeline Encode stage and also add reference Microsoft.BizTalk.Pipeline for use pipeline method. In the last of this post you are able to download Ionic.dll or It is open source library, download from some another 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. Now add following attribute and GUID (it is unique value of across network in the word) You can create your own unique GUID or use already that is already created, just click on Tools menu in VS then click on Create GUI, see the following picture-namespace BizTalkLive.Zip
{
    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
    [System.Runtime.InteropServices.Guid("6F31BA70-F87E-4150-B5A5-2803D00C4FA0")]
    [ComponentCategory(CategoryTypes.CATID_Encoder)]

5. Now use interface IBaseComponent,IPersistPropertyBag,IComponentUI,IComponent in Zip class. As I understand that you know basic concept of IBasecomponent, IComponentUI, IComponent, IPersistPropertyBag so now come to final point IComponent stage.
 
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<string, Byte[]> lst = new Dictionary<string, Byte[]>();

                lst.Add(fileName, TextFileBytes);

                MemoryStream ms;

                using (ZipFile zip = new ZipFile())
                {
                    foreach (KeyValuePair<string, Byte[]> item in (IDictionary<string, Byte[]>)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 above code I am using IBaseMessageContext interface try to get received file name.

6. In the last right click on same project BizTalkLive.Zip then click on Property after click on sign tag and then add new strong name file. Now right click same project and build it.

7. Now go to folder location and go to bin\Debug and installed Ionic and Zip dll into gac. You can reference how to install dll into gac - Install DLL into GAC

8. Now Create new biztalk project within same solution and name as "BizTalkLive.Zip.Encode" then reference dll from gac i.e. BizTalkLive.Zip  in biztalk project , see the following image-

9. Add new send pipeline name as "sndZipEncode" now Click on toolbox of send pipeline and then right click and click choose items.. and select BizTalk Pipeline Component tag and click Browse.. button from right side of bottom and select BizTalkLive.Zip.dll from the already installed into gac assembly. Now a new toolbox added in pipeline toolbox name as "ZipEncoder" then select it and place on encode stage of pipeline. see the following figure-


10. Now add one schema for xml file that xml file zip at send port, Please see the following picture and also you can download complete source in the last of this post so that you can easily create schema.

11. Now see the following below picture its show how I am design orchestration flow, firstly message come from receive location then I am using a expression shape where i log a status to system application event log. In last, configure logical send port  to send XML file in local directory.

12. In the last part of biztalk project zip custom send pipeline, right click on solution and set same name of biztalk project as it solution name and again right click on solution and deployed it. Now create receive port then create receive location and select PassThruReceive in receive pipeline then click on Configure button and set receive folder path and click OK then again OK.

13. Now create two send port one for route same xml file to local directory by using filter condition of BTS property of receive port name and second send port enlist in orchestration logic send port, here I have selected custom send pipeline that we have created in biztalk project so that it converted file into zip format. Please see the following picture-

To download Ionic dll please click this link -  Downlaod Ionic Dll
To download complete project source code please click this link -  Download Source Code 

!! Keep Visiting BizTalkLive !!

Comments

  1. I have a scenario where need to zip multiple files into one zip file , can this be done in your pattern ?

    ReplyDelete
    Replies
    1. No, you need extra logic in pipeline C# code to zip multiple file.

      Delete

Post a Comment

Please write comment only that belongs to this blog

Popular posts from this blog

BizTalk Interview Question and Answer

1. What is BizTalk? BizTalk is a middleware that sits in the middle of any two software who wish to communicate with each other and agree on some specified communication pattern. It uses SQL Server as back end database. “Microsoft BizTalk Server is an Inter-Organizational Middleware System (IOMS) that enables companies to automate business processes, through the use of adapters which are tailored to communicate with different software systems used in an enterprise. Created by Microsoft, it provides enterprise application integration, business process automation, business-to-business communication, message broker and business activity monitoring.”                 2. What is BizTalk Server Architecture and Life Cycle of Message? BizTalk Server Architecture: Life Cycle of Message: In this simplified view, a message is received through a receive location defined...

Configuring BizTalk WCF Timeout Values on a Binding

There are a number of timeout settings available in WCF bindings. Setting these timeout settings correctly can improve not only your service’s performance but also play a role in the usability and security of your service. The following timeouts are available on WCF bindings: OpenTimeout CloseTimeout SendTimeout ReceiveTimeout Open Timeout :  This property represents the amount of time a channel open operation has to complete. Send Timeout :  Use this property to set the amount of time that a send operation has to complete. When used as part of a solicit-response scenario, this value encompasses the total amount of time for the interaction to complete. If we are sending a large message, we may need to increase this timeout to allow for the request and response messages to be processed within this window. Close Timeout :  A time-stamp that is used to indicate the amount Receive Timeout :  Used by the Service Framework Layer to initialize the...

PowerShell Script to enable BizTalk Receive Location

We are facing problem in BizTalk server 2016 that few of running SFTP Receive location would be stopped after some time due to sftp connectivity issue of their threshold so I am going to share PowerShell script to enable receive location by window task scheduler. Before execute following script Please do following task a. Set Server name in ConnectionString b. Set ( $hostname) host name value that is using in SFTP Receive location c. Set ( $rcvLocation) Receive location name # Import external assembly and create a new object [ void ] [ System.reflection.Assembly ]:: LoadWithPartialName( "Microsoft.BizTalk.ExplorerOM" ) $Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer   #BizTalk Config $Catalog . ConnectionString = "SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI"   #connectionstring for the BizTalkMgmtDb $hostname = "BizTalkServerApplication" #hostname $rcvLocation = "RL_GetAccReq_SQL...

Publish a web service in biztalk using BizTalk WCF Service Publishing Wizard

Welcome you again in my Blog and let us do another mind blowing achievement that how to publish a WCF web service in BizTalk and as in previous post to Complete BizTalk project to host Schema as Web Service  so If you want to expose/publish a web service, you should run through the BizTalk WCF Service Publishing Wizard. This wizard guides you through choosing what you want to expose as a web service. This section walks you through the wizard step by step to publish WCF Web Service using  WCF-WSHttp- Step-1:  To start the wizard, go to Start, All Programs, Microsoft BizTalk Server 2016(version), BizTalk WCF Service Publishing Wizard. This fires up a welcoming screen, on which you just click Next. Consider disabling the welcoming screen for the future by checking the check box at the bottom. You will now see the screen shown in above figure. Step-2: Choose the Service Endpoint option, and choose which adapter to use. Check Enable Metadata Endpoint if you wa...

Step by step solution of BizTalk Two-Way WCF-SQL XmlPolling in Send Port

I am going to share most awaited Two way WCF_SQL Two Way XmlPolling so that you can easily enhance your old SQL procedure and BizTalk Map without any big change. I know there is no content available in any website to how work with XmlPolling in WCF-SQL two-way adapter. In the last of this post you can download complete source code with an example. Please follow carefully all steps – 1. Open Microsoft Visual Studio as Administrator user and Click File>New>Project… In Installed Templates click BizTalk Projects then Empty BizTalk Server Project and Name as “BizTalkLive-WCF-SQL” and click OK. 2. Now see the below picture and create all respective folders separately for Schema, Map, Orchestration, Pipeline, Binding- 3. Create Schemas in Schema folder as shown below picture to get Account request using One Way WCF-SQL XmlPolling a) Create Envelope Schema first with Target NameSpace - http://BizTalkLive_WCF_SQL.AccountRequest b) Create Second Sch...