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.
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

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

Related blog posts