Visual Studio assembly location seems to be overridden by a certain package - c#

I'm working with an AWS Lambda project
My dev environment:
IDE: VS Professional 2022 version 17.4.0
Target framework: .NET 6
Package management: paket
This project requires packages Microsoft.Extensions.Hosting and Serilog.AspNetCore
However, I found after adding Serilog.AspNetCore into paket.references, the location of Microsoft.Extensions.Hosting.dll seems to be overridden by Serilog.AspNetCore, causing method not found exception when creating IHost using Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder().
Method not found: 'Microsoft.Extensions.Hosting.IHostBuilder Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()'.
Decompiled CreateDefaultBuilder() with JetBrains decompiler, it shows very different assembly locations are loaded between with and without Serilog.AspNetCore:
// With Serilog.AspNetCore installed
Assembly location: C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\6.0.11\ref\net6.0\Microsoft.Extensions.Hosting.dll
// Without Serilog.AspNetCore
Assembly location: C:\Users\{user}\.nuget\packages\microsoft.extensions.hosting\6.0.0\lib\net6.0\Microsoft.Extensions.Hosting.dll
My colleague cannot reproduce this with our develop branch. I have tried clean build, manually delete folder obj and bin then do dotnet restore,dotnet paket restore, none of them works. I can reproduce this with a new created solution here
Any suggestion for me to try before I do a clean reinstall of VS and git clean -xfd?

Related

Why can't I compile any of my old or new project? [duplicate]

I just upgraded my visual studio 2019 to latest version 16.8.3 and suddenly I am not able to load any C# project and getting the following error for all .NET core projects:
The project file cannot be opened. Unable to locate the .NET SDK.
Check that it is installed and that the version specified in
global.json (if any) matches the installed version.
Try to edit envrionment Variables.
Right click on This PC -> Properties -> Advanced System Settings -> Advanced -> Environment Variables -> System Variables
Add [install path] C:\Program Files\dotnet\ to the variable path.
Restart visual studio.
If it does not solve this problem, you can refer to this answer.
I found that the path C:\Program Files\dotnet was already in my path but it was ordered below the C:\Program Files (x86)\dotnet entry. By simply moving C:\Program Files\dotnet above C:\Program Files (x86)\dotnet in the list and restarting Visual Studio cleared up the problem.
If there is a global.json file in your solution folder(where *.sln file reside), delete this.
In my case, I had .NET 6 preview but then uninstalled it. After uninstalling, some of my projects won't load. I have all the configurations mentioned above but still won't load the projects. I went to C:\Program Files\dotnet\sdk\ where I found that there still exist a .NET 6 folder which is empty. Deleting that empty .NET 6 folder fixes the issue and I was able to load my projects.
This works for me...
Check first if which .net Core SDK is available in the system.
Step 1: Open Command prompt (CMD) and type dotnet --info
The above image shows no SDK is installed or register
Step 2: Install SDK from https://dotnet.microsoft.com/download/dotnet-core
Done Visual studio works like charm... Verify if its properly installed and register
Above image you can see .net core SDK is registered properly on the system.
I had a similar situation upgrading from VS 2019 to VS 2022, trying to run a dotnet 5.0 solution from VS 2022 (that starts to use dotnet 6.0).
In the PATH env. variable I had both 'C:\Program Files\dotnet' and 'C:\Program Files (x86)\dotnet'. I removed the (x86) one and stopped having that error
Installing missed SDK that the project has built solved the problem
I had a similar issue when I tried to build a net5.0 solution that is not installed on my machine.
In my case, I had VS 2022, and net6.0 installed.
Installing net5.0 SDK solved the problem.
I had only C:\Program Files\dotnet in Path env
I just carefully followed the error message and tried to resolve the problem.
I got below error message:
F:\mypath\ServerlessFunc\ServerlessFunc\ServerlessFunc.csproj : error
: The project file cannot be opened. C:\Program
Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk\Sdk not found. Check
that a recent enough .NET SDK is installed and/or increase the version
specified in global.json.
I then tried to navigate through
C:\Program Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk\Sdk
path which was mentioned in the above error and found that the above path does not exist. This problem could be resolved if I install SDK of version of 6.0. Rather I started using VS-2022.

Microsoft Visual Studio 2019: The project file cannot be opened. Unable to locate the .NET SDK

I just upgraded my visual studio 2019 to latest version 16.8.3 and suddenly I am not able to load any C# project and getting the following error for all .NET core projects:
The project file cannot be opened. Unable to locate the .NET SDK.
Check that it is installed and that the version specified in
global.json (if any) matches the installed version.
Try to edit envrionment Variables.
Right click on This PC -> Properties -> Advanced System Settings -> Advanced -> Environment Variables -> System Variables
Add [install path] C:\Program Files\dotnet\ to the variable path.
Restart visual studio.
If it does not solve this problem, you can refer to this answer.
I found that the path C:\Program Files\dotnet was already in my path but it was ordered below the C:\Program Files (x86)\dotnet entry. By simply moving C:\Program Files\dotnet above C:\Program Files (x86)\dotnet in the list and restarting Visual Studio cleared up the problem.
If there is a global.json file in your solution folder(where *.sln file reside), delete this.
In my case, I had .NET 6 preview but then uninstalled it. After uninstalling, some of my projects won't load. I have all the configurations mentioned above but still won't load the projects. I went to C:\Program Files\dotnet\sdk\ where I found that there still exist a .NET 6 folder which is empty. Deleting that empty .NET 6 folder fixes the issue and I was able to load my projects.
This works for me...
Check first if which .net Core SDK is available in the system.
Step 1: Open Command prompt (CMD) and type dotnet --info
The above image shows no SDK is installed or register
Step 2: Install SDK from https://dotnet.microsoft.com/download/dotnet-core
Done Visual studio works like charm... Verify if its properly installed and register
Above image you can see .net core SDK is registered properly on the system.
I had a similar situation upgrading from VS 2019 to VS 2022, trying to run a dotnet 5.0 solution from VS 2022 (that starts to use dotnet 6.0).
In the PATH env. variable I had both 'C:\Program Files\dotnet' and 'C:\Program Files (x86)\dotnet'. I removed the (x86) one and stopped having that error
Installing missed SDK that the project has built solved the problem
I had a similar issue when I tried to build a net5.0 solution that is not installed on my machine.
In my case, I had VS 2022, and net6.0 installed.
Installing net5.0 SDK solved the problem.
I had only C:\Program Files\dotnet in Path env
I just carefully followed the error message and tried to resolve the problem.
I got below error message:
F:\mypath\ServerlessFunc\ServerlessFunc\ServerlessFunc.csproj : error
: The project file cannot be opened. C:\Program
Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk\Sdk not found. Check
that a recent enough .NET SDK is installed and/or increase the version
specified in global.json.
I then tried to navigate through
C:\Program Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk\Sdk
path which was mentioned in the above error and found that the above path does not exist. This problem could be resolved if I install SDK of version of 6.0. Rather I started using VS-2022.

MSBuild fails on Unit Test Project (.NET Core) after NuGet Restore

ISSUE
The Visual Studio Build task in a Visual Studio Team Services build for a solution that contains a default ASP.NET MVC website and a default Unit Test Project is failing to find references. This task runs after a NuGet Restore that should install these references, but even though the NuGet Restore task succeeds, the references are not installed on the server running this build.
Visual Studio Team Services Logs
The error logs have been uploaded to This Github Repository
Attempted Fixes
In an effort to ensure that the packages are properly referenced by the project, all of the NuGet packages were reinstalled from Package Manager using the following command:
PM> Update-Package --reinstall
This only caused MSBuild to not find Microsoft.Net.Compilers.props in both the projects. So in an attempt to correct this, the version of Microsoft.Net.Compilers.props in the project was dropped to version 2.3.2. However, this only brought back the original errors where the MSTest.TestAdapter.props and Microsoft.Net.Compilers.props references could not be found.
Try it with these tasks (works fine for me with the sample solution you shared):
NuGet Tool Installer (Version of NuGet.exe to install: 4.3.0)
NuGet restore (Choose 2.* version)
Visual Studio Build (Visual Studio Version: Latest)
Queue build with the agent that has VS2017 installed.

Nuget looking in "\.nuget\packages" for local project

I'm working on an app with several projects in the solution. Project-1 builds fine and Project-2 references it within the same solution. When I open the references to Project-2, I see the Project-1 reference. I can go to the path specified and I see the Project-1 dll there.
When I try to build Project-2, I get the error
The package Project-1 could not be found in C:\Users\[it me]\.nuget\packages.
Run a NuGet package restore to download the package.
Both projects are set to the same architecture (x64), not "Any CPU". Why is Project-2 looking for Project-1 in the nuget packages folder instead of the path specified?
The problem, as it turned out, was a versioning problem. Project-1 had a lower target version than Project-2 (which was targeted for the Windows 10 Anniversary Build 14393). I installed Visual Studio 2015 Update 3, which includes the 14393 APIs, and that resolved the problem.

Visual Studio 2017 Enterprise Add Unit Test project throws Package Installation Error

I have a solution with projects pointing to .Net Framework 4.5 which I have opened in Visual Studio 2017 and tried to Unit Test Project pointing to .Net Framework 4.5. I am getting the following error
Package Installation error .
Could not add all required packages to the project. The following packages failed to install from 'C:\Program Files(x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\502SXCIF.E5I\Packages':
MSTest.TestFramework.1.1.11: Failed to add reference to 'Microsoft.VisualStudio.TestPlatform.TestFramework'
I have tried changing the Platform to x86 as suggested on some online resources.
I am using a Windows 7 Enterprise 64 bit. Has anyone been able to resolve this.
MsUnit test related template got missed in your local system.if you have another version please try to copy respective folder it will resolve the issue.

Categories

Resources