Visual Studio not copying dependencies | Could not load file or assembly - c#

I'm writing a class library to abstract our company's Database functions, but when the code gets to instantiating one of my database objects we get a:
FileNotFound Exception:
Could not load file or assembly 'MySql.Data,
Version=8.0.13.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or
one of its dependencies. The system cannot find the file specified.
The MySql.Data Dependencies as stated on the website are:
.NETStandard 2.0 Google.Protobuf (>= 3.5.1)
System.Configuration.ConfigurationManager (>= 4.4.1)
System.Security.Permissions (>= 4.4.1)
System.Text.Encoding.CodePages (>= 4.4.0)
But all of them are installed automatically.
The NuGet package is MySql.Data (8.0.13) (which installs successfully)
Project is a .NET Standard 2.0 class library
There are no compile errors or even warnings; just the above error at run-time.
Have looked through Could not load file or assembly or one of its dependencies which advises checking where the dependencies are not being found - so I did - but it doesn't say how to fix the missing reference when you've found where it is?
Using Process Monitor I was able to find the failed CreateFile operation DLL calls, referencing ...\TestingGUI\bin\Debug\MySql.Data\MySql.Data.dll which, manually checking, is not there.
The project that runs is a WinForms app that references another .NET Standard class library (essentially a middleman) which then references the database library which depends on MySql.Data.
Doing a search in the whole solution directory, there are no MySql.Data.dll files, especially after a full solution build.
Here is my csproj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RootNamespace>App1.Database</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.13" />
</ItemGroup>
</Project>
So, who's a .NET Wizard?
UPDATE:
So it turns out the code works fine when run from a .Net Core console app, but only has the error when referenced from a .NET app (specifically winforms). So I've given up having a GUI for now and am just using a .NET Core console app. I thought .NET Standard was compatible with everything, but maybe not? Anyway, I will keep my question here for anyone else having troubles.
UPDATE 2:
Thanks to #Itay Podhajcer's answer we managed to get it working with .NET Winforms by also including the NuGet package there.

I remember encountering the same issue, I think it was related to the new nuget referencing model.
Try adding the missing nuget package directly to the WinForms project.Far from ideal solution, but it should work.
Hope it helps!

Visual Studio not copying dependencies | Could not load file or assembly
It seems the dependencies and assembly are not copied to the output folder. You can check the thread for some more details:
PackageReference is missing copy files to output feature
To resolve this issue, you can try to following workaround:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Hope this helps.

Related

Multi target net frameworks conflicts

I have a Nuget package project that targets framework netcoreapp2.2. I'm trying to use it for an application that contains other projects targeting frameworks netstandard2.0;netcoreapp3.1.
This is what I currently have in the .csproj for that
<PropertyGroup>
<TargetFrameworks>netcoreapp2.2;netstandard2.0;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework) '=='netcoreapp2.2'">
( ........ )
In the classes I get the conflicts I'm using the conditional directive
##if true
1. But I'm getting compile errors because some of the extensions I'm using here are not available in one of the frameworks above.
*[Solved] I get this error:
Error CS0006 Metadata file 'C:\Users\user\source\repos\workspace\project\bin\Debug\netstandard2.0\project.dll' could not be found project.Test (netcoreapp2.2)
Is there any way I can target multiple frameworks to be able to download the NuGet package in the other projects?
Any ideas for a better approach?
Thanks
Update:
Error #2 The file project.dll didn't exist. It wasn't autogenerated by rebuilding the project. Adding the file manually solved it.

How to fix NuGet package Razor runtime compilation Error?

After reading through several answers (.NET Core 3.0 - Preview 2 - Razor views don't automatically recompile on change) that recomend installing Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to get back runtime compilation, I tried the suggested solutions but the project gets the following error message simply by installing the package without even using AddRazorRuntimeCompilation():
The project "project name" must provide a value for configuration
double clicking in the error leads to the following path:
C:\Users....nuget\packages\microsoft.aspnetcore.razor.design\2.2.0\build\netstandard2.0\Microsoft.AspNetCore.Razor.Design.CodeGeneration.targets
But there is no indication as to what is wrong with this file
Add <RazorCompileOnBuild>false</RazorCompileOnBuild> to your .csproj file. That should allow you to build the project.
You also might need <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish> for publishing to a server.
See example below:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
...
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
...
</PropertyGroup>
Found this property from this answer on a related question.

Warning NETSDK1071 A PackageReference to 'Microsoft.AspNetCore.App' specified a Version of `2.1.6`

I have the following warning
Severity Code Description Project File Line Suppression State
Warning NETSDK1071 A PackageReference to 'Microsoft.AspNetCore.App' specified a Version of `2.1.6`. Specifying the version of this package is not recommended. For more information, see https://aka.ms/sdkimplicitrefs MyApi C:\Program Files\dotnet\sdk\2.2.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets 153
I tried removing the reference by editing the project file and then adding the nuget package, however when I did this a lot of references no longer worked correctly.
I note the error is mentioning sdk\2.2 which I did install recently on my computer but there is no reference to it in the project file.
I am using VS2017 15.9.5
There's a few ways around this.
If you include the PackageReference but remove the Version attribute, it should make the warning go away. This is because it is a metapackage, which (simply put) is a type of package that gets the version based on your framework version, more here: https://learn.microsoft.com/en-us/dotnet/core/packages#metapackages
To disable the warnings, add AllowExplicitVersion:
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.11" >
<AllowExplicitVersion>true</AllowExplicitVersion>
</PackageReference>
More here: https://github.com/dotnet/sdk/issues/2602
I ran into a similar situation creating a new xUnit Test Project (.NET Core). When I added a reference to an existing ASP.NET Core Web App project, I got:
Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of:
Microsoft.AspNetCore.Authorization
Microsoft.AspNetCore.Cors
Microsoft.AspNetCore.Diagnostics.Abstractions
Microsoft.AspNetCore.Mvc
Microsoft.AspNetCore.Mvc.Abstractions
Microsoft.AspNetCore.Mvc.Core
Microsoft.AspNetCore.Mvc.Formatters.Json
Microsoft.AspNetCore.Mvc.RazorPages
Microsoft.AspNetCore.Mvc.ViewFeatures
Microsoft.AspNetCore.Razor.Runtime
Microsoft.AspNetCore.Routing
I did not understand how there could be conflicts when I did not find any references to Microsoft.AspNetCore.App NuGet package in my xUnit project.
I eliminated these version conflicts by adding the Microsoft.AspNetCore.App to my xUnit Test project.
At this point, I started getting the explicit version reference warning (NETSDK1071).
NuGet Package Manager and Package Manager Console within Visual Studio
will both add the version attribute to Microsoft.AspNetCore.App when
installing the package. You may remove the version attribute by
editing your .csproj file. This should eliminate the NETSDK1071
warning.
Note that if you do remove the version attribute, then NuGet Package Manager will disable the [Uninstall] + [Update] buttons and state: "- implicitly referenced by an SDK...".
At this point, I am not getting any warnings.
There is a lot of chatter and some tldr; documentation related to this issue. FWIW, here are a couple of succinct resources that I think warrant highlighting:
Microsoft.AspNetCore.App metapackage for ASP.NET Core 2.1 or later
#nguerrera summarized the the situation very well:
It is for all tests, or even all non-web projects that have a reference to a web project. It is not really a workaround in that the web reference did not flow transitively in .NET Core 2.x. You need to add it, and you should add it without the version so that the SDK can pick the correct version to avoid conflicts.
Better news: starting with .NET Core 3.0, the web reference will flow transitively and you can reference a web project from a test project without any other steps. So I am closing this. The design of 2.x cannot be changed, but we specifically designed things in 3.0 to have transitive Framework References, and this was a motivating scenario for that.
And here are a couple of tldr; conversations:
Does not cover Microsoft.AspNet.Core.All #8691
Version conflicts in test project depending on a Microsoft.AspNetCore.App project #2253
I had a similar issue with the error code MB2322. Fixed this by removing the version from the tag and adding the version to my Packages.props file in the src folder for the project.
So to translate here in terms of your project, the PackageReference portion you already have would simply have the version removed:
<PackageReference Include="Microsoft.AspNetCore.App" />
And in your Packages.props file, you'd add:
<PackageReference Update="Microsoft.AspNetCore.App" Version="[whichever version you are using here]"/>
For Developers working on Nopcommerce.
I was having the issue with Nopcommerce Project v(2.2).
To resolve, you have to make an edit inside Nop.Core.csproj file.
change :
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.5" />
with :
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.5">
<AllowExplicitVersion>true</AllowExplicitVersion>
</PackageReference>

Type not found Microsoft.EntityFrameworkCore in .NET Standard project

I am having a total nightmare getting Entity Framework Core working in my .NET standard project! I am using .NET Standard 2
I can install the package fine (Microsoft.EntityFrameworkCore.SqlServer)
However, when I build my application although it compiles, I get the error below
Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.
Has anyone ever had this? I wasnt expecting this to be such a pain just to use Entity Framework in .NET Standard lol
I cant see any dlls related to this in my output folders
My application is hosted within service fabric, but I am not sure thats anything to do with it
Paul
I am assuming there is a bug either with Nuget or Visual Studio
I ended up having to manually copy all the dlls!!
Try to add this into your .csproj file which contains reference to the
Microsoft.EntityFrameworkCore.SqlServer
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

The type [Type] exists in both [Assembly1] and [netstandard 2.0 assembly]

I cannot get Microsoft.EntityFrameworkCore 2.0 to work with NETStandard.Library 2.0
Error CS0433 The type 'AttributeUsageAttribute' exists in both 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Contrived.Model.Schema D:\Contrived\Contrived\Framework\Contrived.Model.Schema\Attributes\EntityColumnAttribute.cs
My .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="NETStandard.Library" Version="2.0.0" />
</ItemGroup>
</Project>
This also happens if i create a new solution and a netstandard 2.0 library then add entityframeworkcore 2.0
Error CS1703 Multiple assemblies with equivalent identity have been imported: 'D:\Users\maksymiuk_a.nuget\packages\netstandard.library\2.0.0\build\netstandard2.0\ref\System.Threading.Tasks.dll' and 'D:\Users\maksymiuk_a.nuget\packages\system.threading.tasks\4.3.0\ref\netstandard1.3\System.Threading.Tasks.dll'. Remove one of the duplicate references. ClassLibrary1 D:\Contrived\Contrived\ClassLibrary1\ClassLibrary1\CSC
Update: Visual Studio 2017 15.3 and the 2.0.0 SDK have been released. Use these versions to get rid of such conflicts.
This appears to be one of the build issues happening when using a mix of preview-tooling and "RTM" packages before all parts have been released.
To get the best support for 2.0.0 at the time of writing, do one of the following:
only use the same preview2 version of the nuget packages, the installed .NET Core SDK and the latest VS 2017 15.3 preview (15.3 Preview 7).
Use the latest VS 2017 15.3 Preview (Preview 7), install the 2.0.0 (not preview2) .NET Core Sdk from the CI build output at https://dotnetcli.blob.core.windows.net/dotnet/Sdk/2.0.0/dotnet-sdk-2.0.0-win-x64.exe (the same build output location is used by microsoft's 2.0.0 docker images)
The reason for this requirement is that the 2.0.0 SDK knows about these conflicts and trims the conflicting references (System.*.dll in this case) out of the projects.
This issue can still happen even with DOT NET Core 2+, and is caused when the packages you reference in your project depend on a different version of the same package, specifically same namespace.
There are 2.5 solutions that I know of:
1) If you control both packages, then update both to make sure they are using the same version of that dependency (or change the namespace of one of the versions of that dependency, like: Your.Thing.v3.Models)
2) If you can find one or both of those packages on an open source site like GitHub, you can download them and update them to use the same version of that dependency (or the namespace thing from option 1, above)
2.5) If you don't control or have access to the source of those packages, and I realize that this isn't really a solution... (hence the .5), then the last option is to stop using one of those packages.
If there's another way, I'd love to know about it.
Probably not the best idea out there, but try to remove the reference to System.Runtime manually by right-click on references and finding it there.
For me it worked to remove the reference to both assemblies and re-add them again.
Expand your project's References in the Solution Explorer --> Right click the faulty assembly -> Remove.
Then right click References -> Add Reference... -> check the boxes of the assemblies you just removed.
Then rebuild.
In my case I have added multiple reference in web.config file.
after removing one of them resolve the issue and working perfectly under local IIS server also

Categories