You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to load multiple versions of .Net assembly module dependencies. #2083
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
I'm happy to move this to a feature-request, though I'm not 100% sure. Please advise. We've observed that when we mix 3rd-party module imports from ISV's, it's quite common that we hit assembly version collisions.
A very common example:
Module1 depends on Newton.Json, Version 6.0
Module2 depends on Newton.Json, Version 9.0
We can't use both these modules in the same session. If we import Module1, then Module2, the cmdlets from Module2 throw claiming assembly version conflicts with Newton.Json. Our current work around is to kill the powershell process and start over with the other module import.
Should powershell isolate module imports into their own AppDomains? I'm happy to implement a repo, if this is not already filed.
Reacted by Christoph Bergmeister, Patrick Meinecke, TylerLeonhardt, Vishal H, Daniel Schroeder, Andrew Arnott, Charles Willis, otogawakatsutoshi and Giorgi Chakhidze
Even if AppDomains are not supported in .NET core, the issue still remains. How can powershell support multiple versions of dlls imported in the same powershell process? I can't update 3rd party modules to use newer dlls versions. So I'm stuck having to explicitly know that I can never use Module1 and Module2 cmdlets in the same ps-session.
changed the title [-]Unable to load multiple versions of module dependencies.[/-][+]Unable to load multiple versions of .Net assembly module dependencies.[/+]on Aug 27, 2016
I have the same issue.
I have a "JsonSchemaValidation" module build by myself which should use:
Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
At the same time AzureRm.Profile uses:
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
PowerShell always uses the "newtonsoft.Json" dll from the module that is being imported first!
Looking in the appdomain, both DLLs are present.
In case of AzureRm.Profile is being imported first, my "JsonSchemaValidation" cmdlets does not work.
I have the same issue.
My binary module has a dependency on assembly version 8 (JSON.NET).
I use it together with another module, which has a dependency on same assembly version 6.
As soon as I Import-Module both of them, 2 different versions of the same assembly are loaded into the AppDomain.
This normally wouldn't present an issue, but my binary module uses features only available in version 8 of dependent assembly. This throws MissingMethodException.
Ilya (@iSazonov)
I tried to handle AssemblyResolve event on the current AppDomain. I redirect any previous version of dependent assembly to v8. Unfortunately, this is too late. This event handler kicks in when I run the first cmdlet from my module. At that stage, the current AppDomain already contains v6.
This would not happen in a normal executable, as assembly redirects or assembly resolve handler would kick in when application attempts to load v6.
Steve Lee (@SteveL-MSFT)
Is this issue still being looked at? It makes some of the modules unusable together in the same session.
It certainly does not solve this problem in general but If we talk specifically about Newton.Json then we might consider replacing it with System.Json.
Xenalite (@Xenalite) yes, this is an issue we are aware of and have had internal discussions about it. Dongbo Wang (@daxian-dbw) perhaps for now we can document some workarounds until we can address this in code
Dongbo Wang (@daxian-dbw) if you have a "hybrid" module, half binary and half PS script cmdlets, will the ps script cmdlets in module scope resolve the assemblies/types in the module ALC?
I was just wondering if there was a way for the assemblyresolver to recognize module scope and direct to the ALC for the module for scripts that are also in module scope. This way scripts could reference [mycustomtype] in the ALC and as long as they don't output the type directly but simply consume it, makes that kind of authoring a lot simpler.
Configure your Script or Module with an overlaying config engine. Uses Microsoft.Extensions.Configuration - GitHub - JustinGrote/PowerConfig: Configure your Script or Module with an overlaying conf...
Justin Grote (@JustinGrote) The PR #16889 and actually all the solutions described in this article only take into account a pure binary module. If the script coming with the module only needs to access the types that are owned by the module assemblies (not those from the conflicting dependency assemblies), then they will work as expected, because the module assemblies (referenced by RootModule or NestedModules) are discoverable to the type resolver.
However, if the module script needs to access types from the conflicting dependencies, all the above solutions won't work, because the conflicting assemblies are intentionally loaded to a separate ALC that is not discoverable to the type resolver, to avoid the type-identity issue.
I was just wondering if there was a way for the assemblyresolver to recognize module scope and direct to the ALC for the module for scripts that are also in module scope. This way scripts could reference [mycustomtype] in the ALC and as long as they don't output the type directly but simply consume it, makes that kind of authoring a lot simpler.
This is also discussed in the RFC. It requires PowerShell to know that a module is loaded into a custom ALC. We will not pursue a solution for the end user to decide when to do this, but there is still room to discuss whether we can add a new key in the module manifest to allow a module to ask PowerShell to always load it into a custom ALC. If we do that, then PowerShell knows the module is loaded into a custom ALC and can do the following special things (described in the RFC):
type resolution goes through the custom ALC first
type loading happening in the module script will be targeting the custom ACL (Add-Type can be updated to work this way implicitly, but the assembly loading APIs won't work well, such as Assembly.LoadFrom and Assembly.LoadFile).
powershell class needs to be loaded into the custom ACL
That would require significant amount of work in PowerShell engine (type caching invalidation is also needed), and there will be lots of corner cases too, for example, the type resolution happening during processing a type.ps1xml file of a module will need to be handled specially because technically there is no module scope at that point yet. And even this works, it works under the assumption that the module doesn't expose types from the conflicting dependencies to outside of the module (as parameter type or return type). Even if the module author can make sure the assumption is satisfied, an end user can run arbitrary script in a module scope by & $moduleInfo { .... }, so types from conflicting dependencies can still be leaked to outside of the module. So, it won't be a bulletproof solution either.
That being said, I think it's interesting to think about this "add a new key in the module manifest to allow a module to ask PowerShell to always load it into a custom ALC" idea. It will be much more simplified -- we don't need to add functionalities to make the type-identity issue manageable, neither do we need to worry about the root module issue. Maybe I will reuse some content from PowerShell/PowerShell-RFC#298 to submit another RFC for this, but again, it's low priority.
That being said, I think it's interesting to think about this "add a new key in the module manifest to allow a module to ask PowerShell to always load it into a custom ALC" idea. It will be much more simplified -- we don't need to add functionalities to make the type-identity issue manageable, neither do we need to worry about the root module issue. Maybe I will reuse some content from PowerShell/PowerShell-RFC#298 to submit another RFC for this, but again, it's low priority.
Since this is a fundamental problem stemming from .Net itself, perhaps it should be discussed there instead of here.
That being said, I think it's interesting to think about this "add a new key in the module manifest to allow a module to ask PowerShell to always load it into a custom ALC" idea. It will be much more simplified -- we don't need to add functionalities to make the type-identity issue manageable, neither do we need to worry about the root module issue. Maybe I will reuse some content from PowerShell/PowerShell-RFC#298 to submit another RFC for this, but again, it's low priority.
Since this is a fundamental problem stemming from .Net itself, perhaps it should be discussed there instead of here.
In this case all of the implementation would be in Powershell, .NET provides the ALC context and it's just a matter of enabling a whole module to load into its own ALC context, similar to the old assembly plug-in model of .NET framework.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 23, 2023
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
rkeithhill commentedon Aug 26, 2016
IIRC AppDomains are not supported in .NET Core.
curiousdev commentedon Aug 26, 2016
Even if AppDomains are not supported in .NET core, the issue still remains. How can powershell support multiple versions of dlls imported in the same powershell process? I can't update 3rd party modules to use newer dlls versions. So I'm stuck having to explicitly know that I can never use Module1 and Module2 cmdlets in the same ps-session.
[-]Unable to load multiple versions of module dependencies.[/-][+]Unable to load multiple versions of .Net assembly module dependencies.[/+]gogbg commentedon Jan 2, 2018
Any progress on this?
I have the same issue.
I have a "JsonSchemaValidation" module build by myself which should use:
Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
At the same time AzureRm.Profile uses:
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
PowerShell always uses the "newtonsoft.Json" dll from the module that is being imported first!
Looking in the appdomain, both DLLs are present.
In case of AzureRm.Profile is being imported first, my "JsonSchemaValidation" cmdlets does not work.
iSazonov commentedon Jan 2, 2018
Georgi Ivanov (@gogbg) You could try to use
bindingRedirect.Also it is expected that AppDomain will come back in .Net Core 2.1.0.
oskarm93 commentedon Feb 1, 2018
I have the same issue.
My binary module has a dependency on assembly version 8 (JSON.NET).
I use it together with another module, which has a dependency on same assembly version 6.
As soon as I
Import-Moduleboth of them, 2 different versions of the same assembly are loaded into the AppDomain.This normally wouldn't present an issue, but my binary module uses features only available in version 8 of dependent assembly. This throws
MissingMethodException.Ilya (@iSazonov)
I tried to handle
AssemblyResolveevent on the current AppDomain. I redirect any previous version of dependent assembly to v8. Unfortunately, this is too late. This event handler kicks in when I run the first cmdlet from my module. At that stage, the current AppDomain already contains v6.This would not happen in a normal executable, as assembly redirects or assembly resolve handler would kick in when application attempts to load v6.
Steve Lee (@SteveL-MSFT)
Is this issue still being looked at? It makes some of the modules unusable together in the same session.
iSazonov commentedon Feb 1, 2018
It certainly does not solve this problem in general but If we talk specifically about Newton.Json then we might consider replacing it with System.Json.
SteveL-MSFT commentedon Feb 1, 2018
Xenalite (@Xenalite) yes, this is an issue we are aware of and have had internal discussions about it. Dongbo Wang (@daxian-dbw) perhaps for now we can document some workarounds until we can address this in code
84 remaining items
JustinGrote commentedon Mar 10, 2022
Dongbo Wang (@daxian-dbw) if you have a "hybrid" module, half binary and half PS script cmdlets, will the ps script cmdlets in module scope resolve the assemblies/types in the module ALC?
iSazonov commentedon Mar 10, 2022
Justin Grote (@JustinGrote) It is for binary modules. See follow great comment in code
PowerShell/src/System.Management.Automation/engine/ExecutionContext.cs
Line 1281 in 25bc64f
JustinGrote commentedon Mar 10, 2022
Ilya (@iSazonov) right, but my point is, you can make a binary module that also references scripts. Example:
https://github.com/justingrote/powerconfig
I was just wondering if there was a way for the assemblyresolver to recognize module scope and direct to the ALC for the module for scripts that are also in module scope. This way scripts could reference [mycustomtype] in the ALC and as long as they don't output the type directly but simply consume it, makes that kind of authoring a lot simpler.
daxian-dbw commentedon Mar 10, 2022
Justin Grote (@JustinGrote) The PR #16889 and actually all the solutions described in this article only take into account a pure binary module. If the script coming with the module only needs to access the types that are owned by the module assemblies (not those from the conflicting dependency assemblies), then they will work as expected, because the module assemblies (referenced by
RootModuleorNestedModules) are discoverable to the type resolver.However, if the module script needs to access types from the conflicting dependencies, all the above solutions won't work, because the conflicting assemblies are intentionally loaded to a separate ALC that is not discoverable to the type resolver, to avoid the type-identity issue.
This is also discussed in the RFC. It requires PowerShell to know that a module is loaded into a custom ALC. We will not pursue a solution for the end user to decide when to do this, but there is still room to discuss whether we can add a new key in the module manifest to allow a module to ask PowerShell to always load it into a custom ALC. If we do that, then PowerShell knows the module is loaded into a custom ALC and can do the following special things (described in the RFC):
Add-Typecan be updated to work this way implicitly, but the assembly loading APIs won't work well, such asAssembly.LoadFromandAssembly.LoadFile).That would require significant amount of work in PowerShell engine (type caching invalidation is also needed), and there will be lots of corner cases too, for example, the type resolution happening during processing a
type.ps1xmlfile of a module will need to be handled specially because technically there is no module scope at that point yet. And even this works, it works under the assumption that the module doesn't expose types from the conflicting dependencies to outside of the module (as parameter type or return type). Even if the module author can make sure the assumption is satisfied, an end user can run arbitrary script in a module scope by& $moduleInfo { .... }, so types from conflicting dependencies can still be leaked to outside of the module. So, it won't be a bulletproof solution either.That being said, I think it's interesting to think about this "add a new key in the module manifest to allow a module to ask PowerShell to always load it into a custom ALC" idea. It will be much more simplified -- we don't need to add functionalities to make the type-identity issue manageable, neither do we need to worry about the root module issue. Maybe I will reuse some content from PowerShell/PowerShell-RFC#298 to submit another RFC for this, but again, it's low priority.
iSazonov commentedon Mar 11, 2022
Since this is a fundamental problem stemming from .Net itself, perhaps it should be discussed there instead of here.
JustinGrote commentedon Mar 11, 2022
In this case all of the implementation would be in Powershell, .NET provides the ALC context and it's just a matter of enabling a whole module to load into its own ALC context, similar to the old assembly plug-in model of .NET framework.
daxian-dbw commentedon Sep 8, 2022
Quote from #2083 (comment):
The work has been done to the v2
platyPSrepo by PowerShell/platyPS#587 and PowerShell/platyPS#588microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.