动态加载装配体的问题 [英] Problem with Dynamic loading of Assemblies

查看:72
本文介绍了动态加载装配体的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我希望第三方开发人员能够创建插件,这些插件将动态加载到我们的

应用程序中以扩展功能。


我使用了让用户为你的.NET添加功能

带有宏和插件的应用程序 MSDN上的文章动态

加载DLL

http://msdn.microsoft.com/msdnmag/is...s/default.aspx


我们定义了一个界面,一切正常。用户

将实现界面,我们将动态加载对象

,我们的客户端应用程序将正确实现该功能。


然后我创建了一个库类,其中包含第三方开发人员需要利用其应用程序开发的对象。

开发。我们在

接口中引用了这个库类,用户需要实现我们的部分

库类的属性。添加此功能并实现界面后,我们的动态加载DLL不再起作用。我得到一个

ReflectionTypeLoadException错误


程序集中的一个或多个类型无法加载。

System.SystemException:{"程序集中的一个或多个类型

无法加载。"}

_classes:{Length = 2}

_exceptions:{Length = 1}

LoaderExceptions:{Length = 1}

种类:{Length = 2}


如果我查看LoaderExceptions,我收到的消息是


{" Com.Corp.CorpImage.CorpImage"}

System.SystemException :{"方法get_CorpImageFields类型

Com.Corp.CorpImage.CorpImage来自程序集CorpImage,

Version = 1.0.1741.17159,Culture = neutral,PublicKeyToken = null不

有一个实现。"}

AssemblyName:" CorpImage,Version = 1.0.1741.17159,Culture = neutral,

PublicKeyToken = null" ;

ClassName:" CorpImage"
消息:" Method get_Cor pImageFields类型

Com.Corp.CorpImage.CorpImage from assembly CorpImage,

Version = 1.0.1741.17159,Culture = neutral,PublicKeyToken = null not

有一个实现。

MessageArg:" get_CorpImageFields"

ResourceId:6012

TypeName:" Com.Corp。 CorpImage.CorpImage"

我可以通过在查询类型时跳过加载库

类来解决加载问题。虽然这是一个解决方案,但我不确定它是否会让一切顺利进行。我也是

我不知道我做了什么:)为什么跳过尝试迭代getTypes

的库dll可以让事情发挥作用?


同样,代码与上面的

MSDN文章中提到的代码非常类似。我跳过加载的代码更改为

跟随...


private String [] DiscoverPluginAssembliesHelper(

String path,PluginCriteria criteria,Type criteriaType)

{

String [] assembly;

//获取.dll名称

path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,path);

assemblies = Directory.GetFiles(path," * .dll");

试试

{

if(assemblies.Length!= 0)

{


ArrayList assembliesIncluded = new ArrayList();

foreach(程序集中的字符串)

{

//加载程序集(它不是控制库)

if(s.ToLower()。IndexOf(" controllibrary")< 0)

{

装配集会= Assembly.LoadFrom(s);

//查找匹配类型?

类型[]类型= assembly.GetTypes();

foreach (在类型中输入t)

{

if(IncludeType(t,c) riteria,criteriaType))

{

assembliesIncluded.Add(s);

break; //匹配找到,继续前进

}

}

}

}

//获取匹配程序集的数组

assemblies =(String [])assembliesIncluded.ToArray(

typeof(String));

}

}

catch(ReflectionTypeLoadException oEx)

{

抛出新的异常(发现插件程序集时出错) ,oEx);

}

catch(异常oEx)

{

抛出新异常(&错误发现插件程序集,oEx);

}

返回程序集;

}


任意非常感谢见解


Endymion济慈

解决方案

界面装配在哪里?程序集解析程序只会查看您的APPBASE(通常是运行应用程序的目录)和一个与程序集具有相同简单名称的子目录(除非接口程序集具有强名称)。因此,如果您的接口位于名为iface.dll的程序集中,并且您的应用程序是从C:\ Foo目录运行的,那么程序集解析程序将只查看目录


C: \ foo

C:\\\\\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\应用程序配置文件中的元素。


我怀疑在枚举类型时,在加载类型信息时无法找到其中一种类型的interfce dll


问候


Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet .languages.csharp /< 47 ************************* @ posting.google.com>


我有一个应用程序,我希望第三方开发人员能够创建插件,这些插件将动态加载到我们的

应用程序中以扩展功能。 br />

我使用了让用户为你的.NET添加功能

带有宏和插件的应用程序 MSDN上的文章动态

加载DLL

http://msdn.microsoft.com/msdnmag/is...s/default.aspx


我们定义了一个界面,一切正常。用户

将实现界面,我们将动态加载对象

,我们的客户端应用程序将正确实现该功能。


然后我创建了一个库类,其中包含第三方开发人员需要利用其应用程序开发的对象。

开发。我们在

接口中引用了这个库类,用户需要实现我们的部分

库类的属性。添加此功能并实现界面后,我们的动态加载DLL不再起作用。我得到一个

ReflectionTypeLoadException错误


我试过把< Probing>标记到我的App.config文件中,确实没有解决问题。


我不是很精通程序集和应用程序域。我相信我正在测试在

第二个域中实现接口的程序集,然后将它们添加到我执行的应用程序中

domain 。这可能是问题的一部分吗?


Endymion济慈


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET .. .get奖励它!


是什么让你说你在辅助AppDomain中运行代码?您是否通过使用AppDomain.CreateDomain()进行编码?

问候


Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/< eR**************@tk2msftngp13.phx.gbl>


我试过把< Probing>标记到我的App.config文件中,确实没有解决问题。


我不是很精通程序集和应用程序域。我相信我正在测试在

第二个域中实现接口的程序集,然后将它们添加到我执行的应用程序中

domain 。这可能是问题的一部分吗?


Endymion济慈


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET .. .get奖励它!


---

收到的邮件经过无病毒认证。

由AVG反病毒检查系统( http://www.grisoft.com)

版本:6.0.771 /病毒库:518 - 发布日期:28/09/2004


[microsoft.public.dotnet.languages.csharp]

I have an application that I would like third party developers to be
able to create Plug-ins that will be dynamically loaded into our
application to extend functionality.

I have utilized the "Let Users Add Functionality to Your .NET
Applications with Macros and Plug-Ins" article at MSDN for the dynamic
loading of DLLs

http://msdn.microsoft.com/msdnmag/is...s/default.aspx

We have an interface defined and everything was working fine. Users
would implement the interface and we would load the object dynamically
and our client application would properly implement the functionality.

I then went and create a Library class that would contain objects that
the third party developers need for utilization with their application
development. We have a reference to this Library class in our
Interface, and users need to implement properties that our part of the
library class. Upon adding this, and implementing the interface, our
dynamic loading of DLLs no longer functioned. I get a
ReflectionTypeLoadException error

"One or more of the types in the assembly unable to load."
System.SystemException: {"One or more of the types in the assembly
unable to load."}
_classes: {Length=2}
_exceptions: {Length=1}
LoaderExceptions: {Length=1}
Types: {Length=2}

and if I look at the LoaderExceptions the message I get is

{"Com.Corp.CorpImage.CorpImage"}
System.SystemException: {"Method get_CorpImageFields in type
Com.Corp.CorpImage.CorpImage from assembly CorpImage,
Version=1.0.1741.17159, Culture=neutral, PublicKeyToken=null does not
have an implementation."}
AssemblyName: "CorpImage, Version=1.0.1741.17159, Culture=neutral,
PublicKeyToken=null"
ClassName: "CorpImage"
Message: "Method get_CorpImageFields in type
Com.Corp.CorpImage.CorpImage from assembly CorpImage,
Version=1.0.1741.17159, Culture=neutral, PublicKeyToken=null does not
have an implementation."
MessageArg: "get_CorpImageFields"
ResourceId: 6012
TypeName: "Com.Corp.CorpImage.CorpImage"
I can get by the loading issue by skipping the loading of the Library
class when querying the types. While it''s a work around, I am not
sure if it will enable everything to work down the road. I am also
unsure what I did :) Why does skipping trying to iterate the getTypes
of the Library dll enable things to work?

Again, the code is pretty similar to the one mentioned in the above
MSDN article. The change to the code I made to skip loading is as
follows...

private String[] DiscoverPluginAssembliesHelper(
String path, PluginCriteria criteria, Type criteriaType)
{
String[] assemblies;
// Get .dll names
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory , path);
assemblies = Directory.GetFiles(path, "*.dll");
try
{
if(assemblies.Length != 0)
{

ArrayList assembliesIncluded = new ArrayList();
foreach(String s in assemblies)
{
// Load the assembly (it it''s not the control library)
if (s.ToLower().IndexOf("controllibrary") < 0)
{
Assembly assembly = Assembly.LoadFrom(s);
// Find matching type?
Type[] types = assembly.GetTypes();
foreach(Type t in types)
{
if(IncludeType(t, criteria, criteriaType))
{
assembliesIncluded.Add(s);
break; // match found, move on
}
}
}
}
// Get array of matching assemblies
assemblies = (String[])assembliesIncluded.ToArray(
typeof(String));
}
}
catch (ReflectionTypeLoadException oEx)
{
throw new Exception("Error discovering plugin assemblies",oEx);
}
catch (Exception oEx)
{
throw new Exception("Error discovering plugin assemblies",oEx);
}
return assemblies;
}

Any insights greatly appreciated

Endymion Keats

解决方案

Where is the interface assembly? The assembly resolver will only look in your APPBASE (normally the directory the application is running in) and a subdirectory with the same simple name as the assembly (unless the interface assembly has a strong name). So if your interface is in an assembly called iface.dll and your app is running from the C:\Foo directory, then the assembly resolver will only look in the directories

C:\foo
C:\foo\iface

unless you extend the seatch path via the <probing> element in the appliction config file.

I suspect that when enumerating the types it cannot find the interafce dll for one of the types when it loads the type information

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<47*************************@posting.google.com>

I have an application that I would like third party developers to be
able to create Plug-ins that will be dynamically loaded into our
application to extend functionality.

I have utilized the "Let Users Add Functionality to Your .NET
Applications with Macros and Plug-Ins" article at MSDN for the dynamic
loading of DLLs

http://msdn.microsoft.com/msdnmag/is...s/default.aspx

We have an interface defined and everything was working fine. Users
would implement the interface and we would load the object dynamically
and our client application would properly implement the functionality.

I then went and create a Library class that would contain objects that
the third party developers need for utilization with their application
development. We have a reference to this Library class in our
Interface, and users need to implement properties that our part of the
library class. Upon adding this, and implementing the interface, our
dynamic loading of DLLs no longer functioned. I get a
ReflectionTypeLoadException error


I tried putting the <Probing> tag into my App.config file and that did
not resolve the issue.

I am not very well versed in Assemblies and application domains. I
believe I am testing the assemblies that implement the interface in a
second domain, and then they get added to my executing application
domain. Could this be part of the issue?

Endymion Keats

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


What makes you say you are running the code in a secondary AppDomain? Have you coded is this way by using AppDomain.CreateDomain() ?

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<eR**************@tk2msftngp13.phx.gbl>

I tried putting the <Probing> tag into my App.config file and that did
not resolve the issue.

I am not very well versed in Assemblies and application domains. I
believe I am testing the assemblies that implement the interface in a
second domain, and then they get added to my executing application
domain. Could this be part of the issue?

Endymion Keats

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]


这篇关于动态加载装配体的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆