runtime icon indicating copy to clipboard operation
runtime copied to clipboard

AssemblyLoadContext crash when collectible assembly use XmlSerializer

Open uffebjorklund opened this issue 7 years ago • 55 comments

[EDIT] Investigation of this issue is complete and has resulted in 5 additional issues being opened. See this comment below for links to each of those issues.

We will keep this issue open and update it as those other issues are fixed. Please add any additional comments to those issues.


When creating a collectible AssemblyLoadContext and then use System.Xml.Serialization.XmlSerializer inside of a collectible assembly you will get a crash.

I have a repro project: https://github.com/uffebjorklund/AssemblyLoadContextBug

Have tried a few different things

  • If creating the ALC as collectible = false there is no crash
  • If the XmlSerializer code is removed from the collectible assembly there is no crash
  • The crash will occur regardless of using netstandard2.0 or netstandard2.1 on the collectible assembly

Exception

A non-collectible assembly may not reference a collectible assembly

Stacktrace

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)

dotnet --info

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100
 Commit:    04339c3a26

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.13-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.0.100/

Host (useful for support):
  Version: 3.0.0
  Commit:  7d57652f33

.NET Core SDKs installed:
  2.1.301 [/usr/local/share/dotnet/sdk]
  2.1.302 [/usr/local/share/dotnet/sdk]
  2.1.400 [/usr/local/share/dotnet/sdk]
  2.1.401 [/usr/local/share/dotnet/sdk]
  2.1.403 [/usr/local/share/dotnet/sdk]
  2.1.500 [/usr/local/share/dotnet/sdk]
  2.2.100-preview3-009430 [/usr/local/share/dotnet/sdk]
  2.2.101 [/usr/local/share/dotnet/sdk]
  2.2.102 [/usr/local/share/dotnet/sdk]
  2.2.103 [/usr/local/share/dotnet/sdk]
  2.2.105 [/usr/local/share/dotnet/sdk]
  2.2.106 [/usr/local/share/dotnet/sdk]
  2.2.203 [/usr/local/share/dotnet/sdk]
  2.2.300 [/usr/local/share/dotnet/sdk]
  3.0.100-alpha1-009708 [/usr/local/share/dotnet/sdk]
  3.0.100-preview-010184 [/usr/local/share/dotnet/sdk]
  3.0.100-preview3-010431 [/usr/local/share/dotnet/sdk]
  3.0.100-preview8-013656 [/usr/local/share/dotnet/sdk]
  3.0.100-preview9-014004 [/usr/local/share/dotnet/sdk]
  3.0.100 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0-preview3-35497 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 3.0.0-alpha1-10062 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0-preview3-35497 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-alpha1-10062 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview-19075-0444 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview3-19153-02 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview8.19405.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview9.19424.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0-preview3-27014-02 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview-27324-5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview1-27029-03 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview3-27503-5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview9-19423-09 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

uffebjorklund avatar Sep 24 '19 12:09 uffebjorklund

Hello,

Is any update on this?

Thanks, Csaba

csalat avatar Oct 04 '19 18:10 csalat

@uffebjorklund Thanks for reporting this, sorry for the slow response. It looks like all the information we need to do further investigation is here. We will look into it.

StephenBonikowsky avatar Nov 12 '19 22:11 StephenBonikowsky

I did a bit of digging in to this and I believe that the problem is due to XmlSerializer creating a dynamic assembly in the default load context which makes the dynamic assembly non-collectible. When we call ILGenerator.Emit(OpCodes.Castclass, type) with a type which is a collectible assembly, we get an exception. I did some digging around the api's and I can't see a way to specify the AssemblyLoadContext to generate a dynamic assembly into.
@sdmaclea, is this scenario supported? If so, can you point me to the api's to create a dynamic assembly in a specific AssemblyLoadContext?

mconnew avatar Nov 22 '19 23:11 mconnew

@sdmaclea, is this scenario supported? If so, can you point me to the api's to create a dynamic assembly in a specific AssemblyLoadContext?

@mconnew I think this was a feature we missed in 3.0. There is an API proposal to close this gap dotnet/corefx#38426

/cc @vitek-karas @elinor-fung @jeffschwMSFT

sdmaclea avatar Nov 23 '19 00:11 sdmaclea

Hi, I share the same supposition, that it can be because of the dynamic generated assemblies. But: I have tried using the XmlSerializerGenerator, with the idea that it wouldn't generated dynamic assemblies anymore. But still I can't make it to work, see attached solution. Do I miss anything?!? Test_ALC with XmlSerializerGenerator.zip

curia-damiano avatar Nov 23 '19 21:11 curia-damiano

Btw this was my first project with the issue, without the XmlSerializerGenerator Test_ALC.zip

curia-damiano avatar Nov 23 '19 21:11 curia-damiano

@curia-damiano How do you generate the serialization code? I tried to turning it on via VS on the CL.Implementation.Problem assembly and that failed (which is probably an issue on its own, but not directly related to this issue).

Also - I noticed that your "plugins" are built as netstandard2.0 - this is not recommended as SDK will not always produce the right assets. Ideally plugins should not target a standard but a specific framework - for example netcoreapp3.1 or so.

vitek-karas avatar Nov 24 '19 16:11 vitek-karas

Hi, I have a ItemGroup in the project that "should" invoke the generation of the serialization code during build time. I haven't found any additional documentation saying I should do something more. FYI I have used VS 2019 Preview (16.4.0 Preview 5 or 6).

curia-damiano avatar Nov 25 '19 08:11 curia-damiano

I tried again - Release build this time - and I spotted this in the output of the build:

3>Method 'System.Xml.Serialization.XmlSerializer.GenerateSerializer' was not found. This is likely because you are using an older version of the framework. Please update to .NET Core v2.1 or later.
3>Sgen utility failed to pregenerate serialization code for E:\AppModel\repro\Test_ALC\CL.Implementation.Problem\obj\Release\netstandard2.0\CL.Implementation.Problem.dll.

@StephenMolloy for the XML serializer specific portions of this question, like the above failure of sgen.

vitek-karas avatar Nov 25 '19 09:11 vitek-karas

@curia-damiano, I haven't tried running your code yet but I can see a problem in Test_ALC with XmlSerializerGenerator.zip. This is the problem code:

	var assembly = TLC.LoadFromAssemblyPath(dllPath);
	labelUnloaded.Text = "Loaded";
	var type = assembly.GetType("CL.Implementation.Easy.Multiplier");
	var constructor = type.GetConstructor(new Type[] { });
	ICompute compute = (ICompute)constructor.Invoke(new object[] { });

When using a different AssemblyLoadContext, the same type in the same assembly is a different runtime type. So this means the ICompute interface that CL.Implementation.Easy.Multiplier implements is a different runtime type that the same interface in the default load context. So the cast on the last line will fail.

mconnew avatar Nov 25 '19 21:11 mconnew

@sdmaclea, in this comment it says it uses the callers AssemblyLoadContext. You can see here that we do use AssemblyBuilder.DefineDynamicAssembly so I would expect the dynamically created assembly to use the custom AssemblyLoadContext. Is the problem that we pass AssemblyBuilderAccess.Run instead of AssemblyBuilderAccess.RunAndCollect? What's stopping the dynamically generated assembly from being created in the correct context? Also, is this going to be a problem for DispatchProxy too?

mconnew avatar Nov 25 '19 21:11 mconnew

@sdmaclea, in this https://github.com/dotnet/corefx/issues/38426#issuecomment-500723986 it says it uses the callers AssemblyLoadContext. You can see https://github.com/dotnet/corefx/blob/e99ec129cfd594d53f4390bf97d1d736cff6f860/src/System.Private.Xml/src/System/Xml/Serialization/CodeGenerator.cs#L1297 that we do use AssemblyBuilder.DefineDynamicAssembly so I would expect the dynamically created assembly to use the custom AssemblyLoadContext.

@mconnew Looking at the link. It looks like System.Xml.Serialization.CodeGenerator.CreateAssemblyBuilder is the caller of AssemblyBuilder.DefineDynamicAssembly so the dynamic assembly would be created in the default ALC, where the CoreFX runtime is loaded.

sdmaclea avatar Nov 25 '19 22:11 sdmaclea

When using a different AssemblyLoadContext, the same type in the same assembly is a different runtime type. So this means the ICompute interface that CL.Implementation.Easy.Multiplier implements is a different runtime type that the same interface in the default load context. So the cast on the last line will fail.

I haven't looked at the code. So I am not sure this statement above is correct.

Presuming the cited code is loaded and running in AssemblyLoadContext.Default, the ICompute cast will attempt to cast to AssemblyLoadContext.Default version of the type.

For the cast to succeed:

  • the CL.Implementation.Easy.Multiplier constructor will need to return an object which implements ICompute.
  • ICompute will need to be defined in a different assembly than CL.Implementation.Easy.Multiplier
  • The assembly which defines ICompute should only be loaded into AssemblyLoadContext.Default. So that there is only one version of the type.

sdmaclea avatar Nov 25 '19 23:11 sdmaclea

Hi, Thank you for the comments above. So, a good point is that using the XmlSerializerGenerator should be a walkaround for the problem. The bad point is that XmlSerializerGenerator is badly (or almost no) documented.

@csalat: Following the comments above:

  • I have converted the dll from netstandard2.0 to netcore3.1;
  • I have removed the project ItemGroup to invoke the generator (now it's in the build.props of the NuGet package, as it comes out from multiple comments);
  • still, I get a compile time error saying that it can't find any time to be serialized.

@mconnew : Also, about the cast at runtime: I get the point that there is another instance of the dll in memory and the cast doesn't work. Still I have to guess how to solve this after I am able to make the serializer generator work. My idea is that I should be able to tell the dynamic dll loader to use the Interface dll already loaded in memory. Any tip about this second point?

I kindly ask all of you if you can please help me in making this simple POC to work.

curia-damiano avatar Nov 26 '19 07:11 curia-damiano

I attach here the modified POC after the changes discussed in the previous post Test_ALC.zip

curia-damiano avatar Nov 26 '19 07:11 curia-damiano

@curia-damiano please consider using the same patterns as shown in this sample https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support. It shows how to write an app with plugins where the problem with casting the interface types doesn't exist.

Namely I would suggest:

  • Use AssemblyDependencyResolver if possible - in the sample you shared this is definitely a typical case where it should be used. Using mechanisms like enumerating files in current directory is problematic (it's not guaranteed that the current directory will be what you expect for example, or if there's a .dll which should not be present it might get picked up and so on).
  • Build the plugins as "plugins" - that is mark the project dependencies which should be provided by the host with the <Private>false</Private> so that the plugins don't carry copies of host assemblies with them.
  • Unless necessary, avoid duplication of assembly resolution logic - in your sample you implement both the AssemblyLoadContext.Load method as well as a handler for the AssemblyLoadContext.Resolving event. You should not need both for typical cases.
  • Ideally the plugins should be self-descriptive, that is carry only the files they need to keep in isolation and rely on the host to provide the rest. If that is the case, then you don't need to implement any logic to search for assemblies which are already loaded - instead, just return null from AssemblyLoadContext.Load and the runtime will fallback to the AssemblyLoadContext.Default which has the host (the app).
  • Avoid usage of AppDomain.CurrentDomain.GetAssemblies - this returns list of all assemblies loaded in the process, across all AssemblyLoadContext instances. This means that if you load two plugins which both carry some private dependency, you might end up using the dependency from another plugin. It might work, but it would for example prevent unloading from working correctly.

In the sample app I fixed the path to the "problem" dll to use netcoreapp3.1 and now it fails to find the XmlSerializers.dll, which is expected given the failure during build. If I comment out loading the XmlSerialziers.dll the plugin itself works correctly (so casting to ICompute works, since the custom ALC does end up sharing the Interface assembly correctly), but it fails in the XmlSerializer due to the wrong load context used by the dynamically generated code.

vitek-karas avatar Nov 26 '19 15:11 vitek-karas

Hi Vitek, Thank you very much for your extensive explanation. Tomorrow I will go through the sample you have suggested me and adapt my small POC accordingly. Only, I think I still miss guidance about how to use the XmlSerializerGenerator... Regards, Damiano

On Tue, Nov 26, 2019 at 4:43 PM Vitek Karas [email protected] wrote:

@curia-damiano https://github.com/curia-damiano please consider using the same patterns as shown in this sample https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support. It shows how to write an app with plugins where the problem with casting the interface types doesn't exist.

Namely I would suggest:

  • Use AssemblyDependencyResolver if possible - in the sample you shared this is definitely a typical case where it should be used. Using mechanisms like enumerating files in current directory is problematic (it's not guaranteed that the current directory will be what you expect for example, or if there's a .dll which should not be present it might get picked up and so on).
  • Build the plugins as "plugins" - that is mark the project dependencies which should be provided by the host with the false so that the plugins don't carry copies of host assemblies with them.
  • Unless necessary, avoid duplication of assembly resolution logic - in your sample you implement both the AssemblyLoadContext.Load method as well as a handler for the AssemblyLoadContext.Resolving event. You should not need both for typical cases.
  • Ideally the plugins should be self-descriptive, that is carry only the files they need to keep in isolation and rely on the host to provide the rest. If that is the case, then you don't need to implement any logic to search for assemblies which are already loaded - instead, just return null from AssemblyLoadContext.Load and the runtime will fallback to the AssemblyLoadContext.Default which has the host (the app).
  • Avoid usage of AppDomain.CurrentDomain.GetAssemblies - this returns list of all assemblies loaded in the process, across all AssemblyLoadContext instances. This means that if you load two plugins which both carry some private dependency, you might end up using the dependency from another plugin. It might work, but it would for example prevent unloading from working correctly.

In the sample app I fixed the path to the "problem" dll to use netcoreapp3.1 and now it fails to find the XmlSerializers.dll, which is expected given the failure during build. If I comment out loading the XmlSerialziers.dll the plugin itself works correctly (so casting to ICompute works, since the custom ALC does end up sharing the Interface assembly correctly), but it fails in the XmlSerializer due to the wrong load context used by the dynamically generated code.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dotnet/corefx/issues/41286?email_source=notifications&email_token=AEN72M72A5KUZPNCNR7M2O3QVU7ZLA5CNFSM4I2BJI4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFGOTYI#issuecomment-558688737, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEN72M45JJKS6QJJ2DIR44TQVU7ZLANCNFSM4I2BJI4A .

-- Damiano Curia Swiss mobile: +41 76 50 40 60 2 Italian mobile: +39 348/360.25.73 skype: curia.damiano email: [email protected] www: http://curia.me

curia-damiano avatar Nov 26 '19 16:11 curia-damiano

I'm not an expert in the XmlSerializerGenerator, so hopefully others on this thread will be able to help with that aspect of the issue.

vitek-karas avatar Nov 26 '19 17:11 vitek-karas

I've worked out the problem with XmlSerializerGenerator. There's a temporary workaround until we work out a proper fix. The issue is being tracked in issue dotnet/corefx#40730. Unfortunately that doesn't solve this problem. XmlSerializer loads the pre-generated serialization assembly into the default AssemblyLoadContext. It then calls the method CanSerialize in the pre-generated serializer which references the types CL.Implementation.Problem.Adder and CL.Implementation.Problem.MyEntity. These types are attempted to be loaded in the default AssemblyLoadContext which is then unable to find them. We should be able to fix that problem by getting the AssemblyLoadContext from the passed in type and loading the pre-generated assembly into it, but I suspect this might end up being an onion problem where we might ultimately need new capabilities from the AssemblyLoadContext feature. We won't know until we get there.

mconnew avatar Nov 26 '19 23:11 mconnew

I tried the workaround and ran into two other issues:

Doesn't work with netcoreapp3.1

If I set the TFM to netcoreapp3.1 (since I'm compiling the component in questions as netcoreapp3.1) the SGEN itself works and produces the .cs file, but then compiling that fails with:

2>CSC : warning CS1703: Multiple assemblies with equivalent identity have been imported: 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Data.DataSetExtensions.dll' and 'C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\3.1.0\ref\netcoreapp3.1\System.Data.DataSetExtensions.dll'. Remove one of the duplicate references.

This in itself shows two problems:

  • We obviously don't de-dupe the ref assemblies for some reason.
  • More alarmingly, we're using .NET Framework assemblies - something in the build decided that it should look for ref assemblies in the .NET Framework installation. This seems really weird for .NET Core tools.

The generated assembly is not in .deps.json

Once I switched to netcoreapp2.2 it worked and generated the serialization assembly. But I noticed that while we do put that assembly to the output folder, we don't put it into the .deps.json for the app/component. As it now becomes part of the app, it should be listed in the .deps.json as well. Additionally, the .deps.json now contains entry for dotnet-Microsoft.XmlSerialzier.Generator.dll. If I understand it correctly that assembly should not be needed/used at runtime at all - so it should not be listed in .deps.json. In 3.0 (if it worked) I would actually expect this to be copied to the output (since all NuGet dependencies are now copied to build output).

Loading the generated code

The code which loads the generated assembly is not only not AssemblyLoadContext aware, but to make things worse, it uses Assembly.LoadFile - this will load the assembly into a new "private" instance of AssemblyLoadContext. Ideally the loader would first try to load it via Assembly.Load (or even better AssemblyLoadContext.LoadAssemblyByName) so that the custom ALC logic can kick in. Note that Assembly.Load normally loads into the ALC of the caller, in this case that would be Default, but this can be changed by using contextual reflection - for example AssemblyLoadContext.EnterContextualReflection. Only if that fails I think we should fall back to searching for the file ourselves, by looking next to the assembly (the way it works now). In that case it should definitely load the file into the same ALC as the assembly for which the serializer was generated - by using AssemblyLoadContext.LoadAssemblyByPath.

vitek-karas avatar Nov 27 '19 09:11 vitek-karas