Repository navigation
Description
While using only System.Management.Automation as a dependency for an application of mine (without the entire SDK because I didn't need it), I was getting sometimes a NullReferenceException that was thrown in this line of code:
| return type.GetMethod(method, BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase); |
because type was null.
Digging in the stack trace, I found out that the null reference was originated by the following instruction:
| Type securityDescriptorCommandsBaseType = TypeResolver.ResolveType("Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase", exception: out _); |
The securityDescriptorCommandsBaseType variable gets passed to the method GetMethodInfo, which contains the return statement with the variable type shown above.
Once you know that the referenced type (Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase) belongs to the Microsoft.PowerShell.Security package, and that the latter depends on System.Management.Automation, it becomes apparent that TypeResolver.ResolveType was being used in place of typeof to avoid a circular dependency at compile time between the two assemblies.
The problem is that the circular dependency still exists and causes the NRE mentioned above when System.Management.Automation is used alone.
Therefore, since I think that probably that dependency can't be broken, I would propose to "loosen" it by adding a null-check that prevents all the code related to the securityDescriptorCommandsBaseType from being executed when Microsoft.PowerShell.Security assembly is not available (which should be lines 4074-4128 or even 4074-4210, I'm not sure).
I'm willing to open a PR to mitigate this problem after receiving your feedback on it, since - unlike me - you know the context and the motivation of the code I mentioned throughout this issue.
Activity
iSazonov commentedon Nov 15, 2020
Dongbo Wang (@daxian-dbw) Could you please comment?
Fs00 commentedon Feb 10, 2021
Hello Dongbo Wang (@daxian-dbw), could you please take a look at this and see if my proposed solution could be enough to solve it?
I don't want to bother you, but this issue is getting stale and I would like to see it fixed (if it's possible to).
TravisEz13 commentedon Mar 8, 2021
Currently, the security assembly is required. We are looking at this type of feature in the future.
Fs00 commentedon Mar 8, 2021
Ok, so - if I understood correctly - the proper fix would consist in fully decoupling those two assemblies, right?
TravisEz13 commentedon Mar 9, 2021
I'll let Dongbo Wang (@daxian-dbw) talk to what the correct fix is.