Nuget package installed but cannot be imported - c#

I am trying to add a nuget package "Azure.ResourceManager" to an existing project.
Here is the .csproj file:-
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.6.0" />
<PackageReference Include="Azure.ResourceManager" Version="1.0.0" />
<PackageReference Include="Azure.ResourceManager.Resources" Version="1.0.0" />
....
</ItemGroup>
</Project>
I ran the nuget restore command on the solution, and I have checked that the package is installed through the Visual Studio Package Manager Console, using command
PM> Find-Package ResourceManager
Id Versions Descriptio
n
-- -------- ----------
Azure.ResourceManager {1.0.0} Azure m...
Azure.ResourceManager.Communication {1.0.0} Azure m...
Azure.ResourceManager.Deployment... {1.0.111} This pr...
Azure.ResourceManager.Deployment... {1.0.111} This pa...
However, when I try to import this package into a class, the type cannot be resolved.
I wondered if the package and project targeted different versions, so I checked.
Target frameworks supported by package
Target framework supported by project
In summary,
As far as I can tell, both project and package support .Net Core 3.1 as a target framework.
Package is installed into project, but cannot be imported into class
Another thing I noticed is that the type "ResourceManager" is being searched for in the "Microsoft.Azure" namespace instead of "Azure"
Looking for any pointers to resolve the issue.

I learnt that the issue was happening because of naming conflicts within the namespace that I was trying to add the import in. There seems to have been another namespace "Azure." within the one I was adding code to. Adding a glocal prefix helped resolve the issue.
using global::Azure.ResourceManager;

This seems like a bug. I just created a solution with a .NET core 3.1 project inside. I used the Nuget Manager ui and it worked fine. Have you tried installing it that way?

Related

ASP.NET 6 Scaffolding dbcontext MySQL Error: Unable to resolve service for type [duplicate]

I am having difficulties to scaffold an existing MySQL database using EF core.
I have added the required dependencies as mentioned in the oracle doc:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkcore.Tools" Version="6.0.0">
and then, I ran this code in the package manager console:
Scaffold-Dbcontext "server=the.server.ip.address;user=user_name;database=db_name;password=db_password;port=3306" MySql.EntityFrameworkCore -o Data -v
It shows this error:
Unable to resolve service for type
'Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger`1[Microsoft.EntityFrameworkCore.DbLoggerCategory+Scaffolding]'
while attempting to activate
'MySql.EntityFrameworkCore.Scaffolding.Internal.MySQLDatabaseModelFactory'
Here are the relevant logs in the output window:
Finding design-time services referenced by assembly 'Test2'...
Finding design-time services referenced by assembly 'Test2'...
No referenced design-time services were found.
Finding design-time services for provider 'MySql.EntityFrameworkCore'...
Using design-time services from provider 'MySql.EntityFrameworkCore'.
Finding IDesignTimeServices implementations in assembly 'Test2'...
No design-time services were found.
I don't know how shall I implement the design time classes and nor did I find any useful links in the web.
Note that I can access and run query on the database using MySQL Workbench.
I got this error not while scaffolding but when trying to create my first migration. I didn't want to downgrade to 5.0 because it would've had to be permanent since I was going to run a lot of migrations.
I fixed it by changing my provider from MySql.Data.EntityFrameworkCore to Pomelo.EntityFrameworkCore.MySql
Remove the old provider from both my API and DAL projects:
dotnet remove package MySql.EntityFrameworkCore
Add Pomelo provider
dotnet add package Pomelo.EntityFrameworkCore.MySql
Update your startup to use Pomelos configuartion:
https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#2-services-configuration
Migrations are working:
dotnet ef migrations add InitialCreate
According this description
https://bugs.mysql.com/bug.php?id=106592
you can solve this error by adding below class to your code
public class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
serviceCollection.AddEntityFrameworkMySQL();
new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
.TryAddCoreServices();
}
}
and after define you have to add it as service in startup file
like this
services.AddSingleton<IDesignTimeServices, MysqlEntityFrameworkDesignTimeServices>();
After these steps you can migrate your code :)
I came across the same issue trying to scaffold an existing MySQL database. It looks like the latest version of MySql.EntityFrameworkCore (6.0.0-preview3.1) still uses the EFCore 5.0 libraries and has not been updated to EFCore 6.0.
It also seems Microsoft.EntityFrameworkCore.Diagnostics was last implemented in EFCore 5 and removed in 6.
When I downgraded all the packages to the 5 version level, I was able to run the scaffold command without that error.
To make Scaffold-Dbcontext work, I had to downgrade both the packages to 5.0.0 version.
MySql.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Tools
After successful scaffolding, I upgraded these packages back to the their latest versions.
I could find the Core 6.0 documentation for microsoft.entityframeworkcore.diagnostics in the official website, but it was still not working when I tried the Scaffold-DbContext command. Had to downgrade all the packages to the last 5.0 version before it worked. Here are the packagerefs in my project settings
"Microsoft.EntityFrameworkCore.Design" Version="5.0.13"
"Microsoft.EntityFrameworkCore.Tools" Version="5.0.13"
"MySql.Data" Version="8.0.28"
"MySql.EntityFrameworkCore" Version="5.0.10"
#https://stackoverflow.com/users/1322226/quinestor I faced same issue and as #https://stackoverflow.com/users/9914700/james-ruth mentioned I downgraded the versions of all EFCore and EFCore Design to 5.0.8 in Visual Studio.
Did not look around for command line commands :) But we can also do it from from dotnet cli, which I guess you probably would be aware of. We can remove - dotnet remove package <PACKAGE_NAME>, and install specific version - dotnet add package <PACKAGE_NAME> --version
You do not need to perminately downgrade your EF version to scaffold, you can edit the project file to use
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MySql.EntityFrameworkCore" Version="5.0.8" />
</ItemGroup>
Then you can run
dotnet ef dbcontext scaffold "[DSN String]" MySql.EntityFrameworkCore -o [DBName]
Then change your project file back (or revert changes) to use the latest version of EF.
Dont forget to edit the "protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)" and remove the DSN it adds to the code.
The latest version of MySql.EntityFrameworkCore still uses the EFCore 5.0 libraries and has not been updated to EFCore 6.0.
It also appears that Microsoft.EntityFrameworkCore.Diagnostics was removed in 6.
I resolve this error by adding the class below to my code:
public class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
serviceCollection.AddEntityFrameworkMySQL();
new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
.TryAddCoreServices();
}
}
Then you must add it as a service in the initialization file
services.AddSingleton<IDesignTimeServices, MysqlEntityFrameworkDesignTimeServices>();
Hope this helps!

C# Visual Studio 2019, Meta Numeric, Error: Could not load file or assembly

Visual Studio 2019
C#
Project 1:
Dependencies: Meta.Numerics 4.1.4. I added Meta Numerics via the Manage NuGet Packages for Solution
Project 1 is a Class Library
Will be compiled and .dll will be shared with an associate who will use it in the main application
Project 1 builds just fine using Debug.
using System;
using Meta.Numerics;
namespace LeakDetection
{
public class LeakDetectionOperations
{
public LeakDetectionOperations(int co = 24)
{ }
public int leakCheck()
{
double result = ComplexMath.Abs(10);
return 0;
}
}
}
Project 2
Dependencies: Project 1. Imported via Add references, browser, and selected the .dll from project 1
Project 2 is just a simple test project that I'm using to test the .dll object.
It runs, but throws an exception when it attempts to call the ABS function of Meta.Numerics.
using System;
using LeakDetection;
namespace LeakTest
{
class Program
{
static void Main(string[] args)
{
LeakDetectionOperations obj = new LeakDetectionOperations();
int ret;
ret = obj.leakCheck();
Console.WriteLine("Hello World!");
}
}
}
I've followed the instructions from the Meta.Numeric gitrepo regarding installation. The installation was done as they suggested to install the package. I've also cleaned build, and rebuilt fresh. I also changed from debug to release to see if there was anything related to the debug that was causing the error. As you can see at the above code, its fairly minimum, as this is not my actual code. Its a bit more elaborate, but rather than posting the full code, this is the minimum usable code that replicates the issue I'm having. Nothing from the Meta.numeric library is usable.
I usually work in Python, have some experience in C and C++, but I have used make files to compile in linux. Using C#, visual studio is fresh for me.
Any suggestions as to where I should look would be much appreciated.
UPDATE:
Per the suggestion by #kit, I've included the .csproj file for project 1 below
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Leak_Detection</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Meta.Numerics" Version="4.1.4" />
</ItemGroup>
</Project>
.csproj for project 2
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="Leak Detection">
<HintPath>..\..\Leak Detection\Leak Detection\bin\Debug\netcoreapp3.1\Leak Detection.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
I was able to kind of replicate your issue (not exactly but I got the same kind of error). I believe your test project is in .NET framework. If I do not reference any nuget package in test project, failure occurs. But, if I install any nuget package and later on uninstall it, program runs successfully. For testing, I installed and uninstalled Newtonsoft.Json package. So, there is a bug in initialization of nuget package engine for .NET framework where in absence of any nuget packages, it is not able to resolve transitive nuget dependencies. If I create test project in .NET core, test project runs without any issue.
I have also put code in github
https://github.com/dheerajjain11/MissingDLLIssue/
MetaNumerics is .NET Standard library
MetaNumericsTest is .NET Framework Test project where I installed and uninstalled nuget package. Now, it runs successfully
MetaNumerics2 is .NET Framework Test project which fails
MetaCore is dotnet core project which runs without any issue and no workarounds
I don't know if this is normal or not, but what resolved the issue for me was installing the package via the Nuget package manager on both the Library and the Console application. Both builds need a reference.
What I was doing previously, was only installing the package for the library build. Seeing how the console application was then compiling with the .dll being referenced and called, the calling application also needed the package installed.

"The project 'Web' must provide a value for Configuration" error after migrating to .NET Core 3

I've migrated an ASP.NET Core 2.2 project to Core 3.0 and am getting the error:
The project [Project location] must provide a value for Configuration.
There's not really a lot to go on with that error message, does anyone know how to resolve this error?
This looks like it could be similar to this issue on the dotnet cli github repo.
The issue turned out to be that I was still referencing Microsoft.AspNetCore.Razor.Design Version="2.2.0" in the .proj file's package references. Deleting that reference (which isn't needed at all as Razor.Design is now part of AspNetCore library) fixed the issue.
Once I'd done that, I then got hundreds of errors about nullable objects being a new feature not compatible with razor. That was because I had <LangVersion>Latest</LangVersion> in my .proj file. Removing that line fixed that issue and got the project running again.
(In some cases you might need to clean and rebuild and/or restart VS too, according to comments on the github thread)
If that doesn't solve it, it's possible that one the NuGet packages used by your project is the cause. Try removing the dependencies to see if that clears the issue, and then re-add them one at a time to work out which NuGet package is the cause.
There is an MS document title "Migrate from ASP.NET Core 2.2 to 3.0". Under "Update the project file", it states,
"A large number of NuGet packages aren't produced for ASP.NET Core
3.0. Such package references should be removed from your project file"
The two given as an example are:
Microsoft.AspNetCore.App
Microsoft.AspNetCore.Razor.Design
Below that, you can expand to see all the packages that are no longer being produced:
Microsoft.AspNetCore
Microsoft.AspNetCore.All
Microsoft.AspNetCore.App
Microsoft.AspNetCore.Antiforgery
Microsoft.AspNetCore.Authentication
Microsoft.AspNetCore.Authentication.Abstractions
Microsoft.AspNetCore.Authentication.Cookies
Microsoft.AspNetCore.Authentication.Core
Microsoft.AspNetCore.Authentication.OAuth
Microsoft.AspNetCore.Authorization.Policy
Microsoft.AspNetCore.CookiePolicy
Microsoft.AspNetCore.Cors
Microsoft.AspNetCore.Diagnostics
Microsoft.AspNetCore.Diagnostics.HealthChecks
Microsoft.AspNetCore.HostFiltering
Microsoft.AspNetCore.Hosting
Microsoft.AspNetCore.Hosting.Abstractions
Microsoft.AspNetCore.Hosting.Server.Abstractions
Microsoft.AspNetCore.Http
Microsoft.AspNetCore.Http.Abstractions
Microsoft.AspNetCore.Http.Connections
Microsoft.AspNetCore.Http.Extensions
Microsoft.AspNetCore.HttpOverrides
Microsoft.AspNetCore.HttpsPolicy
Microsoft.AspNetCore.Identity
Microsoft.AspNetCore.Localization
Microsoft.AspNetCore.Localization.Routing
Microsoft.AspNetCore.Mvc
Microsoft.AspNetCore.Mvc.Abstractions
Microsoft.AspNetCore.Mvc.Analyzers
Microsoft.AspNetCore.Mvc.ApiExplorer
Microsoft.AspNetCore.Mvc.Api.Analyzers
Microsoft.AspNetCore.Mvc.Core
Microsoft.AspNetCore.Mvc.Cors
Microsoft.AspNetCore.Mvc.DataAnnotations
Microsoft.AspNetCore.Mvc.Formatters.Json
Microsoft.AspNetCore.Mvc.Formatters.Xml
Microsoft.AspNetCore.Mvc.Localization
Microsoft.AspNetCore.Mvc.Razor
Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
Microsoft.AspNetCore.Mvc.RazorPages
Microsoft.AspNetCore.Mvc.TagHelpers
Microsoft.AspNetCore.Mvc.ViewFeatures
Microsoft.AspNetCore.Razor
Microsoft.AspNetCore.Razor.Runtime
Microsoft.AspNetCore.Razor.Design
Microsoft.AspNetCore.ResponseCaching
Microsoft.AspNetCore.ResponseCaching.Abstractions
Microsoft.AspNetCore.ResponseCompression
Microsoft.AspNetCore.Rewrite
Microsoft.AspNetCore.Routing
Microsoft.AspNetCore.Routing.Abstractions
Microsoft.AspNetCore.Server.HttpSys
Microsoft.AspNetCore.Server.IIS
Microsoft.AspNetCore.Server.IISIntegration
Microsoft.AspNetCore.Server.Kestrel
Microsoft.AspNetCore.Server.Kestrel.Core
Microsoft.AspNetCore.Server.Kestrel.Https
Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions
Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
Microsoft.AspNetCore.Session
Microsoft.AspNetCore.SignalR
Microsoft.AspNetCore.SignalR.Core
Microsoft.AspNetCore.StaticFiles
Microsoft.AspNetCore.WebSockets
Microsoft.AspNetCore.WebUtilities
Microsoft.Net.Http.Headers
I've had the same issue and it was solved by removing the following references:
"Microsoft.AspNetCore.Mvc" Version="2.2.0"
"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.2.0"
This is kind of weird.
For me, the problem was because of 'Microsoft.AspNetCore.Mvc' package.
I uninstalled it and installed 'Microsoft.AspNetCore.Mvc.Core'.
I need to add that, I had installed 'Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation' too.

Custom .NET Standard Nuget package not installing correctly

I have a .NET Standard class library project with a number of POCO's. This project is built using TeamCity and published as a Nuget package using the built-in Nuget server.
The problem I'm having is when it's installed into my solution with a number of .NET Framework class library projects and ASP.NET MVC and Web API projects (set to .NET Framework 4.7.1), it seems to be stuck on an older version and is not recognising any new classes or methods I add to the project - e.g. NewMethod1()
Project File for Nuget package
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461;net462;net47</TargetFrameworks>
<Version>1.0.0</Version>
</PropertyGroup>
<PropertyGroup>
<NetStandardImplicitPackageVersion>2.0.0</NetStandardImplicitPackageVersion>
<Description>Standard entities used within our systems</Description>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<Authors>Company X</Authors>
<Company>Company X</Company>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>bin\Release</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>bin\Debug</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
</Project>
TeamCity is using the 'dotnet' restore, build & pack options.
The package is stored in my Nuget cache located in C:\Users\antho.nuget\packages. When I use Object Browser to inspect the dll, it contains the new classes and methods (e.g. NewMethod1()).
When I install this package into my .NET Framework solution, no errors occur during the installation. If I try and use the new method - NewMethod1() - the code doesn't compile.
If I create a brand new solution and ASP.NET MVC project and install the package, the new method can be used in code and it compiles successfully.
What could be causing the new version not to be installed correctly? It's tricky to provide a sample reproducing the issue because it seems to work in a new project.
Update
If I add a new project to the solution and install the Nuget package, it gets the latest version.
Project A
<PackageReference Include="AutoGuru.Shared.Utilities" Version="1.0.369" />
public class Class1
{
public void Test()
{
"dfdfdfdf".SanitizeVehicleRego();
}
}
Project B
<PackageReference Include="AutoGuru.Shared.Utilities">
<Version>1.0.369</Version>
</PackageReference>
public class Class1
{
public void Test()
{
"fdfdf".SanitizeVehicleRego();
}
}
Project B compiles successfully and Project A doesn't. SanitizeVehicleRego() is a string extension method in the AutoGuru.Shared.Utilities package.
My answer it quite big, so I add an answer instead of comment.
Step 1
Check the output when you restore the package, sometimes dotnet restore resolve an other version automatically ( you should have a warning for this kind of things in your console ).
Step 2
If any information was found in Step 1. Try to clean all your local nuget cache.
dotnet nuget locals all --clear
I'm not sure if local nuget packages are under cache stategy. But if they are, it should resolve the correct version of your package.

Sautinsoft PdfFocus exception after migration to .NET Core 2.1

I have a purchased license of the DLLs (6.9.4.10) and my *.csproj contains this:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.0"/>
...
<PackageReference Include="System.IO.Packaging" Version="4.5.0"/>
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.0"/>
<PackageReference Include="ZKWeb.System.Drawing" Version="4.0.0"/>
</ItemGroup>
...
<ItemGroup>
<Reference Include="SautinSoft.PdfFocus">
<HintPath>..\PDF Focus .Net Full (6.9.4.10)\DLLs\Net Core 2.0\SautinSoft.PdfFocus.dll</HintPath>
</Reference>
</ItemGroup>
I am trying to convert a PDF into a DOCX-file (which worked in .NET 4.5).
This is the relevant part of the code:
....
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
f.Serial = Settings.GetAppSetting("PdfFocusSerial", "**MySerial**");
f.OpenPdf(buffer);
if (f.PageCount > 0)
{
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Docx;
var result = f.ToWord(); //f.Exception set after this
...
}
...
I've checked that the same buffer is sent in as in the old code, but the output differs by some bytes. And I get an Exception set in f.Exception, which is:
{System.Collections.Generic.KeyNotFoundException:
The given key '0' was not present in the dictionary. ...
When I try to open the newly created *.docx-file, Word says it's damaged. After clicking through some dialogs it can still open the file.
Anyone have any ideas?
Is this a known bug for this library in .Net Core 2.1? (Only 2.0 is mentioned on their website)
I've also tried the free version published on NuGet with the same results.
EDIT
This was indeed a bug in the .NET Core specific version. They have fixed this in version 6.9.6.29.
My Name is Dmitry and I work in SautinSoft.
Thank you for your issue. You are right. We have some problems with PDF Focus.Net and Net Core 2.1
Our developers try to fix this issue. We have found where is a bug (resources/fonts) and I hope, that we will prepare a new version very quickly.
I'll inform you.
If you want to use "RTF to HTML" for Net Core 2.X-6.X please add these references in your project:
System.Drawing.Common, 4.7.0 or up.
System.IO.Packaging, 4.4.0 or up.
System.Text.Encoding.CodePages, 4.5.0 or up.
System.Xml.XPath.XmlDocument, 4.3.0 or up.
But If it will be Net Core 7.X for LinuxOS - it doesn't work, because the System.Drawing.Common NuGet package is now attributed as a Windows-specific library. The platform analyzer emits warning at compile time when compiling for non-Windows operating systems.

Categories