- 有一个应用程序(executor.exe),它使用反射调用类库(lib.dll)中的方法。
- executor.exe嵌入了Newtonsoft.Json 8.0版本作为嵌入式资源。
- lib.dll引用了Newtonsoft.Json 9.0版本。
- lib.dll引用了system.net.http.formatting 4.0.0.21112版本,该版本又引用了Newtonsoft.Json 4.5。
- 除了测试之外,我没有机会修改executor.exe.config。
我希望得到什么:
new JsonMediaTypeFormatter().SerializerSettings;
这是从lib.dll中调用的。但它出现了以下错误:
未找到方法:“Newtonsoft.Json.JsonSerializerSettings System.Net.Http.Formatting.JsonMediaTypeFormatter.get_SerializerSettings()”
我试图做什么:
- Handling AppDomain.CurrentDomain.AssemblyResolve (subscribed correctly, using ModuleInitializer). But it doesn't rise. After crash have 2 Newtonsoft.Json (with different versions) loaded to AppDomain.
Binding in app config:
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="4.0.0.0-5.0.0.0" newVersion="9.0.0.0" />
是的,它有效。但我不能使用这个解决方案。在传递后,AppDomain中加载了2个Newtonsoft.Json(具有不同的版本)。
I don't understand why this works (oldVersion="8.0.0.0-9.0.0.0") but:
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="8.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
异常“方法未找到”不会抛出。在加载了1个Newtonsoft.Json(9.0)到AppDomain之后,但不适合我。
为什么AppDomain.CurrentDomain.AssemblyResolve不能正常工作?我猜问题在于已加载的2个程序集,但我无法更改这种行为。