Could not load file or assembly

Jul 31, 2009 at 11:03 AM

Hi All,

I'm a newbie to the unity framework so please bear with me.

I've created a test windows application.

the config file is as follows

 <unity>
    <containers>
      <container>
        <types>
                   <type type="CommonInterface.ITestClass, CommonInterface" mapTo="Custom.MyConcreteClass, Custom" name="myMapping1"/>
                    <type type="CommonInterface.ITestClass, CommonInterface" mapTo="Custom2.MyConcreteClassII, Custom2" name="myMapping2"/>
          </types>
      </container>
    </containers>
  </unity>
 
and my code is:
 IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);

            // Retrieve an instance of each type
            ITestClass testClass = container.Resolve<ITestClass>(mapping);

            return testClass.Implement();
I currently have my assemblies(Custom, CustomII) in a folder(MyAssemblies) in the root directory of the application.
How do i get unity framework to resolve the directory location?
Thanks in advance.
Jul 31, 2009 at 1:18 PM

Hi devegovernder,

You'll have to set a reference to your assemblies.   You can dynamically load the assemblies however I recently ran into an issue (in Silverlight) where I could not BuildUp() an instance that was instantiated from a dynamically loaded assembly- at least not directly.    As you can see from the second figure on the following blog:

http://www.global-webnet.net/blogengine/post/2009/07/27/How-to-use-NET-RIA-Services-in-PRISM.aspx

I pass in the container and then within the Initialize method do a Buildup(this) which successfully executed setter injection (properties with [Dependency] attribute).

Note: you might want to investigate the Managed Extensibility Framework (MEF) as you can drop assemblies in the configured folders and it will automajically load them. 

Jul 31, 2009 at 1:39 PM

Thanks for the quick response Billkrat.

What I ideally wanted was to create a plug-and-play application. I wanted to separate the plugable assemblies from the applications assemblies.

It would have been nice if we could specify the location of the assembly to be loaded in config file as part of the type attribute.

I'll have a look at MEF.

Aug 1, 2009 at 4:31 AM

Unity doesn't do any assembly loading - we just pass the type name off to the CLR and it does what it does. As such, you need to put the assembly somewhere that the CLR loader can find it.

For desktop apps, that basically means it should be in the same directory with your EXE, or in a subdirectory of that location with a config file that gives a custom probingPath to the loader. You don't have to use an assembly reference, but that's the simplest way to get Visual Studio to get the assembly into your bin directory.

I can't comment about the Silverlight issue; I haven't done a whole lot on that platform yet so I don't know the ins and outs of dynamic loading there.

 

Aug 3, 2009 at 7:32 AM

Thanks ctavares,

That has pointed me in the right direction.