English 中文(简体)
C#可以找到已经装满的组装。
原标题:C# can t find assembly which is already loaded
  • 时间:2011-11-22 07:50:53
  •  标签:
  • c#
  • .net

I am writing an application which uses plugins. Plugins are class libraries which lie in Plug-ins directory. My app loads these libraries via LoadFrom. Some of them have dependencies in the form of libraries which lie in the same Plug-ins directory. When I try to create instance of class from one of plugins via Activator.CreateInstance i recieve an exception Unable to find assembly (this is dependency assembly of plugin), but this assembly is already loaded (!) along with plugins and It is visible in ProcessExplorer. I can t uderstand in what my trouble is.

最佳回答

我有类似的问题,通常通过改变项目特性的目标框架来解决这些问题。

问题回答

Your problem might be, that de loaded assembly isn t the same version as the request one. .Net Runtime maps the Assembly after their name and after their Version if the name equals and the Version differes you get an exception if the other one is loaded, which says "Assembly cann t be found" or something like that. The Problem is, that the assembly could not be matched properly. But there is a solution:
Take a look at the MSDN for further information about that Problem.

Solution for that problem:

  1. If you have to load 2 Versions of that assembly try helping the runtime by implementing the AssemblyResolve Event Samples are also here.
  2. Try using the AssemblyBindLogViewer to determine the dependencies of your plugins and to crosscheck your problem.

I recommend implementing the event anyways if you deal with plugins, so you can log all assembly requests of that AppDomain.

You will find furhter information about runtime behavior and assembly loading here

Hope i could help, please give us feedback about your solution!

Configuring the Plugin Folder

  1. Load the Plugins into a seperat AppDomain which has the pluginfolder as ApplicationBase
    To configure AppDomains see. This is the recomendet solution to load Plugins, becaus you can define the security level of the AppDomain (Sandboxing)
  2. Extend your current AppDomains PrivatePath, so it also searches the Assemblies in this Path. This method is Obsolete!(but does it s job)

你应该提供完整的集会档案。

class Program
{
    static void Main(string[] args)
    {
        var asmFileName = "test.dll"; // Your plug-in file name
        var asmPath = AppDomain.CurrentDomain.BaseDirectory; // Your assemblies s root folder
        var asmFullPath = System.IO.Path.Combine(asmPath, asmFileName);
        var asm = System.Reflection.Assembly.LoadFrom(asmFullPath);
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签