Could not load file or assembly Castle.DynamicProxy2

502 views
Skip to first unread message

jimmarq

unread,
Apr 27, 2009, 1:16:45 PM4/27/09
to Fluent NHibernate
I'm getting an exception at the following line using the r453
binaries:

_sessionFactory = Fluently.Configure()
.Database
(SQLiteConfiguration.Standard.UsingFile("nhibernate.db"))
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Product>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();

Main Exception: "An invalid or incomplete configuration was used while
creating a SessionFactory. Check PotentialReasons collection, and
InnerException for more detail."

InnerException: {"Could not load file or assembly
'Castle.DynamicProxy2, Version=2.0.3.0, Culture=neutral,
PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The
system cannot find the file specified.":"Castle.DynamicProxy2,
Version=2.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc"}

{"The type initializer for
'NHibernate.Proxy.Poco.Castle.CastleProxyFactory' threw an
exception."}


I checked, and I am referencing the correct Castle.DynamicProxy2 dll
file. It is being output to my bin folder. Does anybody have any
suggestions?

Hudson Akridge

unread,
Apr 28, 2009, 10:12:03 AM4/28/09
to fluent-n...@googlegroups.com
Is the correct version of Castle.DynamicProxy2 referenced? Also, which version of NHibernate are you attempting to use?

jimmarq

unread,
Apr 28, 2009, 11:10:01 AM4/28/09
to Fluent NHibernate
Castle.DynamicProxy2.dll
File Version: 2.0.3.5330
Assembly Version: 2.0.3.0

NHibernate.dll
File Version: 2.0.1.4000
Assembly Version: 2.0.1.4000

These came right from fluentnhibernate-binary-0.1.0.453.zip.

Hudson Akridge

unread,
Apr 28, 2009, 11:15:42 AM4/28/09
to fluent-n...@googlegroups.com
Another quick question Jim. Do the tests run/pass on the build you've got? Those should also be opening and creating sessions and encountering this error.

jimmarq

unread,
Apr 28, 2009, 11:41:41 AM4/28/09
to Fluent NHibernate
I downloaded the binaries and source for r457. I built the source and
the tests passed. I still get the same error, though.

Here is the method where the exception is thrown in
FluentConfiguration.cs, line 99:

/// <summary>
/// Verify's the configuration and instructs NHibernate to
build a SessionFactory.
/// </summary>
/// <returns>ISessionFactory from supplied settings.</returns>
public ISessionFactory BuildSessionFactory()
{
try
{
return BuildConfiguration()
.BuildSessionFactory();
}
catch (Exception ex)
{
throw CreateConfigurationException(ex);
}
}

I'm new to NHibernate and Fluent NHibernate, so I'm not exactly sure
how to go about troubleshooting this. I appreciate your help.

Hudson Akridge

unread,
Apr 28, 2009, 11:46:24 AM4/28/09
to fluent-n...@googlegroups.com
Hrm, give me a few and I'll try grabbing the binaries as well and getting a test scenario going. To assist me, would you mind posting a couple of simple class data definitions and their corresponding mappings (say for Product)? I'd like to replicate your environment as much as possible here.

James Gregory

unread,
Apr 28, 2009, 11:49:22 AM4/28/09
to fluent-n...@googlegroups.com
It sounds like there's a versioning issue going on here. How many projects do you have in your solution? How many of those reference either NH or Castle.DynamicProxy? Are all of those projects referencing the same version of NH/DynamicProxy from the same location? Are you using the GAC and are you certain all the projects are either referencing from the GAC, or not at all?

jimmarq

unread,
Apr 28, 2009, 12:11:14 PM4/28/09
to Fluent NHibernate
I left 3 classes in my library: ProductMap, Product, and DBHelper. I
still get the error. Here are the 3 classes:

public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.Id);
Map(x => x.Category);
Map(x => x.Discontinued);
Map(x => x.Name);
}
}

public class Product
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }

}

public class DBHelper
{
private static ISessionFactory _sessionFactory;

private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database
(SQLiteConfiguration.Standard.UsingFile("nhibernate.db"))
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Product>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
return _sessionFactory;
}
}

public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}

private static void BuildSchema(Configuration config)
{
if (File.Exists("nhibernate.db"))
File.Delete("nhibernate.db");

new SchemaExport(config).Execute(false, true, false,
false);
}

}


Here is what I'm calling in a separate assembly that triggers the
Exception.

//This code fails
using (ISession session = DBHelper.OpenSession())
{
//empty
}

Hudson Akridge

unread,
Apr 28, 2009, 12:21:32 PM4/28/09
to fluent-n...@googlegroups.com
Thanks for the postings, I'll take a look at it first chance I get after work ;)

To Jame's point, I think he's actually along a better line of thinking than myself, which is, are you using the Fluent's version of NHibernate (and other dependent classes) in your entire solution consistently? You can't mix and match unfortunately. I believe that the fluent library is depending on the public GA of NHibernate. If you have a more recent version of NHibernate (say an alpha or the like), you'll need to grab fluent's source, swap out the nhibernate reference, and rebuild fluent.

Victor Kornov

unread,
Apr 28, 2009, 12:23:15 PM4/28/09
to fluent-n...@googlegroups.com
binding redirect helped me. haven't done extensive testing or used for anything serious though

jimmarq

unread,
Apr 28, 2009, 12:56:12 PM4/28/09
to Fluent NHibernate
>To Jame's point, I think he's actually along a better line of thinking than
>myself, which is, are you using the Fluent's version of NHibernate (and
>other dependent classes) in your entire solution consistently?

*slaps head*
*slaps head again*

I was missing a DLL reference in my client. I had all of the
references fine in my assembly. I had been testing NHibernate and got
things working, but when I tried Fluent NHibernate, I didn't add the
extra references to my client assembly.

Thanks for your help. I knew I must have been doing something wrong.
Reply all
Reply to author
Forward
0 new messages