unable to import and use RestSharp in Azure functions using Nuget - c#

So the situation is as follows:
I have a azure-botbuilder created chatbot running on a consumption-plan based Azure function. Now I would like to use RestSharp to consume and send requests for REST APIs.
However I followed all the instructions to install NuGet Packages for RestSharp and add the requisite dependencies in my project.json, and the "using RestSharp;" import statement inside the code, however for some reason it seems unable to import the library and always gives the error "The type or namespace name 'RestSharp' could not be found (are you missing a using directive or an assembly reference?)"
Heres my project.json:
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Bot.Builder.Azure": "3.2.5",
"RestSharp": "105.2.3"
}
}
}
}

If you make an Azure function in version 2.x you need to do this in a different way.
Create a new file called function.proj. This file has an XML structure for importing libraries via Nuget.
Here is my example importing RestSharp.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RestSharp" Version="106.6.8"/>
</ItemGroup>
</Project>
Once you hit save the console will tell you that it is restoring packages. Just wait a minute for it to finish. Mine had a compilation error and Azure restarted the package restore itself and eventually compiled successfully after a minute or so.
Here is where I found this solution: https://stackoverflow.com/a/53053897/4821686

Related

Could not load file or assembly Microsoft.Data.SqlClient, Version=5.0.0.0

Facing following issue after deploying app to azure app service:
Unhandled exception. System.IO.FileNotFoundException: Could not load
file or assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0,
Culture=neutral, PublicKeyToken=***********'. The system cannot find
the file specified.
Locally everything works as fine.
Microsoft.Data.SqlClient presented in site/wwwroot
Runtime Stack: Dotnetcore - 6.0
Main app and all class libs on .net6
Server Operating System: Linux
Microsoft.Data.SqlClient presented as reference from Microsoft.EntityFrameworkCore.SqlServer (v.7.0.2)
Tried different kinds of Nuget Packages versions - still have same issue
Tried to install Microsoft.Data.SqlClient (latest version & 5.0.0.0) directly into projects - still the same
The solution for us was to:
Locate the actual DLL file (Microsoft.Data.SqlClient.dll) on the developer's desktop filesystem (easy to find; once you're referenced it using NuGet it gets copied to multiple places)
Add it to the web project (we placed it right in the root)
Mark it (via Properties tool window) as Copy Always
Step 3 results in our .csproj file looks like this:
<ItemGroup>
<None Update="Microsoft.Data.SqlClient.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

EPPlus: System.Text.Encoding.CodePages.dll not found in .Net Core 3.1 Azure function

Using VS2019 I have generated an Azure Function project. The Azure function is triggered by an HTTP request to convert CSV content to an XLSX file using EPPlus.
The code is really really simple:
[FunctionName("Function1")]
public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
// line below will raise "System.Text.Encoding.CodePages.dll" not found exception
ExcelPackage xcel = new ExcelPackage();
// ... some other code
}
The ExcelPackage instanciation will raise the following error:
"Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified."
EPPlus is relying System.Text.Encoding.CodePages 5.0.0.0 but looks like .Net core 3.1 is relying on a 4.x
version.
The workaround I thought and tested successfully was just to copy System.Text.Encoding.CodePages.dll in the bin folder of my solution. But very unsatisfactory because every time I am rebuilding the solution, I have to deploy the System.Text.Encoding.CodePage.dll manually.
I have tried different things I read about binding redirect but unsuccessfully. Not very surprising as binding redirect is more .net framework related.
This drives me nuts.
In your Visual Studio Solution Explorer, right-click Azure Functions Project and click on "Edit Project File". Then, add an ItemGroup to preserve System.Text.Encoding.CodePages.dll dependency, like this:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<FunctionsPreservedDependencies Include="System.Text.Encoding.CodePages.dll" />
</ItemGroup>
Be happy!
The ExcelPackage instanciation will raise the following error: "Could
not load file or assembly 'System.Text.Encoding.CodePages,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
The system cannot find the file specified." EPPlus is relying
System.Text.Encoding.CodePages 5.0.0.0 but looks like .Net core 3.1 is
relying on a 4.x version.
This is no matter with the version of the System.Text.Encoding.CodePages package. I can explain this situation to you(In fact, it is related to the after-build operation of azure function tools in VS 2019.).
In fact, the System.Text.Encoding.CodePage.dll file has already been copied to the .\bin\Release\netcoreapp3.1\bin folder after the azure function app is built. But, azure function tools did an operation, it removed many files from .\bin\Release\netcoreapp3.1\bin after the azure function app is built.
The workaround I thought and tested successfully was just to copy
System.Text.Encoding.CodePages.dll in the bin folder of my solution.
But very unsatisfactory because every time I am rebuilding the
solution, I have to deploy the System.Text.Encoding.CodePage.dll
manually.
You can try to use the 'build' operation instead of the 're-build' operation. 'build' operation will not remove the System.Text.Encoding.CodePage.dll after you put it in the bin folder.
It's not your fault, I think this is a design error of the azure function tool. If the delete operation is not automatically performed after the build operation, no problems will occur. Hope my answer can answer your doubts.
And you can also give a feedback of this problem.
(I think the code you don't need, so I won’t post it. I have do a test, works fine.)
I had the same problem while trying a similar approach.
The solution was "quite simple" and a bit different than the answer Bowman gave.
System.Text.Encoding.CodePages, Version=5.0.0.0 is a package made for .NET5 framework. It references:
Microsoft.NETCore.Platforms (>= 5.0.0)
System.Runtime.CompilerServices.Unsafe (>= 5.0.0)
I think the package is not fully compatible with .NetCore 3.1, as apparently it says.
The solution I found is quite simple, just reference the version 4.71 witch supports the runtime used by Azure Functions on NetCore 3.1 (not NET5).

CS1069: The type name 'RegistryKey' could not be found in the namespace

I use VS 2019 .NET Core app and getting this type of error
CS1069: The type name 'RegistryKey' could not be found in the namespace "Microsoft.Win32". This type has been forwarded to assembly 'Microsoft.Win32.Registry, version=4.1.3.0, Culture="neutral, PublicKeyToken=b03f5f711d50a3a' Consider adding a reference to that assembly.
What causing it?
Did you install the NuGet Package Microsoft.Win32.Registry?
The "Registry" has not be part of .NET Core. For more info, you can refer to Use the Windows Compatibility Pack to port code to .NET Core.
Well, as the message itself puts it, it´s just a matter of adding a reference to that assembly. Add this to your .csproj:
<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="4.4.0" />
</ItemGroup>

VS2019 extension using IDebugEventCallback2

I created a new VS extension using VS2019 16.1.6.
and I added this using statement
using Microsoft.VisualStudio.Debugger.Interop;
and added the interface IDebugEventCallback2 to my class
public sealed class VSIXProject1Package : AsyncPackage, IDebugEventCallback2
Not I get the error:
error CS0433: The type 'IDebugEventCallback2' exists in both 'Microsoft.VisualStudio.Debugger.Interop, Version=8.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'Microsoft.VisualStudio.Debugger.InteropA, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
How can I get rid of this error? Or is there an other way to react to debugger events than using IDebugEventCallback2?
edit:
Problem reported to Microsoft:
https://developercommunity.visualstudio.com/content/problem/651199/vs2019-extension-using-idebugeventcallback2.html
VS2019 uses PackageReference format to manage nuget packages for VSIX project.
And by default it will reference Microsoft.VisualStudio.SDK and Microsoft.VSSDK.BuildTools package.Also, since Microsoft.VisualStudio.SDK package have dependencies on many other packages, this project will also reference those packages.
See this simple structure:
Microsoft.VisualStudio.SDK
......(other dependencies)
--Microsoft.VisualStudio.Debugger.Interop
--Microsoft.VisualStudio.OLE.Interop
--Microsoft.VisualStudio.Debugger.Interop.10.0
--Microsoft.VisualStudio.Debugger.InteropA
......(11.0,12.0,14.0,15.0)
--Microsoft.VisualStudio.Debugger.Interop.16.0
--Microsoft.VisualStudio.Debugger.InteropA
So it's clear this issue results from the VSIX project adds reference to both Microsoft.VisualStudio.Debugger.Interop and Microsoft.VisualStudio.Debugger.InteropA.
These two assemblies have the same namespace Microsoft.VisualStudio.Debugger.Interop, and all have IDebugEventCallback2 Interface. I think it's why causes this issue.
As a workaround:
Normal we can use extern alias for this situation. But it hasn't supported for PackageReference format yet. Fortunately I found a good hint from gertjvr. So all we need is:
Unload the project=>Edit the xxx.csproj=>Add the content below into the project file:
<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'Microsoft.VisualStudio.Debugger.Interop'">
<Aliases>signed</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
You can change the ReferencePath to Microsoft.VisualStudio.Debugger.InteropA if you want to use the Interface from this assembly. It depends on your need.

Blazor Client problem with mscorlib.dll / mono-runtime

I have project created with an blazorhosted template.
Yesterday for some reason Client side had problem with compiling due to like 500 characters error message
I clicked on it twice, and it opened iirc something like mono runtime config file in C:\ProgramFiles\dotnet\...\0.7 folder with weird lines like {blazor smth} and I removed something and unfortunely saved that and went sleep...
I also remember that that 500 characters long error mentioned Linker
Now after starting my app it just shows "Loading..." page and error on web browser console
WASM: The assembly mscorlib.dll was not found or could not be loaded.
blazor.webassembly.js:1:32055 WASM: It should have been installed in
the
`/mnt/jenkins/workspace/test-mono-mainline-wasm/label/ubuntu-1804-amd64/sdks/out/wasm-runtime-release/lib/mono/4.5/mscorlib.dll'
directory. blazor.webassembly.js:1:32055 Error: Failed to start
platform. Reason: [object XMLHttpRequest]
I totally understand that it may be hard to debug it, but maybe somebody has an idea? or how can I reinstall or repair Blazor / Mono?
I just downloaded and installed .NET Core 3.0 (previously had 2.x)
But still the above mentioned error occurs.
If you want use Blazor with .NET Core 3.0 (at least Preview 2) then you essentially use Razor Components + Blazor. It require for you use version of Blazor only from dev feed and not from official Nuget which will work just on .NET Core 2.1 (not on 2.2).
In order to use Blazor with .NET Core 3.0 you should add following changes to your project
Add RestoreAdditionalProjectSources property
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>7.3</LangVersion>
<RestoreAdditionalProjectSources>
https://dotnet.myget.org/F/dotnet-core/api/v3/index.json;
https://dotnet.myget.org/f/blazor-dev/api/v3/index.json;
</RestoreAdditionalProjectSources>
</PropertyGroup>
Change Blazor references to
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="0.8.0-preview-19075-0444" />
<PackageReference Include="Microsoft.AspNetCore.Components.Browser" Version="3.0.0-preview-19075-0444" />
<PackageReference Include="Microsoft.AspNetCore.Components.Build" Version="3.0.0-preview-19075-0444" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="0.8.0-preview-19075-0444" PrivateAssets="all" />
</ItemGroup>
For me that happened when i edited index.html with some changes (page title and footer) it started with this strange error. Not sure maybe had an error in html... Well, reverted index.html back to when it was working, the error was gone.

Categories