I tried creating a Windows Forms .NET core 3.1 application via the template and switching the output type to Console Application.
Here's my code:
static class Program
{
static void Main()
{
System.Console.WriteLine(0 switch { 0 => "Hello World" });
}
}
When I compile I get:
error CS8370: Feature 'recursive patterns' is not available in C# 7.3. Please use language version 8.0 or greater.
I'm targeting .NET Core 3.1. I thought that would get me C# 8.0 language features by default. Apparently I am mistaken.
What do I do?
EDIT: I'm using Visual Studio 2019 16.3.9
This is the part that confuses me the most because it says that the Language version is "Automatically selected based on framework version" (and I can't change it.) Also I don't see an adequate explanation of why I can't change language versions at Why can't I select a different C# version? That page says that if I'm using .NET Core 3.x that I should be using C# 8.0.
The .csproj file is as follows:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon />
<StartupObject>Program</StartupObject>
</PropertyGroup>
</Project>
Adding this line fixes the problem:
<LangVersion>8.0</LangVersion>
But is that really the only way to create an application? I have to manually edit my .csproj? Why can I not change the Language version and why is it not automatically selecting C# 8.0 based on me using .NET Core 3.1?
Open your csproj and see if you have a line like
<LangVersion>7.3</LangVersion>
If yes try removing it, if that doesn't work try to change it to 8.0
From https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults
You should remove the
<LangVersion>latest</LangVersion> from your project file when you
update the .NET SDK.
I had the same problem (c#8 unavailable on Core 3.1 project) and I fixed it that way :
Uninstall previous version of Visual studio (2015 and 2017 in my
case)
Repair Visual studio 2019 (V16.4.1 in my case) with "visual studio installer" launched with administrator right.
No need of LangVersion in csproj.
In Visual Studio 2019 if I try to create an ASP.NET Core web application project, I get to select the framework version in the following screen -
But when I try to create a .NET Core class library project I am prompted with the following screen which does not provide any option for selecting the framework version -
Clicking the Create button always creates the project right away taking the latest .NET Core version installed on my machine.
So, how can I select the framework version while creating the class library project? Or do I have to change it manually every time after creation ?
The ASP.NET project creation dialog providing a framework selection seems to be an exception in .NET Core / Standard projects to me. At least since VS2019 with the new "New Project" dialog, you have the following options after creating the project with this dialog.
"Normally" (to my experience), you right-click the project file in the Solution Explorer, choose "Edit Project File" and modify the <TargetFramework> element by naming one of the valid target framework monikers. See MSDN about them.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
You can also rename the element to TargetFrameworks (note the pluralized name) to build against multiple frameworks at the same time, which are ; separated:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net451;netstandard2.0;netcoreapp3.0</TargetFrameworks>
</PropertyGroup>
</Project>
Alternatively, you can also choose "Properties" from the project right-click menu and select a framework via a slightly dated UI not supporting all of the new csproj features, like said multi targeting:
If you need many new projects building against a specific framework, create a template csproj and just copy and rename it.
Also, if you want to build against preview versions of .NET Core in non-preview versions of VS, ensure you allow usage of them in Tools > Options > Environment > Preview Features.
I want to simplify my configuration in my ASP.NET Core Web Application (.NET Framework) application using VS 2017.
I already know that my website will be running under Windows/IIS in x64 environment and .NET 4.6.2. There is no chance in the foreseen and unforeseen future for this application to use any other environment from the dev to production.
So, I only need Debug x64 and Release x64 modes only. (AnyCPU and x86 are not needed!), so I went ahead and removed all other configuration from the project.
Now, upon compilation, I am getting the following error:
'C:\Projects\MyProject\My.Website\obj\project.assets.json' doesn't
have a target for '.NETFramework,Version=v4.6.2/win7-x64'.
Ensure you
have restored this project for TargetFramework='net462' and
RuntimeIdentifier='win7-x64'. MD.Website C:\Program Files
(x86)\Microsoft Visual
Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.targets
I am developing on Windows 7, I am not sure how to fix this one. Any idea?
For some reason <TargetFramework> in my .csproj file was singular. I added an "s" and it became "TargetFrameworks", which worked:
<PropertyGroup>
<TargetFrameworks>net462</TargetFrameworks>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
</PropertyGroup>
I didn't change my TargetFramework, I ran in the Package Manager Console the command:
dotnet restore
And it worked! (I'm using VS2017 and I'm doing a .net core application pointing to .net framework)
I had manually changed mine from x86 to x64. In this case, just restoring the packages from Visual Studio would not work but closing Visual Studio, deleting the project.assets.json, re-starting Visual Studio and re-building the project worked for me. I left <TargetFramework> singular.
Command line nuget restore ... may also have worked.
I had this issue when trying to publish a dotnetcore console application to a local folder after upgrading it to version 2.0.
After trying all the deleting folders and dotnet restore and making sure all settings in Application and Build were 2_0 to no avail. I realised my publish profile was still targeting 1.1, even though 2.0 was showing as selected when I went into edit the profile, it showed 1.1 in the publish summary. So I re-selected 2.0 in the dropdown and it updated the summary to show 2.0 and it all worked fine.
For some reason <RuntimeIdentifier> in my .csproj file was missing.
Adding it resolved this issue for me:
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Platforms>AnyCPU;x64</Platforms>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
I have a .net core application pointing to .net framework 4.6.1 in VS2017 which I tried to publish. I changed my platform target from x86 to x64 and started to get this error when I tried to publish again but I didn't have any problems to build. I just opened publish profile settings and everything looked normal (target runtime was win7-x64) but that was enough for my publish to start working properly.
Check the actual publishing profile file of the Publish you are trying to run. In our case, we have a set of projects that have to be shared between Core and regular asp.net, so we target Core 1.1 and set the runtime version to 4.6.1. After running through all of the projects and updating them to target version 4.7, I started getting this error on publish (the actual builds worked fine, as did localhost debugging, it was the publish that was jacked).
Checking the actual "widgets - Web Deploy.pubxml" file, I found this at the bottom:
<_DestinationType>AzureWebSite</_DestinationType>
<TargetFramework>net461</TargetFramework>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
</PropertyGroup>
</Project>
As others on this thread have mentioned, simply opening up the property sheet of the publishing profile will show you, in my case, that the framework was targeted to 4.7 (which was accurate per the projects involved but did not reflect the actual value in the file)... I did still need to click the Save button to get the underlying .pubxml file to actually be updated with the correct value. You can probably edit that file manually too, if you feel so inclined.
This one drove me nuts. :)
As Boris pointed out, in my case the problem was in the PublishProfiles.
First, I added the following in my .csproj file (As BluE mentioned in the post above):
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Platforms>AnyCPU;x64</Platforms>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
and then I opened my PublishProfile
and then click on the Edit link:
and finally in the opening dialog, set the TargetFramework to your project's Framework and the TargetRungime to win-x64 and then click Save!
Thats it!
Now if you try publishing your project using this profile, it should be working perfectly.
Hope it helps someone.
For me moving the PlatformTarget section to the common PropertyGroup fixed the same problem on Azure DevOps build:
Does not work:
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
...
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
...
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
Works:
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
...
<Platforms>x64</Platforms>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
...
</PropertyGroup>
I'm trying to build a class library that multi-targets both .NET 4.5.1 and .NET Standard 1.3. According to the documentation, I should be able to do this:
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
</PropertyGroup>
However, when I try to build, I get these odd errors:
Cannot infer TargetFrameworkIdentifier and/or TargetFrameworkVersion from TargetFramework='net451'. They must be specified explicitly.
MSB3645 .NET Framework v3.5 Service Pack 1 was not found. In order to target ".NETFramework,Version=v1.3", .NET Framework v3.5 Service Pack 1 or later must be installed.
MSB3644 The reference assemblies for framework ".NETFramework,Version=v1.3" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
If I specify the target framework identifiers manually, it builds fine:
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net451'">
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
</PropertyGroup>
I'm using Visual Studio 2017 Community. Am I doing something wrong here?
Have you definitely written
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
and not
<TargetFramework>net451;netstandard1.3</TargetFramework>
?
I was getting the same error until I added the missing s
I have a Visual Studio 2008 project that is a Smart Device project targeting a .NET 3.5 Compact Framework Windows CE device and it currently references a 3.5 class library project.
What I would like to do is be able to add the class library into a Visual Studio 2012/2013 solution without "migrating" it.
For example the structure is currently:
VS2008 Solution
Smart Device Project
Class Library Project
I want to have is:
A VS2008 Solution
Smart Device Project
Class Library Project
A VS2013 Solution
Class Library Project
ASP.NET MVC Project
The result of this would be that I can work on the Smart device project in VS2008 and add classes to the Class Library Project, and then be able to open the VS2013 solution file and have those new classes available.
When I look at the difference between a 2008 Class Project and a 2013 Class Project csproj files, the only thing that really is different is the ToolsVersion on the Project element (http://msdn.microsoft.com/en-us/library/bcxfsh87.aspx).
In VS2008 the ToolsVersion is 3.5, but in VS2013 its 12.0.
I've attempted to make the ClassLibraryProject switch between 3.5 and 12.0 for the ToolsVersion with the following:
<Project ToolsVersion="$(ProjectToolsVersion)">
<Choose>
<When Condition="'$(VisualStudioVersion)' != ''">
<PropertyGroup>
<ProjectToolsVersion>12.0</ProjectToolsVersion>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<ProjectToolsVersion>3.5</ProjectToolsVersion>
</PropertyGroup>
</Otherwise>
</Choose>
<!-- other project stuff -->
</Project>
I can open this fine in VS2013, but when I open it in VS2008 it says:
Unable to read the project file 'ClassLibraryProject.csproj'.
C:\Test\ClassLibraryPorject.csproj: The tools version "$(ProjectToolsVersion)" is unrecognized.
Please Note: I am not able to migrate the entire project to VS2013 because VS2013 does not support Smart Device projects.
VS2008 which uses MSBuild 3.5 should be able to handle ToolsVersion 12, as in that case it automatically falls back to ToolsVersion 3.5.
Thus, you should migrate the class library project in VS2013, and it should be able to work still in the VS2008 solution without any modification.
VS2008 and MSBuild 3.5 gives you the error, because Choose is evaluated later than Project, so what you define in Choose (aka ProjectToolsVersion) cannot be used in Project.