Repository navigation
Add-Type failed to create a type that implements IWebProxy #2764
Copy link
Copy link
Closed
Labels
Blockedblocked on something external to this repoblocked on something external to this repoResolution-FixedThe issue is fixed.The issue is fixed.WG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issues
Milestone
Description
This is a regression introduced by .NET Core 1.1 preview release libraries. It also happens on alpha.12 release.
Steps to reproduce
$RequiredAssembliesForInternalWebProxy = @( [System.Net.IWebProxy].Assembly.FullName,
[System.Uri].Assembly.FullName )
$SourceForInternalWebProxy = @"
using System;
using System.Net;
namespace Microsoft.PowerShell.Commands.PowerShellGet
{
/// <summary>
/// Used by Ping-Endpoint function to supply webproxy to HttpClient
/// We cannot use System.Net.WebProxy because this is not available on CoreClr
/// </summary>
public class InternalWebProxy : IWebProxy
{
Uri _proxyUri;
ICredentials _credentials;
public InternalWebProxy(Uri uri, ICredentials credentials)
{
Credentials = credentials;
_proxyUri = uri;
}
/// <summary>
/// Credentials used by WebProxy
/// </summary>
public ICredentials Credentials
{
get
{
return _credentials;
}
set
{
_credentials = value;
}
}
public Uri GetProxy(Uri destination)
{
return _proxyUri;
}
public bool IsBypassed(Uri host)
{
return false;
}
}
}
"@
Add-Type -ReferencedAssemblies $RequiredAssembliesForInternalWebProxy `
-TypeDefinition $SourceForInternalWebProxy `
-Language CSharp `
-ErrorAction SilentlyContinueExpected behavior
Succeeds
Actual behavior
Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more
information.
At line:1 char:1
+ Add-Type -ReferencedAssemblies $RequiredAssembliesForInternalWebProxy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
+ FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCommand
PS D:\>
PS D:\> $Error[0].Exception.LoaderExceptions
Method 'GetProxy' in type 'Microsoft.PowerShell.Commands.PowerShellGet.InternalWebProxy' from assembly 'nxg304ea.wnz,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.Environment data
> $PSVersionTable
Name Value
---- -----
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSVersion 6.0.0-alpha
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
GitCommitId v6.0.0-alpha.12-104-g67e9bf6376b90c289d725c904280a2439de4dd3b
PSEdition Core
BuildVersion 3.0.0.0
CLRVersion
SerializationVersion 1.1.0.1Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Blockedblocked on something external to this repoblocked on something external to this repoResolution-FixedThe issue is fixed.The issue is fixed.WG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issues
Activity
daxian-dbw commentedon Nov 22, 2016
Manikyam Bavandla (@bmanikm) and I talked about why we didn't catch it earlier, and we find
alpha.11in PowerShellGet repo.vors commentedon Dec 1, 2016
As a workaround, we can try to leverage PS classes, instead of
Add-Typevors commentedon Dec 3, 2016
Here is what I found, when decompiled the assembly generated by
Add-Typevsdotnet cli.GetProxy() dotnet (good)
GetProxy() Add-Type (bad)
The only difference is that
System.Urilives inSystem.Private.Uriassembly (implementation assembly) and not in the reference assembly (System.Runtime)Since we currently don't ship reference assemblies together with PowerShell, we cannot make
Add-Typecompile against them.That could potentially affect other inheritance cases.
Still, it's not clear how to mitigate it and why it worked before and suddenly stopped now.
Dongbo Wang (@daxian-dbw) can you follow-up about it with dotnet team, please?
Note: the generated IL is the same in
alpha.11, whereAdd-Typeversion works.Note: here is the IWebProxy disasm (also uses
System.Runtime)vors commentedon Dec 3, 2016
I verified that it's a dotnet core (not corefx) regression: if I copy-replace
System.Private.CoreLib.dllin alpha.11 to the newer version, I get exactly the same errors.vors commentedon Dec 3, 2016
I just tried it on the latest build from master and it's gone. O_o
1 remaining item
vors commentedon Dec 3, 2016
I was trying to repro the problem outside the PowerShell context, but I could not. :(
Both
dotnet runanddotnet publishare able to load the types from the assembly.Maybe somebody else may be able to do it.
project.json
{ "name": "webproxy", "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "frameworks": { "netcoreapp1.0": { "dependencies": { "System.Net.Primitives": "4.3.0-preview1-24530-04", "Microsoft.NETCore.App": "1.1.0-preview1-001100-00", "Microsoft.CodeAnalysis.CSharp": "1.3.1-beta1-20160616-03", "System.Runtime.Loader": "4.3.0-preview1-24530-04" } } }, "runtimes": { "osx.10.11-x64": { } } }Program.cs
daxian-dbw commentedon Dec 6, 2016
An update from Manikyam Bavandla (@bmanikm): there were 2
Add-Typeuses inPowerShellGetpreviously. Now he merged those 2 into one C# code snippet and use only oneAdd-Type, andAdd-Typestarted to work even though the sameInternalWebProxyimplementation is used. SoPowerShellGetis unblocked for now (at least for alpha.14 release), but this bug still stands.Manikyam Bavandla (@bmanikm), please add more details if I miss anything.
brywang-msft commentedon Feb 3, 2017
Adding another repro. Fairly sure this is a dotnet core issue, but it could be a dotnet core fix exposing an Add-Type issue? As above, we've found the issue doesn't repro in alpha11. I copied all the dotnet core DLLs from alpha11 into alpha15, and the issue doesn't repro. I don't know how type forwarding manifests in disassemblers, but ildasm, at least, shows
[System.Private.Uri]System.Uriin ProgTest.dll (compiled in alpha11 and alpha15) instead of[System.Runtime]System.Urithat's present in other disassembled assemblies (e.g. System.Net.Http.dll's HttpClient).Steps to reproduce
Contents of Test.cs:
In PowerShell alpha15:
Expected behavior
No error from test script above
Actual behavior
[-]Add-Type failed to create a type that implementing IWebProxy[/-][+]Add-Type failed to create a type that implements IWebProxy[/+]Move powershell to .NET Core 2.0 (#3556)
daxian-dbw commentedon Apr 17, 2017
Fixed via #3556:
Move powershell to .NET Core 2.0 (PowerShell#3556)