Friday 19 February 2010

A Custom BizTalk File Adapter - BizTalk 2006 R2

The native FILE adapter released with BizTalk eats all zero-byte (empty) files without triggering any associated processes. This behaviour, according to Microsoft, is by design. Though you can argue that it is not consistent how empty files are treated by different adapters, e.g. FTP adapter can transfer empty files with no problem.

This behaviour has caused us grief after an upgrade of a third-party system. The system now creates an empty trigger file first without locking it. Then it grabs the file again and writes some data to it. So you see the problem here, the BizTalk FILE adapter may grab the file AFTER the system creates the empty file but BEFORE the system attempts to write to it.

What we want here is to have a custom file adapter that will ignore zero-byte files and leave them untouched.

After reading this good MSDN article, I got some idea and am ready to give it a go.

I go to the AdaptersDevelopment folder which is part of the BizTalk 2006 SDK.


Open the File Adapter folder.


Open the project file under the Runtime folder. I found DotNetFileReceiverEndpoint.cs is the file I'm interested in. The DotNetFile adapter is implemented as a Timer thread which checks the configured folder at the configured polling interval. To make it to ignore zero-bytes files, one change is required in the PickupFilesAndSubmit() method. I've made some other changes to suit my specific needs but the change highlighted below is the most important one.


I then open the Design Time folder, and edit the ReceiveLocation.xsd. This file defines the configuration properties that will display for the custom file adapter. I don't want to have some property to be mandatory so I change it to optional in the schema file.


Compile the Runtime and Design time projects. GAC the Microsoft.Samples.BizTalk.Adapter.Common.dll under C:\Program Files\Microsoft BizTalk Server 2006\SDK\Samples\AdaptersDevelopment\BaseAdapter\v1.0.2\bin\Debug.

Go back to the File Adapter folder, there are 2 .reg files. If you want to use the provided configuration UI, open StaticAdapterManagement.reg and make sure everything is correct or update the file paths to wherever the compiled DLLs are saved. Then simply double click DynamicAdapterManagement.reg to register the new custom file adapter. If you want to customize the property configuration UI, use DynamicAdapterManagement.reg instead.


Start up BizTalk Admin console, add the new adapter. The newly registered "Static DotNetFile" will appear in the Adapter dropdown list.



OK, the custom file adapter is all ready to be used! And it's not hard at all to re-brand the file adapter if you need to.

1 comment:

Anonymous said...

thanks for the useful blog.