views:

317

answers:

1

Hi

First I will try to explain what I want to do.

The app loads a NhHibernate config file and parse the xml, allowing changes to the connection string and the mapping assembly.

The problem is that when I run the app in the directory that contains NHibernate.dll and all references, it works well, but if I run the app from other directory, the Configure() fails.

This is the code I have:

        Configuration cfg = new Configuration();
        cfg.Properties = props; // the properties are OK
        cfg.AddAssembly(Assembly.LoadFrom(filename)); // <- this line fails

When I debugged NHibernate, it failed in this method in class NHibernate.Util.ReflectUtil:

public static System.Type TypeFromAssembly(AssemblyQualifiedTypeName name, bool throwOnError) 
.
.
Assembly assembly = Assembly.Load(name.Assembly); // line 265
.
.

That method search for the assembly in the current domain, but the assembly is located in other directory.

The purpose of the app is to replicate in different development and test environments little changes made to the entities. I don't want the others developers copy this app to their projects, I want that they select the correct project directory in the app, then the app will configure NHibernate, and if requested by the user, it will execute the updates to their DB.

I tried loading/executing the app in a different AppDomain but with no success.

Maybe this is not possible? or I'm missing something.
Some advise please, I'm stuck with this problem.

PD: Sorry for my English, correct the question if necessary.

+1  A: 

If the required assemblies are in a non-standard directory, you will need to help the CLR find them. There are a number of ways to do this:

  1. Via a probing element in configuration.
  2. Via AppendPrivatePath in a custom AppDomain.
  3. Via the AssemblyResolve event of any AppDomain.

HTH,
Kent

Kent Boogaart
Thanks Kent, I resolved my problem with the 3rd option, the other 2 didn't work as expected.Here is another example:http://support.microsoft.com/kb/837908
emmanuel