在asp.net中动态加载程序集

发布于 2024-10-27 14:12:58 字数 500 浏览 1 评论 0原文

我目前正在尝试从 asp.net httphandler 中动态加载程序集。我有一个作为单独库的一部分构建的 dll,我的项目包含对所述 DLL 的引用,并与 CopyLocal true 的服务一起部署。我创建了一个一次性对象来获取程序集路径,并且已确认 Temporary ASP.NET Files 文件夹中存在该 dll,但调用 GetTypes() 会引发异常。

我做了类似的事情:

string assemblyPath = new SomeClassInAssembly().GetType().Assembly.Location;
Type[] types = System.Reflection.Assembly.LoadFrom(assemblyPath).GetTypes();

我无法将程序集添加到 GAC,因为这会破坏我尝试对服务执行的操作(认为必要时加载程序集的沙箱服务),并且我找不到任何能够解决我的问题的东西远的。

作为参考,我使用的是 VS 2008。

I'm currently trying to dynamically load an assembly from within a asp.net httphandler. I have a dll that is built as part of a seperate library and my project contains a reference to said DLL and is deployed along with the service with CopyLocal true. I create a throwaway object to get the assembly path and I have confirmed the existence of the dll within the Temporary ASP.NET Files folder, but calling GetTypes() throws an exception.

I do something like:

string assemblyPath = new SomeClassInAssembly().GetType().Assembly.Location;
Type[] types = System.Reflection.Assembly.LoadFrom(assemblyPath).GetTypes();

I cannot add the assembly to the GAC since that would defeat what I am trying to do with the service (think sandbox service that loads assemblies when necessary) and I cannot find anything that has been able to fix my problem thus far.

For reference I'm using VS 2008.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装纯掩盖桑 2024-11-03 14:12:58

由于您使用对程序集的引用来编译 Web 应用程序,因此我认为您不需要使用 Assembly.LoadFrom 加载它。 GetTypes 应该可以使用:

Type[] types = typeof(SomeClassInAssembly).Assembly.GetTypes();

Since you compile your web application with a reference to the assembly i don't see your need to load it using Assembly.LoadFrom. The GetTypes should be available using:

Type[] types = typeof(SomeClassInAssembly).Assembly.GetTypes();
岁月静好 2024-11-03 14:12:58

哦,我终于连接到了异常并查看了 LoaderMessage,我缺少一个引用的程序集。

Doh, I finally hooked up to the exception and looked at the LoaderMessage and I was missing a referenced assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文