I followed several tutorials to add Newtonsoft.Json in my project by doing the following :
dotnet add package Newtonsoft.Json
Here is the terminal log :
info: Added PackageReference for package 'Newtonsoft.Json' in project 'C:\Users\XXX\Documents\Visual Studio Code\[...]'
info: Restoring packages for 'C:\Users\XXX\Documents\Visual Studio Code\[...]'
info: CACHE https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json
info: The 'Newtonsoft.Json' package is compatible with all frameworks specified in the 'C:\Users\XXX\Documents\Visual Studio Code\[...].csproj' project.
info: PackageReference for package 'Newtonsoft.Json' version '13.0.1' added to file 'C:\Users\XXX\Documents\Visual Studio Code\[...].csproj'.
info: Validation of the restoration...
info: Writing component file to disk. Path: C:\Users\XXX\Documents\Visual Studio Code\[...]\obj\project.assets.json
log: Restore completed in 277.34 ms for C:\Users\XXX\Documents\Visual Studio Code\[...].csproj.
After executing the command, here is the content of my project .csproj file :
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>
Then I add:
using Newtonsoft.Json;
The problem is that Visual Studio Code doesn't offer me Newtonsoft.Json when I start writing it with using.
Is there something I need to do on my part?
Related
tl/dr;
My project-referenced Roslyn analyzer works fine in Visual Studio 2019 but fails to load in dotnet build with the following build warning:
CSC : warning CS8032: An instance of analyzer Nearmap.CodeAnalysers.UseCtorInjection.UseCtorInjectionAnalyser cannot be created from [solution path]\MyCodeAnalysers\bin\Debug\netstandard2.0\MyCodeAnalysers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
Details
I have a Roslyn analyzer in a project which is loaded into the other projects in the solution.
The analyzer works as expected, and produces the expected warnings, during build in Visual Studio 2019.
When I try to build the solution from the command line (as our CI pipeline does) using dotnet build MySolution.sln I get the following build warning for every project that references the analyzer project:
CSC : warning CS8032: An instance of analyzer Nearmap.CodeAnalysers.UseCtorInjection.UseCtorInjectionAnalyser cannot be created from [solution path]\MyCodeAnalysers\bin\Debug\netstandard2.0\MyCodeAnalysers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
where [solution path] is the full path to the folder containing the solution.
The analyzer project looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<!-- added in an attempt to fix the problem -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
</ItemGroup>
</Project>
Projects reference this analyzer as:
<ItemGroup>
<ProjectReference Include="..\MyCodeAnalysers\MyCodeAnalysers.csproj"
PrivateAssets="all"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
I've checked the build output folder of the analyzer project and it contains all the DLL dependencies needed to load the analyzer, but it's obviously not being looked at when try to load the analyzer.
What am I missing?
The problem was the SDK version.
Aparently the Microsoft.CodeAnalysis.dll (and others) are loaded from the .Net SDK in use. We're currently still using the .Net 6.0 SDK to build our solutions. .Net 6.0 includes Microsoft.CodeAnalysis.dll 4.1.0 not the 4.4.0 version I was using. VS2019 runs the build differently, and didn't have the DLL resolution problem.
I fixed it by rolling the package references back to v4.1.0:
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
</ItemGroup>
I have a Visual Studio solution which has several projects. One of them, which is called Extensions is supposed to build a Nuget package. Problem is that if I use MSBuild to build the solution (by executing msbuild from the command prompt), I get the following error for the Nuget package build task:
"C:\git\repo\dirs.proj" (default target) (1:7) ->
"C:\git\repo\sources\dirs.proj" (default target) (2:5) ->
"C:\git\repo\sources\dev\Sdk\CompanyName.ProductName.Extensions.csproj" (default target) (7:6) ->
(GenerateNuspec target) ->
C:\Program Files\dotnet\sdk\5.0.408\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(221,5): error : Could not find a part of the path 'C:\git\ess\target\distrib\Debug\Amd64\CompanyName.ProductName.Extensions'. [C:\git\ess\sources\dev\Sdk\CompanyName.ProductName.Extensions.csproj]
Here is the CSPROJ file I have for this project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>CompanyName.ProductName.Extensions</AssemblyName>
<RootNamespace>CompanyName.ProductName.Extensions</RootNamespace>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeCoreAssets</TargetsForTfmSpecificBuildOutput>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<NuspecFile>CompanyName.ProductName.Extensions.nuspec</NuspecFile>
<PackageOutputPath>$(DistribRoot)\$(Configuration)\$(Platform)\$(MSBuildProjectName)</PackageOutputPath>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SkipAssemblyComVisible>true</SkipAssemblyComVisible>
<IncludeBuildOutput>false</IncludeBuildOutput>
<OutputPath>$(DistribRoot)\$(Configuration)\$(Platform)\$(MSBuildProjectName)</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<NuspecBasePath>$(OutputPath)</NuspecBasePath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core\CompanyName.ProductName.Core.csproj" PrivateAssets="All" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<Target DependsOnTargets="ResolveReferences" Name="IncludeCoreAssets">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>
</Project>
I personally think the issue is that for some reason when MSbuild is trying to create the Nuget package, it's trying to find the Extensions project DLLs in this path: \target\distrib\Debug\Amd64\CompanyName.ProductName.Extensions\, even though it actually built and stored the said binaries in this path earlier: \target\distrib\CompanyName.ProductName.Extensions\. <--- This is something I checked manually myself by examining the 'target' folder after running msbuild.
Visual Studio however doesn't have this issue. When I build this solution within Visual Studio, it stores the binaries for this Extensions project in \target\distrib\Debug\AnyCPU\CompanyName.ProductName.Extensions\ folder, which I think matches the pattern defined in the CSPROJ file:
<OutputPath>$(DistribRoot)\$(Configuration)\$(Platform)\$(MSBuildProjectName)</OutputPath>
Visual Studio also stored the Nuget package in this folder, which is also correct as per the <PackageOutputPath> spec mentioned in the CSPROJ file.
So can someone suggest why MSbuild is storing the built DLLs of this project in \target\distrib\CompanyName.ProductName.Extensions\, but is trying to find them in \target\distrib\Debug\Amd64\CompanyName.ProductName.Extensions\ ?
I think the issue is that for some reason, MSbuild isn't adhering to the <OutputPath> spec mentioned above when it's storing the built DLLs for this project.
I am trying to open a solution using Visual studio 2019 and visual studio 2017. All the projects in the solution are loading except for one. When trying to load the unloaded project i get an error in the output window as
TakstMVC.csproj : error : The imported project
"....build\MSBuild.Community.Tasks.targets" was not found. Confirm
that the expression in the Import declaration
"TakstMVC\.....build\MSBuild.Community.Tasks.targets" is correct,
and that the file exists on disk. TakstMVC\FluentMigrator.targets
When i tried to open using VS 2017 i saw a migration report which said
TakstMVC.csproj: The application which this project type is based on
was not found. Please try this link for further information:
http://go.microsoft.com/fwlink/?LinkID=299083&projecttype=E3E379DF-F4C6-4180-9B81-6769533ABE47
Part of the .csproj of the project is as below:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A4354F5C-ECF5-4621-AA9E-B91FE543F096}</ProjectGuid>
<ProjectTypeGuids>{E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TakstMVC</RootNamespace>
<AssemblyName>TakstMVC</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>4.0</OldToolsVersion>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<MvcProjectUpgradeChecked>true</MvcProjectUpgradeChecked>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
I have the following installed in my computer
Microsoft .NET Framework 4.7.1 SDK
Microsoft .NET Framework 4.7.1 SDK Targeting Pack (ENU)
Microsoft .NET Core 3.1.1 - Windowsserver hosting
Microsoft .NET Core SDK 3.1.101 (x64)
Microsoft .NET Core SDK 2.2.207 (x64)
Microsoft .NET Core Runtime - 3.1.1
Microsoft .NET Core Runtime - 2.2.8
I tried to install dotnetfx35.exe but it doesnt even run when executed (not even a message or error).
The windows feature are as below:
How can I identify the target framework of the project and load it in visual studio successfully ? appreciate some advise on this.
The error is clear that you did not import the MSBuild.Community.Tasks.targets correctly on your local area. The reason is that you did not install MSBuild.Community.Tasks.targets on your PC or the import path from csproj file is incorrect.
You should check this document to install the right target.
First, remove xml node under csproj file like these:
<Import Project="..\build\MSBuild.Community.Tasks.targets" Condition="Exists('..\build\MSBuild.Community.Tasks.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\build\MSBuild.Community.Tasks.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\build\MSBuild.Community.Tasks.targets'))" />
</Target>
Second, install the msi file.
then, add these under the csproj file:
<Import Project="C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
Besides, you could also use nuget function. First, uninstall the nuget package if you have a old version on the project. Then, use the first suggestion to remove any previous import projects="xxx\MSBuildTasks.targets". After that, install the MSBuildTasks nuget package. That is the same.
Update
Try to You should change ..\..\ to ..\. Use the right path.
Or use this command under Tools-->Nuget Package Manager-->Package Manager Console
update-package -reinstall
Answer from the Customer
embarrassingly enough the whole issue was with the solution path where my solution was located in my local computer. I had a long path including a folder having two words as well. After trying everything else I moved the solution folder to C:\temp folder and the issue was no longer there. The error showed in VS was quite misleading. What a waist of time. Thank you for your efforts !
I'm not sure how to set the default nuget feed for my .net core project in Visual Studio Code to https://api.nuget.org/v3/index.json
When I attempt to add a package (and subsequently, restore dependencies), I get the following errors...
C:\Program Files\dotnet\sdk\2.1.403\NuGet.targets(114,5): error : Unable to load the service index for source https://smartassessor.pkgs.visualstudio.com/_packaging/SANuget/nuget/v3/index.json. [c:\Users\Matthew.OConnor\Desktop\Important Documents\Programming\DatingApp\DatingApp.API\DatingApp.API.csproj]
C:\Program Files\dotnet\sdk\2.1.403\NuGet.targets(114,5): error : Response status code does not indicate success: 401 (Unauthorized). [c:\Users\Matthew.OConnor\Desktop\Important Documents\Programming\DatingApp\DatingApp.API\DatingApp.API.csproj]
This source https://smartassessor.pkgs.visualstudio.com/_packaging/SANuget/nuget/v3/index.json has nothing to do with my current project, however it is used for other projects that are typically run using full blown Visual Studio. Those projects are saved in a completely different place to this project.
I simply want to be able to add nuget packages from nuget.org in my .net core project. How do I do this in VS code?
I don't currently have a nuget.config file in this project.
The package source mentioned in the error appears to be coming from a package source I have setup whilst using Visual Studio
This is my csproj file...
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.4"/>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="4.0.1"/>
<PackageReference Include="CloudinaryDotNet" Version="1.3.1"/>
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.1.0-preview1-final"/>
</ItemGroup>
</Project>
CloudinaryDotNet is the package that generated the errors above.
I think VS Code is just running a dotnet restore, and the reason you're seeing this source being used is because it's configured in your User/Computer nuget configuration file (located on windows, which you seem to be running, at %appdata%\NuGet\NuGet.Config & %ProgramFiles(x86)%\NuGet\Config respectively). The VS configuration editor you showed is just a nice GUI for this configuration file.
If you want to keep this general setting, you should be able to use a nuget.config file in your VS Code project (which you mentioned you don't have at the moment). There is more info on this here -Add custom package source to Visual Studio Code.
Also, if you're trying to restore manually, you can use one of these 2 flags -
dotnet restore --source https://api.nuget.org/v3/index.json
dotnet restore --ignore-failed-sources
These are pretty self explanatory, but you can see the full documentation here - https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-restore?tabs=netcore2x
Hope this helps (:
When I use Visual Studio 2017 RC to create netcore project and Nuget Microsoft.EntityFrameworkCore.Tools.DotNet, but I get an error.
Package 'Microsoft.EntityFrameworkCore.Tools.DotNet
1.0.0-msbuild2-final' has a package type 'DotnetCliTool' that is not supported by project 'src\WebApplication1'.
How do I resolve it?
Open your .csproj file and add the lines:
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools"
Version="1.0.0-msbuild2-final" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet"
Version="1.0.0-msbuild2-final" />
Edit your project file and add these
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet">
<Version>1.0.0-msbuild3-final</Version>
</DotNetCliToolReference>
</ItemGroup>
And than open visual studio's developer PowerShell editor and run these command
dotnet add package Microsoft.EntityFrameworkCore.Tools.DotNet