How to use System.Management.Automation - c#

I have an Azure function that is supposed to trigger a Powershell script. In order to get this to work I user the System.Management.Automation package. Every time the function gets called, I get this exception:
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Management.Automation, Version=6.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.'
I have looked at where the path of the Nugget package points and this seems to be correct. I have also tried moving this code from the class library(.NET standard 2.0) to the function itself (.NET Core 2.1).
None of my colleagues has ever seen this before either.
At this point I have been looking into this for over a day and I still don't have even the slightest idea of what I am doing wrong here. Can anyone help?

This looks to me like one of the errors that occurs because the library has a deep dependency on a very specific version of the .NET Core runtime (entity framework is another where only newer versions need to wait for the runtime underneath to update). I see this is using 6.0.4 - let me see if I can repro. In the meantime would recommend calling PowerShell scripts using the Azure Functions PowerShell language support (trigger another function)

Related

Polly.Caching.Memory.MemoryCacheProvider throws System.TypeLoadException : method not implemented for one application

I'm using these pacakges:
PackageReference Include="Polly" Version="7.2.2"
PackageReference Include="Polly.Caching.Memory" Version="3.0.2"
I have a testproject that uses these packages and the code works.
When I try to integrate my code in an existing application with lots of other nuget packages, suddenly I get a TypeLoadException.
The method TryGet in the type Polly.Caching.Memory.MemoryCacheProvider from the assembly Polly.Caching.Memory, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc doesn't have an implementation.
I have been checking everything, but I can't find the cause of this.
The versions of Polly and Polly.Caching.Memory are aligned across all projects involved.
In the bin folder the correct version appears.
I looked at the code of the assembly and the code has an implementation for TryGet
Any suggestion on what I can do to further investigate this?
The packages were all correctly configured.
I got on the right track when I saw that the method was returning a tuple.
I figured that perhaps this second application was not using the correct versions of the same packages.
I changed :
<HintPath>..\Solutions\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
To
<HintPath>..\Solutions\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
Bingo!

Could not load file or assembly using .NET Core 2.0

I just start to write a new application with Net Core 2.0 and I have this structure:
ConsoleApp
Library
the ConsoleApp import the Library reference that is a dll, and Library import HtmlAgilityPack 1.8.0 that's the last version available.
When I run the ConsoleApp, I get this error:
Unhandled exception of type 'System.IO.FileNotFoundException' in Library.dll
Could not load file or assembly 'HtmlAgilityPack, Version=1.8.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a'. The specified file could not be found.
The file is available, as you can see in the image:
Seems that I was able to fix the "bug". To summarize, I have two applications, the first one is a .NET Core Console Application, and the second one is a .NET Core Library. So I've removed the second assembly (.NET Core Library), and I've created the same project as .NET Standard Library.
I got the same problem: when I run the Console Application, the compiler shows the error message:
Unhandled exception of type 'System.IO.FileNotFoundException' in Library.dll Could not load file or assembly 'HtmlAgilityPack, Version=1.8.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a'. The specified file could not be found.
So I've tried to install the HtmlAgilityPack also on the Console Application, and, like dark magic, this worked perfectly.
So I guess that I should add the dependency used on the Library imported also to the Console Application, because .NET Core must be sure that all the dependencies of the imported Library are compatible with .NET Core.
IMHO, this should be specified by Microsoft, and again, this problem should be handled by Visual Studio itself. Why do I need to add the dependencies twice?
Of course, I'm not going to accept my own answer; I prefer to wait for someone who explains this situation.

Azure Function referencing mixed-mode assembly

TL;DR: Is it possible to use mixed-mode assemblies in Azure Functions?
Details: I have an in-house mixed-mode assembly that I'm trying to use in an Azure Function. When I try to build, I get the following error:
------ Build started: Project: AzureDemo, Configuration: Debug x64 ------
AzureDemo -> C:\src\local\2017\AzureDemo\bin\x64\Debug\net462\bin\AzureDemo.dll
C:\Users\hugh\.nuget\packages\microsoft.net.sdk.functions\1.0.2\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(31,5): error : Could not load file or assembly 'file:///C:\src\local\2017\AzureDemo\bin\x64\Debug\net462\bin\AzureDemo.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Done building project "AzureDemo.csproj" -- FAILED.
For clarity, the in-house library is x64 and my demo project is also x64, so it's not an x86/x64 problem.
Also, when I take the code out of the Azure Function project and put it in a plain old .NET Core 2.0 project, it builds just fine. edit: it builds fine, but it still doesn't run (System.EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.). So I may be in deeper water than I thought.
I'm kind of afraid Azure Functions won't work with mixed-mode assemblies... but I haven't found any definitive statement to that effect.
I'm also a little concerned that the fact that it's using netstandard1.0 might have something to do with it.
The error is happening at build time when the SDK launches the build task to generate the build artifacts/function metadata.
I've opened this issue with the details to track the bug: https://github.com/Azure/azure-functions-vs-build-sdk/issues/131
Actual x64 CLI releases would also be required for successful local testing, and this is tracked here: https://github.com/Azure/azure-functions-cli/issues/117

Failure to resolve System.Security.Cryptography.CngKeyBlobFormat when using Npgsql with Mono

I'm trying to use Npgsql in a mono app (built for Mac in Xamarin Studio). I was previously using Npgsql 2.x, and that worked OK for me, though gave us cryptic errors related to SSL on another machine. So yesterday I upgraded to the latest (3.0.5) version of Npgsql, but now when I try to build, I get an error:
error MM2002: Failed to resolve
"System.Security.Cryptography.CngKeyBlobFormat
System.Security.Cryptography.CngKeyBlobFormat::get_EccPublicBlob()"
reference from "System.Core, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
I posted about this on the Xamarin forums, and was told "Poking around mono, mcs/class/System.Core/System.Security.Cryptography/CngKeyBlobFormat.cs exists but is not included in any build currently."
I'm still relatively new to mono, and don't know how to proceed from here. What do I need to do to use Npgsql in my mono Mac app?
"Poking around mono, mcs/class/System.Core/System.Security.Cryptography/CngKeyBlobFormat.cs exists but is not included in any build currently."
It means that the class is not included in this file.
Now, given that this class is in Microsoft's sources, you should create a pull request that links to that class (via relative path, like other classes are linked already), and remove the old Mono implementation.
NOTE: After you create your pull request (which is easy enough), you will be notified by a Jenkins bot if your change breaks the build or not. If it breaks it, most probably it will be because this class depends on some other one (then, to fix it, just add a new entry in the .sources file; keep doing this until it passes).
UPDATE: Given that you seemed unwilling to contribute to fix the problem yourself, I went ahead and fixed it myself proposing this pull request, which has been merged today. This means that Mono 4.3 (or 4.4) will contain the fix.

NSevriceBus Assembly Version Error

I have an issue when trying to load the NServiceBus assembly from a client machine. The software runs correctly from my development box but once rolled out I get the following error:
Could not load file or assembly 'NServiceBus, Version=2.0.0.1100, Culture=neutral, PublicKeyToken=9fc386479f8a226c' or one of its dependencies. The system cannot find the file specified.
The weird thing is I am not using version 2.0.0.1100, I am using the latest version 2.0.0.1219. It seems to be referencing the old version somewhere but I cannot find where. I have set the “Specific Version” flag to true on both the NServiceBus & NServiceBus.Core assemblies and have tried to explicitly set the version number in the app config, both with no luck. I don’t understand why it works fine locally but errors once rolled out. The project causing the error is a shared class library and it only appears to be erroring on some projects too.

Categories