BizTalk and zero byte file

About this fact I wrote a blog post many years ago, but in Italian language, one friend ask me to translate it in English, after I read it I decide to write another post about this argument, because around it there are some important things to write, for example how make a simple new file adapter, or to understand the best method to create a custom BizTalk adapter and others many stuff.

You can download the entire article as a PDF document.
BizTalk and Zero Byte File

I assume that who read this post has good BizTalk knowledge.
The necessity to integrate a system that send files at zero byte is frequent, in these years I found this requirement many times, many big infrastructures use this method to warn the destination system that the transmission is ended, for example during AS400 file transmission, these file are usually called tap files.

BizTalk adapter file cannot receive these type of files, and the error returned is:

The FILE receive adapter deleted the empty file “C:mydirinNew Text Document.txt” without performing any processing.

To resolve this “issue” is very simple, many persons ignore an important BizTalk directory, the SDK directory.

In this directory Microsoft released all the most important BizTalk parts with source code, why?, but to resolve all these situations 🙂

The directory is under, C:%PrgFile%Microsoft BizTalk Server 2010SDK

And in this directory exist the source code of the BizTalk file adapter, HTTP adapter, , not only, you can find all the base class library to develop a BizTalk Adapter in few minutes.
One of the method to receive a file zero byte with BizTalk is write and adapter that can do it, and it is very simple.

Under directory C:%PrgFile%Microsoft BizTalk Server 2010SDKSamplesAdaptersDevelopmentFile Adapter
you can find all source code of BizTalk File Adapter,

clip_image001

The BizTalk adapter project architecture is simple, what is an adapter if not a .Net assembly called from a NT Service? 😉

In the directory you find ,BizTalk Project directory with one BTS sample, Design Time directory is a project to manage adapter configuration, Runtime diirectory is the project of adapter runtime.
Runtime project is the most important, open it, in this project you can observe the two principal files DotNetFileReceiverEndpoint.cs that contain the code for receive the stream and DotNetFileTransmitterEndpoint.cs that contain the code to transmit the stream.
In DotNetFileReceiverEndpoint.cs

If you want play with the adapter to understand how work a BizTalk adapter, you can point a break point in the CreateMessage function in DotNetFileReceiverEndpoint.cs file,

after you must attach the BizTalk process to start your debug, from VS 2010 menu, Debug / Attach Process, select Show process in all sessions, if you don’t find BTSNTsvc.exe, start the BizTalk Host Instance, if you have more than one, you must intercept the correct BizTalk process , for example with Process explorer of mythic Mark Russinovich

But to work better I suggest you to stop all Host Instances and use only once for the adapter and port

The first function called is PickupFilesAndSubmit and is called in polling mode, with or without files in the directory

clip_image002

As I said before the most important is CreateMessage,

clip_image003

You can download the entire article as a PDF document.
BizTalk and Zero Byte File

Observe what arrived in the adapter in this point,

clip_image004

The first is modify the type of file access fro read to write

clip_image005

This is the best point where you can operate, you can use your best strategy to manage this situation, for example to test if stream.length is > 0 or others, normally I use to test the length of the stream and for stream == 0 I assign one ZERO bite array.

For example

if (fs.Length == 0)

fs.Write(new byte[0], 0, 0);

clip_image006

Or you can put in the stream all your useful information to identify this type of file, for example, do you remember the BizTalk Schedule Adapter, now you can understand how it work :), at the correct time the adapter put in BizTalk the XML stream you have configured 🙂

Another always present issue during this type of operations is this error, see below,

The breakpoint will not currently be hit. No Symbols have been loaded for this document.

clip_image007

Is a normal error during a debug with a BizTalk process attached, rebuild your project, restart the Host instance and restart VS 2010
BizTalk and VS 2010 make a great battle to win the lock of the processes 🙂

Ok at this point you can rebuild your adapter and register it.

To register the adapter you must execute the registry file StaticAdapterManagement.reg, but before you must modify something, I found some possible issues in this file.
The first is the directories, check all path directory because all are wrong:

C:\Program Files\Microsoft BizTalk Server 2010\SDK\Samples\AdaptersDevelopment\FileAdapter\Runtime\bin\Debug\Microsoft.BizTalk.SDKSamples.Adapters.DotNetFile.Runtime.dll”

“OutboundEngineCLSID”=”{024DB758-AAF9

Is not FileAdapter but File Adapter

The second, check if your sample is under Program Files or Program Files (x86) in case modify the path or create your own different directory project.

The third and the most important is, if your BizTalk run in 64 bit mode you must modify the registry keys because BizTalk server in 64 bit search configuration under Wow6432Node

clip_image008

So, for example, if you want work with BizTalk configuration in 32 bit mode the correct string is
[HKEY_CLASSES_ROOTCLSID{62018D08-281A-415b-A6D3-6172E3762867}]
in 64 bit is

[HKEY_CLASSES_ROOTWow6432NodeCLSID{62018D08-281A-415b-A6D3-6172E3762867}]

In the BizTalk Admin console, right click on the adapters, new and select Static DotNetFile, for the adapter name as you want.

clip_image009

Restart the host instance

Create one receive port, one send with correct filter and test the solution

clip_image010

clip_image011

The result must be this

clip_image012

You can download the entire article as a PDF document.
BizTalk and Zero Byte File
  • You have posted a nice article. However, you have done the readers a disservice by not discussing the reasons the FILE adapter does not support zero-byte files. Understanding this may help readers avoid time on an adapter when there is another issue.

    The reason, as I am sure you know, is to handle file writers who do not keep the file handle open during the entire write operation. Many ftp programs are guilty of this as well as interfaces with non-Windows systems. The FILE adapter will not grab a file that is read-only or that is currently locked. It will process any that are closed and writable. That is the root cause of many problems.

    • ninocrudele

      Hi Brad, thank for compliment 🙂
      About this topic (zero file), I think that the file adapter doesn’t catch the zero file because there is not reason to enter a zero byte stream in a ESB, for this reason BizTalk doesn’t catch an error but only an information that is enter a zero byte stream and for this reason no process will be execute.

      In BizTalk there are different reasons to use FTP or File adapters, the File Adapter locked the file and this is the best reason to use this adapter in BizTalk cluster / NLB Group architecture.
      for example FTP adapter in cluster / nlb Group is very dangerous
      for others type of scenarios is better one or the other , depend from the architecture , requirements and solution

  • Liz

    Hi,
    I have a cenario where i have to pick up 0 kb files *& modify the content of file before it is picked by rcv pipeline.I am using xml receive pipeline here.Msg is getting suspended with error msg :-
    “No Disassemble stage components can recognize the data. ”

    But when i am resuming the suspended instsnce I am able to get back the required output in a folder.
    Is there any change that has to be done to polling or processing time??
    Please share your inputs.

    • What do you put in Body message in pipeline? check the content, in case send me your code without problem
      cheers,
      Nino

  • Dave

    Great article … as I need to provide the ability to respond to empty files …
    i have followed what you have set out but get a “The process cannot access the file … ” exception at the point the renamed file is being deleted in OnBatchComplete(bool overallStatus) … namely:

    foreach (BatchMessage batchMessage in messages)
    {
    //Close the stream so we can delete this file
    batchMessage.Message.BodyPart.Data.Close();
    File.Delete((string)batchMessage.UserData); <=== ERROR HERE
    }

    I then tried to run the original sample from th SDK and this also errors in the same place??

    Have you any experience of this?

    Any help would be much appreciated …

    Many Thanks,

    Dave

    • No any experience but BTW
      The following post uses the official FileAdapter sample source code created by Microsoft, I used this code many times and never seen this bug, can you send me more details about error stack?

  • Hi Nino,
    I tried to work following above article, but the input file name is repeat renamed.
    I saw in system log, the “SubmitMessage” method in the Batch.cs file that throw the exception
    “Cannot submit empty body or body with non-seekable stream”

    public void SubmitMessage (IBaseMessage message, object userData)
    {
    if( submitResponseMessageArray != null )
    throw new InvalidOperationException(“SubmitResponseMessage and SubmitMessage operations cannot be in the same batch”);

    // We need to have data (body part) to handle batch failures.
    IBaseMessagePart bodyPart = message.BodyPart;
    if (bodyPart == null)
    throw new InvalidOperationException(“The message doesn’t contain body part”);

    Stream stream = bodyPart.GetOriginalDataStream();
    if (stream == null || stream.CanSeek == false)
    {
    throw new InvalidOperationException(“Cannot submit empty body or body with non-seekable stream”);
    // ==> REPEAT THROW EXCEPTION AT HERE
    }

    this.transportBatch.SubmitMessage(message);
    if (null == this.submitArray)
    this.submitArray = new ArrayList();
    this.submitArray.Add(new BatchMessage(message, userData));

    workToBeDone = true;
    }
    Can you tell me the approach to resolve that issue?

    Thanks and regards.

    • Hi, the following post simply used the official FileAdapter sample source code created by Microsoft, you can find this under ROOT:Program Files (x86)Microsoft BizTalk Server YOUR VERSIONSDKSamplesAdaptersDevelopment.
      You only need to modify some parts reported in post, for example referring to (fs.Write(new byte[0], 0, 0);) and so on.

  • Hi, I’m using the FileAdapter sample source code from biztalk SDK then modify the CreateMessage method like you suggest.
    But if I change the file access from Read to Write, I got the error “The stream does not support reading”
    so, I changed the file access from Read to ReadWrite, Unfortunately that no luck, the input file renamed many times.
    I haven’t got any output file and the input file only rename, not delete.

    below are code lines in the CreateMessage method

    Trace.WriteLine(“[DotNetFileReceiverEndpoint] Renaming file ” + srcFilePath);
    File.Move(srcFilePath, renamedFileName);
    renamed = true;
    fs = File.Open(renamedFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);

    if (fs.Length == 0)
    fs.Write(new byte[0],0,0);

  • You write about how to receive a 0 byte file, but what about sending one? We were able to get a message with 0 data in it to a port and using a pipeline component, archive the 0 byte file, but the port never actually sent it out. Do you have any recommendations on how to do that?

    • Hi Rob, I don’t have any particular reccomendation, let me know if you find some problems

      • Any solution to sending a 0 Byte file? My customer wants this for his AS400 type something.

        • Hi Volker, I think the BTS adapter should be able to do, in case I would use a code approach

  • Pingback: Processing Zero Byte Files with BizTalk Server | Connected Pawns()

  • Pingback: BizTalk SB-Messaging Receive Adapter Suspends Brokered Messages Without a Body | Mind Over Messaging()

  • Rama Nathan

    i need the to send and receive a 0 Kb file in Biz talk can any one help plz

    • Hi Rama, it’s very simple, (go by memoery now…) just use the File adapter code in the SDK folder.
      You will find the instructions to install it.
      In the Runtime ReceiveHandler you will find the code line which ingest the stream, at the end of this line add + chr(0)
      Build and deploy

      Cheers
      Nino