-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AssemblyLoadContext does not provide proper isolation of types #41625
Comments
Tagging subscribers to this area: @vitek-karas, @agocke |
The problem is this line: runtime/src/libraries/System.CodeDom/src/System/CodeDom/Compiler/CompilerResults.cs Line 26 in 731eaf3
So the end result is that it looks almost like loading it into Default - it gets all its dependencies from Default. In the world of ALCs this compiler API should ideally provide a way to specify which ALC to load the file into. Or at the very least react to https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext.currentcontextualreflectioncontext?view=netcore-3.1 As for a workaround - if you can modify the code which calls the This is kind of related to #29842 as it's a similar type of problem (which doesn't have a workaround at least via the ContextualReflectionContext) |
I have modified the code to pass in an assembly load context but I still am getting type checks that fail. I have the entire default context into the new context but performing == on typeof still seems to fail. Shouldn't this be using the types from the context of the caller before the default? |
@groogiam I'm sorry but I don't think I understand the problem you're describing. So for example typeof(MyType) in itself has no ALC interaction. What matters is which ALC the assembly which has this code in it is loaded into - and then when the MyType is loaded and causes to load its assembly MyAssembly - how does that ALC respond to the request of loading MyAssembly. It's pretty hard to answer what's wrong without knowing a bit more about how your ALCs are setup and the code in question. One way to debug this is to print out which ALC the type/assembly is from. This can be done by calling |
@vitek-karas: may be I have a similar issue and you could help me: Say we want to implement the corner right image where Json.Net is already loaded into Default ALC, yet we want to load an assembly that refrences Json.Net, yet while its referenced Json.Net version and name are same we want to use ALC to load it from another path. Currently when we load Addon Default resolves Json.Net and does not give us a chance to load it from the path we want. How would one do such thing? |
There's a guide how to write plugins/hosts here: https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support You have two choices on where/how to determine isolation:
Note that the plugin project file needs to be aware of this. It needs to use the correct properties to mark dependencies it wants from the host (and thus those won't be copied to the build output of the plugin). Typically that should be anything from the core framework and the interface assembly which defines the interface with the host. |
Dear @vitek-karas: Yes, I am aware of general use pattern of AssemblyLoadContext, yet now please imagine the following situation:
The problem is One may expect that if you load the desired modified Json.Net first and than load Plugin AssemblyLoadContext would use the modified version. Yet it does not happen. |
I must admit I'm a bit confused, but let me describe how it "should" work:
It you need to diagnose this, you can try this: https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/collect-details |
Dear @vitek-karas: created a small prototype to illustrate my point here (compilable and runable) In it I:
|
The problem is here. This calls I think the confusion is that Unlike The resolution of an assembly reference is purely based on the ALC into which the calling assembly belongs - in this case you start with assembly in Default, and thus the reference resolution happens in the Default context. |
Dear @vitek-karas: even using
Even if I modify ALC Load with so that it always loads not
Still I only get ALC call for Main and no calls for Dependency(es)... Is there any workaround that could allow force resolution of all the dependencies using ALC? |
Sorry - I missed one additional problem. Please don't call If you replace Note that the code as above will not work either - it will actually load the This is generally why I would recommend that you build the plugin into a separate location (as shown by our sample) and load it from there using the |
I am running into an issue where I am trying to port some logic which creates dynamic assemblies from framework to core. In this particular scenario the code that creates the dynamic assembly is in a nuget package that use CodeDomCompiler. The assembly is loaded from CompilerResult.CompiledAssembly which appears to always use the default context. This also happens with typeof comparisons inside of the library that is being dynamically loaded. What I'm seeing is that the AssemblyLoadContext seems to have some shortcomings in terms of control of how it can be used to isolate and unload dynamic code in libraries. The AppDomain concept supported these scenarios because all the types load in the domain where internally consistent. Is there any way to mimic the isolation of an AppDomain for libraries where a developer does not directly control the dynamic assembly load? If not is there any plans to perhaps support this in the future. Thanks.
The text was updated successfully, but these errors were encountered: