我的项目称为Z
,它依赖于另外两个项目X
和Y
,并使用Newtonsoft.Json
进行序列化,同时还依赖于System.Text.Json 7.0.0
提供对自己类型的注解。
项目X
依赖于System.Text.Json 7.0.0
。项目Y
依赖于System.Text.Json 6.0.1
。
当使用Newtonsoft.Json.JsonConvert
序列化来自Y
的对象时,我得到以下异常:
System.IO.FileLoadException: Could not load file or assembly 'System.Text.Json, Version=6.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).
这是因为Newtonsoft.Json
试图从Y
中的一些自定义转换器解析Y
中类型的属性注解:
ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
CustomAttribute.IsDefined(RuntimePropertyInfo property, RuntimeType caType)
DefaultContractResolver.GetSerializableMembers(Type objectType)
DefaultContractResolver.CreateProperties(Type type, MemberSerialization memberSerialization)
DefaultContractResolver.CreateObjectContract(Type objectType)
ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
<6 more frames...>
JToken.FromObjectInternal(Object o, JsonSerializer jsonSerializer)
JArray.FromObject(Object o, JsonSerializer jsonSerializer)
CustomConverterFromProjectY.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)
JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
JsonConvert.SerializeObject(Object value)
我的假设是,这是因为Z
依赖于版本7。0.0,因此加载了1,但对6不起作用。0.1.我试着将Z
的依赖降到6。0.1,但是当我与X
中的类型交互时,我得到了一个对称错误:
System.IO.FileLoadException: Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
通常解决方案是确保X
和Y
使用相同版本的System.Text.Json
,* 但 * 这里还有两个警告:
*我不控制X
或Y
,无法编辑它们。
- 从
Z
中的Newtonsoft.Json
迁移到System.Text.Json
可能会解决这个问题(因为是Newtonsoft的属性查找引发了异常),但我想要一个没有这个问题的解决方案。Z
是一个大项目,迁移会带来巨大的成本和风险。
我需要一种方法来使用X
和Y
,而不编辑它们的依赖项。有没有办法在Z
中使用两个不同版本的System.Text.Json
,并让反射位解析属性知道在哪里可以找到它们?我该如何解决这个问题?
所有的项目都是为。NET Framework 4.6.2.
1条答案
按热度按时间yjghlzjz1#
@dbc对binding redirects的建议是这里的解决方案。
在项目
Z
的App.config
中,我添加了以下内容:这将把所有对
System.Text.Json
的引用重定向到最新版本。这种方法奏效了--我开始得到关于System.Text.Json
依赖的不同程序集的错误。我也重定向了所有这些,然后这个项目就成功了。下面是我在最后的完整配置:此外,我必须在我的测试项目中放置相同的配置,引用
Z
以使测试通过。