Tuesday, September 28, 2010

The type initializer for 'NHibernate.ByteCode.LinFu.ProxyFactory' threw an exception.

The type initializer for 'NHibernate.ByteCode.LinFu.ProxyFactory' threw an exception.

Today most of my time was spent around the Exception figuring out what made this error to be thrown by nhibernate. It was weird as the application was compiling without any warnings or Errors, and, was running without any problems till it tried to access the database.
Exception thrown was not helpful at all as it said “object reference not set….. “And there weren’t any inner exceptions as well.

Therefore the exception handling in the application was improved by including the Build session factory method in side an exception block. When the error was logged it said “The type initializer for 'NHibernate.ByteCode.LinFu.ProxyFactory' threw an exception”. The next question was how come ? as my code never accessed the LinFu proxy factory directly and it was part of the nhibernates business to do so.

In fact the problem for me was that as the proxy is being loaded dynamically, Visual Studio does not copy all the dependencies properly to the bin dir. or bin\debug dir.
Simply I had to manually copy the nhibernate dependencies to the bin folder. This fixed the problem, though it’s weird that the LinFu Proxy is not copied to the bin folder even though there is a reference from my Data Layer.

Sunday, September 26, 2010

The referenced assembly "some.dll" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=

The referenced assembly "some.dll" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.

This annoying exception tormented me for almost a day, while I was developing and WPF - nhibernate application using VS 2010.

By the way the error had nothing to do with either nhibernate or WPF. As I found out this was due to the more compact version of the .NET framework (client profile) introduced with VS2010.

Resolution – By Default the desktop client applications like Windows Forms and WPF applications) targets .NET Framework (Client profile) as client profile is just a subset of the .NET framework introduced to improve the deployment experience. Therefore re-target your applications to Full .NET framework instead of the compact framework to get rid of the Error. As you can see the error pops up only when you refernce assemblies like System.Web, System.Data.OracleClient etc. which are not part of the Compact framework.

There is a good article on the two Frameworks here: http://blogs.msdn.com/b/jgoldb/archive/2009/10/19/what-s-new-in-net-framework-4-client-profile-beta-2.aspx