Monday, October 26, 2009

XamlParseException when no AssemblyInfo.cs file available

In my project I have an exe and a dll assembly. The App.xaml in the exe references a ResourceDictionary in the dll assembly. This had worked for a long time, but all of a sudden it stopped working. The following very frustating error showed up during runtime:

System.Windows.Markup.XamlParseException: Could not load file or assembly 'MyAssembly.MyResource, Version=0.0.0.0, Culture=neutral, PublicKeyToken=1cf5186c6493e5b0' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Error in markup file 'MyAssembly.MyResource;component/MyResource.xaml'. ---> System.IO.FileLoadException: Could not load file or assembly 'Pulsim.UnitConversions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=1cf5186c6493e5b0' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Strange thing is, that my compiled MyAssembly gets version 1.0.0.125.

So what this error basically says is: “I’m not able to load my self, because the version is incorrect.”

After several attempts to resolve this issue, I finally got to my build procedure which is based upon msbuild. I use CommunityTasks in combination with an automatic generation of AssemblyInfo.cs. This way makes it possible to include the SVN revision number in the assembly and file version. How to do that can be found here: http://rod.blogsome.com/2008/02/21/compiling-solution-in-visual-studio-with-generating-project-version-based-on-svn-revision/.

That article states that the following addition (among others) must be made to the .csproj file.

   1: … cut …
   2: <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   3: <Import Project="$(RootPath)\Common.proj" />

I finally switched line 2 and 3 in order to get the AssemblyInfo.cs file generated before the XAML parser/compiler kicks in. And guess what: it worked!

Summary: Be sure that an AssemblyInfo.cs file exists and is referenced within your project if you want to use XAML ResourceDictionaries!

No comments: