How to use in Powershell?
SamLowryMOI opened this issue · 10 comments
I would like to use the client in Powershell, may you advise me?
I tried this:
if ($PSVersionTable.PSVersion.Major -gt 5) {
Add-Type -Path "$PSScriptRoot\net6.0\Ae.Dns.Protocol.dll"
Add-Type -Path "$PSScriptRoot\net6.0\Ae.Dns.Client.dll"
}
else {
Add-Type -Path "$PSScriptRoot\netstandard2.1\Ae.Dns.Protocol.dll"
Add-Type -Path "$PSScriptRoot\netstandard2.1\Ae.Dns.Client.dll"
}
$httpClient = [System.Net.Http.HttpClient]::new()
$httpClient.BaseAddress = [uri]::new('https://cloudflare-dns.com/')
$dnsCloudFlareClient = [Ae.Dns.Client.DnsHttpClient]::new($httpClient)
$query = [Ae.Dns.Protocol.DnsQueryFactory]::CreateQuery("google.com")
$answer = $dnsCloudFlareClient.Query($query, [System.Threading.CancellationToken]::None).GetAwaiter().GetResult()
$answer.ToString()
In Powershell 7 it works.
In Windows Powerhell 5.1 these calls fail:
Add-Type -Path "$PSScriptRoot\netstandard2.1\Ae.Dns.Protocol.dll"
Error:
Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
LoaderExceptions
Could not load type 'System.ReadOnlyMemory`1' from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
Could not load type 'System.ReadOnlyMemory`1' from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
Could not load type 'System.ReadOnlyMemory`1' from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
and
Add-Type -Path "$PSScriptRoot\netstandard2.1\Ae.Dns.Client.dll"
Error:
Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
LoaderExceptions
$_.Exception.LoaderExceptions
Method 'Query' in type 'Ae.Dns.Client.DnsRawClient' from assembly 'Ae.Dns.Client, Version=0.0.17.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
Could not load type 'System.ReadOnlyMemory`1' from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
Could not load type 'System.Memory`1' from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
Could not load type 'System.Runtime.CompilerServices.ValueTaskAwaiter`1' from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
Could not load type 'System.Runtime.CompilerServices.ValueTaskAwaiter`1' from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
-edit
# This is only possible in the terminal in Visual Studio Code, as if by magic.
[System.ReadOnlyMemory[byte]] $rom = [System.ReadOnlyMemory[byte]]::new([byte[]](1..10))
[System.Memory[byte]] $mem = [System.Memory[byte]]::new([byte[]](1..10))
The errors are probably due to netstandard2.1 instead of netstandard2.0 (documentation still says netstandard2.0).
Do I have to port the whole thing back to netstandard2.0? Then how can I replace ReadOnlyMemory, etc.?
Do you have any ideas?
If this is just a case of re-adding support for netstandard2.0
, that makes sense as a feature request and I can look at doing that (PRs welcome also).
If it's anything more obscure (that is PowerShell specific and can't be easily tested outside of PowerShell) I'd push back on it.
that makes sense as a feature request
YES - um, I mean, yes please.
$_.Exception.LoaderExceptions
I took a closer look at the error messages. They all seem to be caused by Windows Powershell 5.1 not supporting netstandard2.1.
-edit
This is only possible in the terminal in Visual Studio Code, as if by magic.
[System.ReadOnlyMemory[byte]] $rom = [System.ReadOnlyMemory[byte]]::new([byte[]](1..10)) [System.Memory[byte]] $mem = [System.Memory[byte]]::new([byte[]](1..10))
It would be really nice if you could make it run for netstandard2.0!
Unfortunately my skills are nowhere near as good as yours, otherwise I'd be happy to support you.
Please test https://github.com/alanedwardes/Ae.Dns/tree/netstandard2.0
It builds and appears functional, there are some network bugs which I'm trying to fix. I'm not sure if I'll finish it.
I noticed you commented but later deleted the comment - I hope that means good news!
The netstandard2.0
branch was merged into main
and is tested with the full suite passing under netcoreapp2.1
. Going back to my original message:
If it's anything more obscure (that is PowerShell specific and can't be easily tested outside of PowerShell) I'd push back on it.
If the library still doesn't work with PowerShell, that says to me that PowerShell doesn't properly implement netstandard2.0
and may need something weird doing to support it (in which case it makes less sense as a feature request - I wouldn't rule it out but it gets into the "high effort low reward" territory).
Under netstandard2.0
there is a System.Memory
package which "fills in the gaps" for the memory types, which is included here: https://github.com/alanedwardes/Ae.Dns/blob/main/src/Ae.Dns.Protocol/Ae.Dns.Protocol.csproj#L18
Somehow it works and somehow it doesn't. I still have a few things to try; I already found the System.Memory
package, but I don't have the time for further testing right now. I'll get back to you by the weekend at the latest.
From version >=0.19 the client can be used in both Windows Powershell and Powershell Core. Very nice!
I am in the process of "translating" the examples to Powershell. And there is already my first question.
There is the restriction
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
[...]
Why?
Windows Powershell 5.1 (and thus, in my opinion, netstandard2.0) can handle even versions 7.0.0 of the above dlls without any problems. Is the restriction to version 6.0.0 possible? Otherwise you can only use logging in netstandard2.0 in a somewhat awkward way.
From version >=0.19 the client can be used in both Windows Powershell and Powershell Core. Very nice!
I am in the process of "translating" the examples to Powershell. And there is already my first question.
There is the restriction
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" /> [...]
Why?
Windows Powershell 5.1 (and thus, in my opinion, netstandard2.0) can handle even versions 7.0.0 of the above dlls without any problems. Is the restriction to version 6.0.0 possible? Otherwise you can only use logging in netstandard2.0 in a somewhat awkward way.
There are certain Microsoft packages that don't let you mix and match across versions like this - probably the packages in question are fine, but this way I don't have to think about that compatibility. Your application can pull in whatever version of the dependencies it wants though, the only restriction is "greater than or equal to": https://www.nuget.org/packages/Ae.Dns.Client
In other words, your application can force the netstandard2.0
version of the library up to Microsoft.Extensions.DependencyInjection.Abstractions
version 7.0.0
without issue.
There are certain Microsoft packages that don't let you mix and match across versions like this - probably the packages in question are fine, but this way I don't have to think about that compatibility. Your application can pull in whatever version of the dependencies it wants though, the only restriction is "greater than or equal to": https://www.nuget.org/packages/Ae.Dns.Client
In other words, your application can force the
netstandard2.0
version of the library up toMicrosoft.Extensions.DependencyInjection.Abstractions
version7.0.0
without issue.
Ok. One problem remains. To enable logging in the console, I use the 6.0 versions of the dlls. However, the dll
Microsoft.Extensions.Logging.Console, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
absolutely expects this dll
System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
while Ae.Dns.Protocol
expects the version
System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
.
Is it possible to change the default in file Ae.Dns.Protocol.csproj
<PackageReference Include="System.Memory" Version="4.5.5" />
to
<PackageReference Include="System.Memory" Version="4.5.4" />
?
The same problem exists in C#.
Can you give this branch a test: #12
Thanks for the branching and my apologies for the late reply.
--- edit ---
After getting it running, I have to say that the change doesn't make much difference.
While Powershell Core accepts the "greater than or equal to" rule for dll versions, Windows Powershell wants exactly the same version. But you can load multiple versions of the same dll in Windows Powershell (I couldn't believe it). So with all the dlls you have to load, one more or less doesn't make a difference either. So everything may stay as it is.