BizTalk Pipeline Component Error: "Object reference not set to an instance of an object"
- by Stuart Brierley
Yesterday I posted about my BizTalk Archiving Pipeline Component, which can be found on Codeplex if anyone is interested in taking a look.
During testing of this component I began to encounter an error whereby the component would throw an "Object reference not set to an instance of an object" error when processing as a part of a Custom Pipeline.
This was occurring when the component was reading a ReadOnlySeekableStream so that the data can be archived to file, but the actual code throwing the error was somewhere in the depths of the Microsoft.BizTalk.Streaming stack.
It turns out that there is a known issue where this exception can be thrown because the garbage collector has disposed of of the stream before execution of the custom pipeline has completed.
To get around this you need to add the streams in your code to the pipeline context resource tracker.
So a block of my code goes from:
originalStrm = bodyPart.GetOriginalDataStream();
if (!originalStrm.CanSeek)
{
ReadOnlySeekableStream seekableStream = new ReadOnlySeekableStream(originalStrm);
inmsg.BodyPart.Data = seekableStream;
originalStrm = inmsg.BodyPart.Data;
}
fileArchive = new FileStream(FullPath, FileMode.Create, FileAccess.Write);
binWriter = new BinaryWriter(fileArchive);
byte[] buffer = new byte[bufferSize];
int sizeRead = 0;
while ((sizeRead = originalStrm.Read(buffer, 0, bufferSize)) != 0)
{
binWriter.Write(buffer, 0, sizeRead);
}
to
originalStrm = bodyPart.GetOriginalDataStream();
if (!originalStrm.CanSeek)
{
ReadOnlySeekableStream seekableStream = new ReadOnlySeekableStream(originalStrm);
inmsg.BodyPart.Data = seekableStream;
originalStrm = inmsg.BodyPart.Data;
}
pc.ResourceTracker.AddResource(originalStrm);
fileArchive = new FileStream(FullPath, FileMode.Create, FileAccess.Write);
binWriter = new BinaryWriter(fileArchive);
byte[] buffer = new byte[bufferSize];
int sizeRead = 0;
while ((sizeRead = originalStrm.Read(buffer, 0, bufferSize)) != 0)
{
binWriter.Write(buffer, 0, sizeRead);
}
So far this seems to have solved the issue, the error is no more, and my archive component is continuing its way through testing.