ASP.NET application reference to assembly in programmatically loaded assembly

我们两清 提交于 2019-12-10 10:59:26

问题


My ASP.NET application loads an assembly and creates a class instance from an object within it. The assembly itself has a reference to a dll called "Aspose.Cells.dll" which it can access without any problems and is located on the same path as the assembly file itself. However, when I make a method call to one of its methods I get this error:

Could not load file or assembly 'Aspose.Cells, Version=4.1.2.0, Culture=neutral, PublicKeyToken=9a40d5a4b59e5256' or one of its dependencies.

I figure that I need to make my ASP.NET application reference this DLL as well but everything I've tried so far has failed. The assembly DLL which is loaded is not located within the root of the web application and I'm hoping that it doesn't have to be.

I can load the assembly like this:

Assembly asm = Assembly.LoadFile(@"D:\Dev\EasyFlow\Plugins\ImportExportLibrary\bin\Debug\Aspose.Cells.dll");

But I can not use it like this:

AppDomain newDomain = AppDomain.CreateDomain("testAspose");
Assembly asm = newDomain.Load(System.IO.File.ReadAllBytes(@"D:\Dev\EasyFlow\Plugins\ImportExportLibrary\bin\Debug\Aspose.Cells.dll"));

Is there a better way to do it?

Update:

Thanks for your replies.

I forgot to mention that I cannot copy the assemblies to the bin folder or the GAC because I want it to work as a plugin system where assemblies can be replaced easily by changing the path to assemblies in a configuration file and not having to manually copy files into the main project.

However, the AssemblyResolve event seems interesting and I will give it a try later.


回答1:


There are a few ways to do this. One way is described in the Microsoft Knowledge Base article "How to load an assembly at runtime that is located in a folder that is not the bin folder of the application". It provides a pretty good step-by-step method of how to do this.




回答2:


You can copy the dependent assembly into the Bin folder or install it in the GAC.




回答3:


I'm thinking you can add assembly references to the web.config, in the <compilation> section, like so:

<compilation debug="true">
  <assemblies>
     <add assembly="Aspose.Cells, Version=4.1.2.0, Culture=neutral,  PublicKeyToken=9a40d5a4b59e5256"/>
  </assemblies>
</compilation>

I am not altogether certain this will work, and it may require a corresponding entry to web.config in a <configSections> node under <configuration>.



来源:https://stackoverflow.com/questions/804267/asp-net-application-reference-to-assembly-in-programmatically-loaded-assembly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!