可收集ALC问题[A non-collectible assembly may not reference a collectible assembly.]

ITask程序集在共享类库中定义,初衷是任务调度程序,创建新的可卸载ALC以供每一个任务运行,此时会出现两个问题:

  1. 任务调度程序加载了任务程序后,任务程序中的ITask类型和任务调度程序中的ITask类型不是同一个类型。
  2. 若在可回收的ALC中使用Xml序列化,会报异常:A non-collectible assembly may not reference a collectible assembly.

问题2的堆栈跟踪

    at System.Reflection.Emit.ModuleBuilder.GetTypeRef(QCallModule module, String strFullName, QCallModule refedModule, String strRefedModuleFileName, Int32 tkResolution)

    at System.Reflection.Emit.ModuleBuilder.GetTypeRefNested(Type type, Module refedModule, String strRefedModuleFileName)

    at System.Reflection.Emit.ModuleBuilder.GetTypeTokenWorkerNoLock(Type type, Boolean getGenericDefinition)

    at System.Reflection.Emit.ModuleBuilder.GetTypeTokenInternal(Type type, Boolean getGenericDefinition)

    at System.Reflection.Emit.ILGenerator.Emit(OpCode opcode, Type cls)

    at System.Xml.Serialization.CodeGenerator.Castclass(Type target)

    at System.Xml.Serialization.CodeGenerator.InternalConvert(Type source, Type target, Boolean isAddress)

    at System.Xml.Serialization.SourceInfo.InternalLoad(Type elementType, Boolean asAddress)

    at System.Xml.Serialization.XmlSerializationWriterILGen.WriteElement(SourceInfo source, ElementAccessor element, String arrayName, Boolean writeAccessor)

    at System.Xml.Serialization.XmlSerializationWriterILGen.WriteElements(SourceInfo source, String enumSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, String arrayName, Boolean writeAccessors, Boolean isNullable)

    at System.Xml.Serialization.XmlSerializationWriterILGen.WriteMember(SourceInfo source, String choiceSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, TypeDesc memberTypeDesc, Boolean writeAccessors)

    at System.Xml.Serialization.XmlSerializationWriterILGen.GenerateTypeElement(XmlTypeMapping xmlTypeMapping)

    at System.Xml.Serialization.XmlSerializationWriterILGen.GenerateElement(XmlMapping xmlMapping)

    at System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace)

    at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location)

    at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace, String location)

    at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)

    at System.Xml.Serialization.XmlSerializer..ctor(Type type)

 

问题1:

可收集ACL中也加载了一份ITask所在的程序集,而宿主程序中也加载了一份,因此.net core认为不是同一个类型。

在可收集ACL的Load方法中,跳过加载ITask所在程序集即可,这样当可收集ACL需要使用ITask程序集时,会从宿主程序中查找,即Load方法返回null时对应的程序集都会从宿主程序中查找。

 

问题2:

.NET5 Preview1中会解决Xml序列化的问题

github上的issues,其中就有提到这两个问题

https://github.com/dotnet/runtime/issues/1388

 

posted @ 2020-04-02 09:53  我的伙伴  阅读(560)  评论(0编辑  收藏  举报