MissingMethodException on Google Contact embedded dll

本文关键字:embedded dll Contact Google on MissingMethodException | 更新日期: 2023-09-27 18:34:06

我想创建一个C#库(库范围是与Google联系人api的通信),并将依赖项嵌入库中。

所以,在我的类构造函数中,我把这段代码放进去:

AppDomain.CurrentDomain.AssemblyResolve += (sender, evento) =>
            {
                var assemblyName = evento.Name.Split(',')[0].Trim();
                if (assemblyName.ToLower().Equals("google.gdata.contacts"))
                    return Assembly.Load(Assembly.GetExecutingAssembly().GetEmbeddedResource("Contacts.Assembly.Google.GData.Contacts.dll"));
                else if (assemblyName.ToLower().Equals("google.gdata.client"))
                    return Assembly.Load(Assembly.GetExecutingAssembly().GetEmbeddedResource("Contacts.Assembly.Google.GData.Client.dll"));
                else if (assemblyName.ToLower().Equals("google.gdata.extensions"))
                    return Assembly.Load(Assembly.GetExecutingAssembly().GetEmbeddedResource("Contacts.Assembly.Google.GData.Extensions.dll"));
                else if (assemblyName.ToLower().Equals("newtonsoft.json"))
                    return Assembly.Load(Assembly.GetExecutingAssembly().GetEmbeddedResource("Contacts.Assembly.Newtonsoft.Json.dll"));
                return null;
            };

这样,当 AppDomain 尝试解析 Google 联系人库或其依赖项时,我会返回我的嵌入式程序集。这工作!!

我的问题是当我调用此代码时:

RequestSettings settings = new RequestSettings("ApplicationName");
ContactsRequest cr = new ContactsRequest(settings);
Feed<Google.Contacts.Contact> f = cr.GetContacts();

此代码同样适用于 RequestSettings(此类位于 google.data.client.dll 中),但是当尝试创建 ContactRequest 实例(此类位于 google.data.contacts.dll 中)时,它会引发"MissingMethodException"。

为什么代码返回此错误?

MissingMethodException on Google Contact embedded dll

检查你的google.data.contacts.dll:可能这取决于你不包括的另一个dll(例如log4net)。

还要检查您内部的异常,它应该包括有关它的详细信息。

我发现了问题!!与不同,每次调用方使用未引用的程序集时,都会引发 AssemblyResolve 事件。但是,在我的代码中,我每次加载相同的程序集,但对于 AppDomain 来说,它们是不同的程序集。例如:

MyDLL 将 AssemblyResolve 提升为加载:google.gdata.client 和 google.gdata.contact然后是应用程序实例 ContactRequest (google.gdata.contact)。但是这个dll会引发AssemblyResolve加载:google.gdata.client

对于AppDomain,google.gdata.client(加载在MyDLL中)与google.gdata.client(加载到google.gdata.contact)不同

解决问题,请构建一个字典,其中包含要加载的所有 dll,因此当域请求解析程序集时,代码将返回相同的程序集