Thursday, September 10, 2009

OutOfMemoryException on MemoryStream in Pipeline Component

On one of my projects, I was using a custom pipeline component to decompress a file in a send pipeline (*see side note on design decision at end of post).  In the production environment, we began to see pipeline failures caused by mscorlib OutOfMemoryExceptions in the pipeline component.  It was happening sporadically, so at first was not clear what was happening, though we knew the issue was occurring with growing frequency, roughly in correlation to the increase in the size of the file (the compressed file was a statement of account balances which grew as the number of accounts grew).

As many BizTalkers do, I use the SharpZipLib library for Zip compression (see Pro BizTalk 2006 by Dunphy and Metwally for a great example of this) and was taking the approach of loading the zipstream into a MemoryStream object.  The exception was being thrown in my loop which copied 4K segments into the stream.

After consulting my trusted advisor I saw a few discussions related to the need to provide a contiguous segment of memory for a MemoryStream to work.

I quickly inferred the likely culprit: I was trying to load data into a MemoryStream without the OS knowing how much memory to allocate it, so in many cases it was allocating a contiguous block that would not be enough for the uncompressed file.

Short term fix:

For the time being, I have simply updated my pipeline component to declare the size of the MemoryStream up front based on the zipstream’s Size property which is the number of bytes of the fiie uncompressed.

DANGER: the Size property is a Long, whereas you can only instantiate a MemoryStream with an Int32 (of course from the 2GB memory limit for 32-bit processes).  Knowing the file size will not grow beyond 1GB uncompressed, I have squished the long into an int, which of course is terrible.  Hence a long-term fix:

Long term fix:

Though I have yet to implement this, the sensible approach is instead to use something like the VirtualStream which will offload data to the filesystem if a stream exceeds a configured size, saving your poor BTSNTSvc.exe

Indeed hardware should never be used to mask up bad code, but it’s interesting to consider that this issue would likely not have arisen if it had been deployed on a 64-bit OS, which we can hopefully encourage all clients to do in the future.

 

* as a side note, the unzip was done in a send pipeline, in opposition to the usual approach of decompressing a file in a receive pipeline because the contents of the file were not needed.  All we wanted to do was route based on the file name, so by delaying the decompression we only needed to load a 16MB compressed file to the messagebox instead of a 700MB uncompressed file.

Thursday, August 27, 2009

Relative .snk file paths in Visual Studio 2008

I’m sure someone has already blogged on this, but as a reminder to myself and others, in Visual Studio 2008 it is not as straightforward to provide a relative path for a shared .snk file (for instance when signing multiple BizTalk projects with the same key file).

In the past, you could simply type in the path to the snk file in the project properties. Now this field is a dropdown and selecting browse will only let you find and copy the file to the root of the project.

image

To get around this, you can edit the project file (in the case of BizTalk, the .btproj). The following is an example where the .snk is both included as a shortcut in the project (not a copy) and uses the .snk for signing:

(Note: this project is the updated project schema for BizTalk 2009, so don't try to use this approach for BTS2006+VS2005)

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6092B853-D9B4-4C5E-B480-746703C74E80}</ProjectGuid>
<ProjectTypeGuids>{EF7E3281-CD33-11D4-8326-00C04FA0CE8D};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>library</OutputType>
<GenericProcessing>true</GenericProcessing>
<RootNamespace>Test.Orchestrations</RootNamespace>
<AssemblyName>Test.Orchestrations</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<BpelCompliance>True</BpelCompliance>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\Test.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System">
<Name>System</Name>
</Reference>
<Reference Include="System.Xml">
<Name>System.XML</Name>
</Reference>
<Reference Include="System.Configuration">
<Name>System.Configuration</Name>
</Reference>
<Reference Include="Microsoft.BizTalk.Pipeline">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Microsoft.BizTalk.DefaultPipelines">
<Name>Microsoft.BizTalk.DefaultPipelines</Name>
</Reference>
<Reference Include="Microsoft.BizTalk.GlobalPropertySchemas">
<Name>Microsoft.BizTalk.GlobalPropertySchemas</Name>
</Reference>
<Reference Include="Microsoft.BizTalk.TestTools">
<Name>Microsoft.BizTalk.TestTools</Name>
</Reference>
<Reference Include="Microsoft.XLANGs.BaseTypes">
<Name>Microsoft.XLANGs.BaseTypes</Name>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\Test.snk" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\BizTalk\BizTalkC.targets" />
</Project>

Just save the file and VS will reload the project showing “..\Test.snk” as the location of the strong name key file.

Friday, August 21, 2009

SysPreping a BizTalk development machine

Great info from MSDN on how to set up a development VM that can be shared with other developers in team scenarios. The scripts are included in the BizTalk SDK.

http://msdn.microsoft.com/en-us/library/dd792685%28BTS.10%29.aspx

Note that it mentions that WSS installations will likely need to be reconfigured to get working again following the sysprep. If WSS is required, considering either leaving WSS unconfigured or not installing until after sysprepping.

An error occurred when validating an AS2 message

In an AS2 BizTalk solution for a client, we began to see a number of errors in the event log and the behaviour was that no messages or MDNs would arrive from a particular third party:

We checked the certificate store and determined the certificate had not been revoked or expired. Searching the certificate store for the certificate to see where it was installed revealed that the certificate was no longer in the Other People store which is used by BizTalk for verifying signed messages. We found the resolution on our own, but after the fact noticed the following MSDN article which seems to be pretty thorough about resolving this issue. Note that our resolution is the last item on the "User Action" list:

http://msdn.microsoft.com/en-us/library/bb898960%28BTS.10%29.aspx

BizTalk and Dynamics AX 2009 AIF Integration

I'm taking the first steps down the road of integrating BizTalk and DAX. By many accounts, it's not the straightest road, but I'm up for the challenge :) I'll include any learnings on this as I go. In the mean time, here is where I have started on this road:

Microsoft Dynamics AX 2009 AIF BizTalk Adapter Configuration White Paper
http://www.microsoft.com/downloads/details.aspx?familyid=EDC62433-5B21-4F74-B065-B075BA6DC86D&displaylang=en

Latest BizTalk HotRod issue
http://biztalkhotrod.com/Documents/Issue7_Q3_2009.pdf