ConfigurationManager in a .NET Core application - c#

I get the following error upon application debug/launch:
System.TypeInitializationException: 'The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.'
FileNotFoundException: Could not load file or assembly
'System.Configuration.ConfigurationManager, Version=4.0.2.0,
Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot
find the file specified.
With the following piece of code that uses DatabaeContext:
private CamelotViewsStandardContext db = new CamelotViewsStandardContext();
The project is on the .NET Core 3.0 with System.Configuration.ConfigurationManager NuGet package installed. It works on the previous version which is on the .NET Core 2.2 so no idea why it is not working on 3.0?
The code behind the reference for creating the database context is using Entity Framework, which is supposed to be supported in 3.0?

This happened to me when I upgraded a project from .NET Core 2.2 to 3.1. Unit tests would pass but the exception would be thrown when the webapp was deployed. I resolved it by adding
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageReference Include="System.Management" Version="4.7.0" />
to the .csproj file for the project.
Reference: https://github.com/dotnet/runtime/issues/627

install this package in both project
Install-Package System.Configuration.ConfigurationManager -Version 4.7.0

Related

All tests are inconclusive with the same "An assembly specified in the application dependencies manifest was not found" error when migrating to .NET 6

I'm migrating from .NET 5 and all tests are working, and finishing succesfully, until I update all .csproj files to .NET 6.0 like this:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
Afterwards all integration and unit tests show as inconclusive in Rider with the following error:
Test not run
Last runner error: Test runner agent exited unexpectedly Process /usr/share/dotnet/dotnet:138437 exited with code '140': Error: An assembly specified in the application dependencies manifest (Logging.UnitTests.deps.json) was not found: package: 'Autofac', version: '6.4.0' path: 'lib/net6.0/Autofac.dll'
--- EXCEPTION #1/3 [UnknownExitCodeException]
Message = “
Process /usr/share/dotnet/dotnet:138437 exited with code '140':
Error:
An assembly specified in the application dependencies manifest (Logging.UnitTests.deps.json) was not found:
package: 'Autofac', version: '6.4.0'
path: 'lib/net6.0/Autofac.dll'
”
I have tried updating all nuget packages, including autofac, to the latest version, tried all possible combinations with Autofac, but the error still remains. I'm on Arch linux and have all the correct dotnet sdks and runtimes.
dotnet --list-sdks
5.0.407 [/usr/share/dotnet/sdk]
6.0.102 [/usr/share/dotnet/sdk]
6.0.301 [/usr/share/dotnet/sdk]
dotnet --list-runtimes
Microsoft.AspNetCore.App 5.0.16 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.16 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.6 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
The application runs normally in both Run and Debug mode, it's just that all tests have stopped working. This also happens in the GitHub pipeline, any ideas?
Thank you very much in advance!
So after many hours finding a fix I tried the following:
Create a new .NET 6 test project
See if a small test without dependencies works, and it did work.
Look at the NuGet package references that were automatically added to that test project
Look at the NuGet package refences in the tests projects that were failing
Realize some were only implicitly referenced, remember how this has #$%^ me in the ^&%$ in the past
Directly reference "Microsoft.NET.Test.Sdk" and "xunit" in every test project like this:
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
Witness how all tests are running correctly again.
TL;DR, add the following to every test project you have:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>

MySQL .NET connector throwing "System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Security.Permissions" [duplicate]

I am using Visual Studio Code, and I'm trying to connect to my MySql server (5.5). I have downloaded the latest (6.10.6) MySql connector and installed it. I had to research to add references in VS Code, but I managed it. Now my .csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="Lib\MySql.Data.dll">
<HintPath>MySql.Data</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
</ItemGroup>
</Project>
I have copied the content of C:\Program Files (x86)\MySQL\MySQL Connector Net 6.10.6\Assemblies\v4.5.2 to myProject\Lib. At compile time there are no errors, all green. But when I try to connect using this code
using System.Data;
using System.Security.Permissions;
using MySql.Data;
using MySql.Data.MySqlClient;
public static bool Connect(){
string str = "server=192.168.0.160;user=usr;database=DB;port=3306;password=pass";
MySqlConnection connection = new MySqlConnection(str);
connection.Open();
System.Console.WriteLine(connection.State);
return true;
}
it fails, and produces this error:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in MySql.Data.dll: 'Could not load file or assembly 'System.Security.Permissions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system can't find the file.'
I do not know why, as the using directive is not signaling an error.
The assembly System.Security.Permissions is currently not available for .NET core applications so my guess is you are using an older version of MySQL Database Provider that is not compatible with .NET core 2.
According the official documentation .NET core 2.0 is only supported from version 6.10.
Try installing the latest version from: https://dev.mysql.com/downloads/connector/net/6.10.html
Edit
If you already have that version and it is still not working, might be that you are missing some references. Why don't you try using the official NuGet instead of referencing the dll in the GAC, here is the command:
Install-Package MySql.Data -Version 6.10.6
If you are using VS Code, you can use the NuGet package manager extension to manage the packages directly from the editor: https://marketplace.visualstudio.com/items?itemName=jmrog.vscode-nuget-package-manager
Edit 2
Seems it might be a bug as I found this question .NET Core 2 with MySql.Data results in permission error and the accepted answer recommends updating to version 8.
So try to update to version 8.0.10-rc and let the problem be gone, here is the NuGet command:
Install-Package MySql.Data -Version 8.0.10-rc
Can't just upgrade the MySQL connector because there may be compatibility issues between newer connectors and legacy databases.

Upgrading from 4.5.2 to 4.7.2 breaks System.Reactive usage

I'm upgrading a .Net framework 4.5.2 console application to 4.7.2 and getting an issue related to System.Reactive
After setting the version to 4.7.2, i run
Update-Package -Reinstall -ProjectName My.Project
When recompiling, I get the following error:
Severity Code Description Project File Line Suppression State
Error CS1069 The type name 'ISubject<>' could not be found in the
namespace 'System.Reactive.Subjects'. This type has been forwarded to
assembly 'System.Reactive, Version=4.4.0.0, Culture=neutral,
PublicKeyToken=94bc3704cddfc263' Consider adding a reference to that
assembly.
Referencing this line of code:
private readonly ISubject<bool> _lostConnection = new Subject<bool>();
I already have a reference to System.Reactive.Interfaces, but it is v3.1.1. The lowest I can select is 3.0.0.
Does anyone know why the upgrade broke this?

VS2017 .NetCore 2.0 API - Could not load file or assembly Microsoft.AspNetCore.Hosting.Abstractions

I've just created a new API using ASP.NET Core 2.0.
I haven't coded anything yet. I just want to test the initial API template.
When I try to start my project I receive the following error:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module.
Could not load file or assembly 'Microsoft.AspNetCore.Hosting.Abstractions, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
I do not receive any errors when building the project.
The assemby can be found at the following directory: 'C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.abstractions\2.0.2'.
Is that correct? Where should the 'Microsoft.AspNetCore.Hosting.Abstractions' assembly be located and why can't VS find it?
I had the same error attempting to debug a simple Azure Function App (this is using .NET core) created from the Function App template in Visual Studio 2017. Attempting to run in debugger gave me the same error. It turned out to be I had updated the Microsoft.NET.Sdk.Functions package from version 1.0.6 to 1.0.12. That was my problem. Reverted the update back to 1.0.6 and it worked fine. Hope this helps!
Please re-install the nuget package or upgrade to the latest at: https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Abstractions/

IO.FileNotFoundException in MySql.Data.dll: Can't load System.Security.Permissions

I am using Visual Studio Code, and I'm trying to connect to my MySql server (5.5). I have downloaded the latest (6.10.6) MySql connector and installed it. I had to research to add references in VS Code, but I managed it. Now my .csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="Lib\MySql.Data.dll">
<HintPath>MySql.Data</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
</ItemGroup>
</Project>
I have copied the content of C:\Program Files (x86)\MySQL\MySQL Connector Net 6.10.6\Assemblies\v4.5.2 to myProject\Lib. At compile time there are no errors, all green. But when I try to connect using this code
using System.Data;
using System.Security.Permissions;
using MySql.Data;
using MySql.Data.MySqlClient;
public static bool Connect(){
string str = "server=192.168.0.160;user=usr;database=DB;port=3306;password=pass";
MySqlConnection connection = new MySqlConnection(str);
connection.Open();
System.Console.WriteLine(connection.State);
return true;
}
it fails, and produces this error:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in MySql.Data.dll: 'Could not load file or assembly 'System.Security.Permissions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system can't find the file.'
I do not know why, as the using directive is not signaling an error.
The assembly System.Security.Permissions is currently not available for .NET core applications so my guess is you are using an older version of MySQL Database Provider that is not compatible with .NET core 2.
According the official documentation .NET core 2.0 is only supported from version 6.10.
Try installing the latest version from: https://dev.mysql.com/downloads/connector/net/6.10.html
Edit
If you already have that version and it is still not working, might be that you are missing some references. Why don't you try using the official NuGet instead of referencing the dll in the GAC, here is the command:
Install-Package MySql.Data -Version 6.10.6
If you are using VS Code, you can use the NuGet package manager extension to manage the packages directly from the editor: https://marketplace.visualstudio.com/items?itemName=jmrog.vscode-nuget-package-manager
Edit 2
Seems it might be a bug as I found this question .NET Core 2 with MySql.Data results in permission error and the accepted answer recommends updating to version 8.
So try to update to version 8.0.10-rc and let the problem be gone, here is the NuGet command:
Install-Package MySql.Data -Version 8.0.10-rc
Can't just upgrade the MySQL connector because there may be compatibility issues between newer connectors and legacy databases.

Categories

Resources