Build C# in VSCode not working - c#

I'm trying to build my C# (WPF) project, which I originally generated in Visual Studio 2017 in VSCode. If I open a command prompt and build the project with C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe .\[Name]\[Name].csproj /t:Rebuild /p:Configuration=Debug
the build succeeds and the program runs as expected, but with the following message (not a warning nor an error):
Project file contains ToolsVersion="15.0". This toolset may be unknown or missing, in which case you may be able to resolve this by installing the appropriate version of MSBuild, or the build may have been forced to a particular ToolsVersion for policy reasons. Treating the project as if it had ToolsVersion="4.0".
But when I try to run and build in VSCode (Start with Debugging (F5)), the build fails. Also, VSCode shows every XAML-Element I referenced in code as undefined:
(The name '[Button/Label/TextBox/etc.]' does not exist in the current context [Name]
What do I need to define in the tasks.json and launch.json to make VSCode build my project?

The C# support in VS Code is optimized for cross-platform .NET Core development. WPF is neither cross-platform nor .NET Core and VS Code does not support debugging WPF applications. Due to this focus, many of the standard C# project types are not recognized by VS Code: https://code.visualstudio.com/docs/languages/csharp.
If you want the best possible experience when creating WPF applications, you should use Visual Studio.

I don't think that VSCode supports WPF since it focusses on projects built with .NET Core and Mono. WPF is not supported in .NET Core

Related

Manually Setting up a .NET Framework Project using VS Code

I am working on a project that requires me to build a .dll file in .NET framework 4.x
I know this would be super easy if I were to use Visial Studio. But I have set my mind on using VS Code as my IDE.
In theory Visual Studio "only" automates the creation of all sorts of metadata and references. So I figured it should be possible to do these things manually. Correct?
Can anybody direct me to where I can find how to do that?
I am working on Windows 10.
Because Code isn't adept at managing a project file (.csproj) - nor should anyone have to - you can actually use the dotnet.exe CLI to create the project and target .NET Framework instead. The only requirement is that .NET Core SDK needs to be installed, even if you use MSBuild because the new SDK-style project requires different targets.
Run: dotnet new classlib -o MyLibrary
Run: code MyLibrary
Open MyLibrary.csproj in Code
Change the line <TargetFramework>v4.5</TargetFramework>
Now you can run dotnet build or msbuild build, and even set up build and test tasks in Code. By default, Ctrl+Alt+B will run a build task, or prompt to create one from a template if none exist yet.
Now you can simply add files without modifying the .csproj file. I participate in many OSS projects that use technique and can easily switch between VS, Code, or even non-IDEs like vim.
It would require from you to type a lot of code and files that are otherwise automatically scaffold (generated) when you create a new project in Visual Studio. Even though VS Code is supportive for .NET framework coding (with the C# plugin), I would advice you to use Visual Studio (the community version would suffice). It has much better support for any .NET framework development.

The target process exited without raising CoreCLR started event error with .NET Core 2.2

I want to debug an empty WebApi Project based on .NET Core 2.2.
I installed the "Core 2.2 SDK x86" and changed the target framework to 2.2:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
When I started to debug this project, IIS starts, but in the route api/values I see nothing (it loads forever) and I get this error:
The target process exited without raising a CoreCLR started event.Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core
In my solution WPF and Class Library projects exist. I wanted to make a WebApi for it. Like I said, it's an empty base project generated by Visual Studio 2019. I just installed Core 2.2. Why do I get that error and what am I doing wrong?
I had the same issue.
I ran Program.cs file from the command line and the error message was different.
So, apparently, I didn't have the appropriate .NET Core runtime installed.
You can download it by the following link - https://dotnet.microsoft.com/download
I was facing the same issue!
I updated my Visual Studios to the most recent via the VS Installer.
While updating, I also added the following VS Workloads:
ASP.NET and web development
.NET desktop development
See about workloads here:
https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/docs/install/modify-visual-studio.md
This worked for me! 😊
I did not have to downgrade or remove anything
I had to do a "clean" and "rebuild" of the project and after that it worked again.
I ran with the same problem. But for me it was that my project was running in .NET Core 2.2, which I didn't have installed. I'm using VS 2019.
So for the solution, just go to Visual Studio Installer and for your VS 2019 hit "Modify" like in the following image (it's in spanish though):
Then, click "Individual Components", like
And then, make sure you checked in ".NET Core 2.2 Runtime (EOL)" (and above like .NET Core 3.0, if you want) and click "Modify", as shown
Hope this works for you.
In my case, switching to x64 save the day.
It seems like there are lots of different causes for this. For me, it was because I had upgraded a .csproj file but I hadn't updated the image used in my docker file.
I had same issue after package upgrading. You don't need any vs update.
Just go to
dotnet.microsoft.com/download/dotnet-core/2.2
and get runtime
dotnet-hosting-2.2.7-win.
I had the same error. Remove "Microsoft.AspNetCore.All" from dependencies.
I have downgraded "Microsoft.AspNetCore.App" version and error gone .
Deleting all bin folders of my solution
building everything again did the trick for me
(.NET Core 3.1 VS 2019)
I see many answers like try and error
after searching about the issue, I got the following :
it simply depends on three parameters and they have to be compatible
Windows platform "x86,x64" you can change it from the CPU option of the visual studio
or choose any CPU hence the install .net x framework should be compatible with the windows
The target framework for the project
(check the project target framework "right-click on the project and check target framework") if it is 5 so the enabled in the third parameter should be 5 or change this one but take care you should tell the other team members if you change it
The Enabled framework go for tools and get tools and go for individuals component and chose the target framework the same as found in step 2 and press modify
Faced this issue during xUnit testing. Downgrading nuget package "Microsoft.NET.Test.Sdk" to 15.9.0 worked for me
I got this error because of a Project reference pointing to a project, that did not exist anymore (was removed in merged git-commit). Solved it by right-clicking the project, then Edit Project File, delete the reference and save.
I was trying to run a project that used net core 2.2 but I didn't have it, and there was no error(I had 1.1), so after installing the 2.2 using the "Visual Studio Installer" it worked like a charm. =)
I restarted my Visual Studio instance in Admin mode and I was able to debug.
After closing Visual Studio I can now start in normal mode and debug as expected. I'm not sure what changed though.
I was missing a reference to AWSSDK.Core. After installing the specific version and re-starting VS2019 and rebuilding, the project ran fine.
All of a sudden it stopped working. For me clean solution and re-build solution worked.
I was getting a similar error with my project: The target process exited without raising a CoreCLR started event.
My startup project was targeting multiple frameworks: netstandard2.0;netcoreapp2.2
I fixed this issue by changing my target framework from netstandard2.0 to netcoreapp2.2 using the following stackoverflow directions: How to switch between target frameworks for .NET Core projects in Visual Studio
Be sure to install the exact version of .net core that you want your project to run on
I had this issue and finally tracked it down to doing a "publish" operation with a deployment mode of: "Self contained" That resulted in dropping all .NET runtime binaries in my bin directory. Where I think things got totally messed up is I have several solutions all using that same common bin directory, each build different exe's and dll's.
Only one was for a self contained deployment as a test. I think that started mixing up some of the minor dlls for the .NET core and causing this issue. Once I purged all the dll's that were "not mine" and rebuilt things started working again. I'm using Visual Studio 2019 16.8.2 / .NET Core 3.1
I realize the original question is regarding .NET Core 2.2 but this question comes up first when searching for the main issue.
.Net Core 2.2 is required to be installed. I fixed when I added this in Visual Studio Installer.
VS2019 16.7.1 ASP.Net Core 3.1
Our Nunit test projects that issued this error, only worked with a very select set of Nunit and Microsoft packages.
Microsoft.NET.Test.sdk - 16.5
Nunit - 3.12
Nunit3TestAdapter 3.16.1
But here's the catch, you have to go into
c:\users\yourname\.nuget\packages and look for all three folders shown above. If you find any other version in those folders, delete it.
I found two versions in the Microsoft.NET.Test.SDK folder. I suspect the newer version was being loaded. Why? Because when I deleted it everything ran fine.
A corrupt layout is also possible, had this happen myself. To fix, go to projpath\bin\x64\configtype\AppX, for example:
C:\Users\Foo\Documents\Visual Studio 2019\projectname\bin\x64\Debug\AppX
Delete everything here, rebuild and you're good to go.
Since I don't see it here yet, here's what worked for me. I have a UWP project, Win32 project, and Windows application packaging project in the same solution. Running the Win32 app using FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync was giving this message and not working.
What fixed it was changing a setting in the packaging project's project settings:
Debug -> Debugger type -> Background task process: Native Only
I had same issue. Now in VS2022 you cannot download NetCore2.2.
So just download and install NetCore 2.2 from microsoft dotnet website.
https://dotnet.microsoft.com/en-us/download/dotnet/2.2
This error occurred when I tried changing C# projects in my solution to use Docker and running it:
The error went away when I changed the C# projects back to use the Visual Studio's debug server and running it:
In the Test Explorer window of Visual Studio 2019, make sure that there are no references to any *.testsettings or *.runsettings files if you are not using them intentionally. I had referenced a *.testsettings file unintentionally, and that was causing the issue.
For me, I'd just installed Visual Studio 2022 on a fresh installation of Windows 11. My project uses .Net 5, which wasn't installed by default (I only had .Net 6). I simply used the Visual Studio installer, modified my VS2022, selected Individual Components, and enabled .Net 5. Viola!
I started to get this error after updating Visual Studio from 2019 to 2022. It turned out that the project target framework was set to .Net Core 3.1, which was removed when I uninstalled VS2019, and the solution was to change it to .Net 6.0 which is the component I had selected during installation (Installing .Net Core 3.1 from the Visual Studio Installer should work as well).
To do so, right click on the project name and look for the "Target framework" option (my installation is in Spanish so that's what I presume it should say).
I've encountered this issue multiple times, using VS 2017 and VS 2019 across .NET Core 2.1, 3.1 and 5.0.
There are many conditions that can cause this problem to occur:
Microsoft Update can install a version of .Net Core at any time, automatically and outside of your awareness and knowledge.
It can also change the installation of any .Net Core SDK version's installed components.
Your version of VS has to be compatible with the version of the .Net Core SDK that you have installed.
You must have all of the following components installed for this error to be resolved/pre-empted for .NET 5.0 (please check the dotnet 5.0 site for the compatibility list by versions).
Microsoft .NET SDK 5.0.408 which includes (the following below) must all be installed and listed in the control panel:
.NET Core 5.0.17 Windows Desktop Runtime,.NET Runtime, Windows Server Hosting, Shared Framework
If any .NET Core 3.1 SDK are installed without your knowledge, you must uninstall it and repair your targeted .net core version's installation.

Build server of an old ASP.NET 4 application fails building new C# 7, but it works in development

I recently start to work on a legacy ASP.NET codebase based on .NET framework 4.0. We managed to pass everything from Visual Studio 2012 to VS 2017, updated the build server with a new version of Jenkins and installing .NET framework 4.7.x.
Locally we can write C# code of the newest version (7.3) and the build works (VS doesn't use MSBuild if I remember right), but when we deploy on the build server the build fails because there MSBuild cannot recognize constructs newer than C# 4.0. To avoid mistakes I fixed the lang version to 4.0 (advanced build properties on projects), so if I write too new C# VS blocks me in dev, but we would like to start using new C#.
We also tried to fix C# 7.3 directly in the project (<LangVersion>7.3</LangVersion> in PropertyGroup inside csproj) and the but ToolsVersion property of Project element (csproj) to 14.0, but then building we MSBuild fails with the error:
CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3,
4, 5 or Default
Here it's explained that what I want to do it is possible: https://www.dotnetcurry.com/dotnet/1427/build-apps-different-dotnet-framework-versions
No matter which .NET framework version we target in the project, the
C# language version in use will not change. That’s fine because the
vast majority of language features that were introduced in later
versions of the language don’t depend on the CLR or specific APIs.
They are only syntactic sugar and the bytecode generated by the
compiler will still work in .NET framework 2.0.
Anyone have an idea of what mistake are we doing?
The problem was that on the build server MSBuild wasn't properly installed and build scripts got an old one.
Installing Visual Studio 2017 Build tools and fixing the path on the script we solved.
After we had the problem "The “GetReferenceNearestTargetFrameworkTask” task was not found" we solved like explained here: The "GetReferenceNearestTargetFrameworkTask" task was not found
(the right answer depends on what strategy have you used to install VS Buld tools).

Visual Studio 2013/15 doesn't load portable Silverlight projects after VS2017 install

After installing Visual Studio 2017 on my workplace, I can't build legacy solutions with a common, portable library anymore. The library was supposed to work both under regular .NET and Silverlight. The .proj file hasn't been changed for a very long time and worked perfectly, until installing VS2017. I think it's not possible with VS2017, but VS2013 and VS2015 also can't load it anymore.
Is it possible that VS2017 installation removed the Silverlight support, even from the old Visual Studios?
Edit: I could compile it, after using VS 2015, to remove the targets Windows 8 and Windows Phone Silverlight 8, leaving only .NET Framework 4 and Silverlight 5. VS 2013 could also load it after the change. The project was supposed to only work with Webbrowser Silverlight, not anything Windows 8 or phone, probably targeted by default.
VS 2015 error:
There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.
(note: the project properties under Build have only "AnyCPU", I can't switch to x86 anymore, not even when setting the solution to x86)
VS 2013 error:
VS2013 doesn't load the project at all. Upon clicking "Reload Project" in context menu, I get the message:
"Visual Studio update required
One or more projects require a framework SDK (.NETPortable, Version=v4.0, Profile=Profile136) that is either not installed or is included as part of a future update to Visual Studio"
The download link only takes me to the Microsoft landing page, nothing related.
Installing Visual Studio 2017 doesn't affect Visual Studio 2015's support for Silverlight.
An answer to a similar question can be found here:
https://stackoverflow.com/a/21725379/90287
Try installing Microsoft .NET Portable Library Reference Assemblies
4.6

How to enable C# 6.0 feature in Visual Studio 2013?

I was going through the latest features introduced in C# 6.0 and just followed an example of auto property initializer,
class NewSample
{
public Guid Id { get; } = Guid.NewGuid();
}
but my IDE did not recognize the syntax.
I am wondering how I could enable C# 6.0 in Visual Studio 2013. The Target framework I am using is 4.5.1.
Under VS2013 you can install the new compilers into the project as a nuget package. That way you don't need VS2015 or an updated build server.
https://www.nuget.org/packages/Microsoft.Net.Compilers/
Install-Package Microsoft.Net.Compilers
The package allows you to use/build C# 6.0 code/syntax. Because VS2013 doesn't natively recognize the new C# 6.0 syntax, it will show errors in the code editor window although it will build fine.
Using Resharper, you'll get squiggly lines on C# 6 features, but the bulb gives you the option to 'Enable C# 6.0 support for this project' (setting saved to .DotSettings).
As mentioned by #stimpy77: for support in MVC Razor views you'll need an extra package (for those that don't read the comments)
Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
If you want full C# 6.0 support, you'll need to install VS2015.
Information for obsoleted prerelease software:
According to this it's just a install and go for Visual Studio 2013:
In fact, installing the C# 6.0 compiler from this release involves little more than installing a Visual Studio 2013 extension, which in turn updates the MSBuild target files.
So just get the files from https://github.com/dotnet/roslyn and you are ready to go.
You do have to know it is an outdated version of the specs implemented there, since they no longer update the package for Visual Studio 2013:
You can also try April's End User Preview, which installs on top of Visual Studio 2013.
(note: this VS 2013 preview is quite out of date, and is no longer updated)
So if you do want to use the latest version, you have to download the Visual Studio 2015.
A lot of the answers here were written prior to Roslyn (the open-source .NET C# and VB compilers) moving to .NET 4.6. So they won't help you if your project targets, say, 4.5.2 as mine did (inherited and can't be changed).
But you can grab a previous version of Roslyn from https://www.nuget.org/packages/Microsoft.Net.Compilers and install that instead of the latest version. I used 1.3.2. (I tried 2.0.1 - which appears to be the last version that runs on .NET 4.5 - but I couldn't get it to compile*.) Run this from the Package Manager console in VS 2013:
PM> Install-Package Microsoft.Net.Compilers -Version 1.3.2
Then restart Visual Studio. I had a couple of problems initially; you need to set the C# version back to default (C#6.0 doesn't appear in the version list but seems to have been made the default), then clean, save, restart VS and recompile.
Interestingly, I didn't have any IntelliSense errors due to the C#6.0 features used in the code (which were the reason for wanting C#6.0 in the first place).
* version 2.0.1 threw error The "Microsoft.CodeAnalysis.BuildTasks.Csc task could not be loaded from the assembly Microsoft.Build.Tasks.CodeAnalysis.dll. Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
UPDATE One thing I've noticed since posting this answer is that if you change any code during debug ("Edit and Continue"), you'll like find that your C#6.0 code will suddenly show as errors in what seems to revert to a pre-C#6.0 environment. This requires a restart of your debug session. VERY annoying especially for web applications.
It worth mentioning that the build time will be increased for VS 2015 users after:
Install-Package Microsoft.Net.Compilers
Those who are using VS 2015 and have to keep this package in their projects can fix increased build time.
Edit file packages\Microsoft.Net.Compilers.1.2.2\build\Microsoft.Net.Compilers.props and clean it up. The file should look like:
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
Doing so forces a project to be built as it was before adding Microsoft.Net.Compilers package
It is possible to use full C# 6.0 features in Visual Studio 2013 if you have Resharper.
You have to enable Resharper Build and voilĂĄ!
In Resharper Options -> Build - enable Resharper Build and in "Use MSBuild.exe version" choose "Latest Installed"
This way Resharper is going to build your C# 6.0 Projects and will also not underline C# 6.0 code as invalid.
I am also using this although I have Visual Studio 2015 because:
Unit Tests in Resharper don't work for me with Visual Studio 2015 for some reason
VS 2015 uses a lot more memory than VS 2013.
I am putting this here, as I was looking for a solution for this problem for some time now and maybe it will help someone else.
It seems there's some misunderstanding. So, instead of trying to patch VS2013 here's and answer from a Microsoft guy: https://social.msdn.microsoft.com/Forums/vstudio/en-US/49ba9a67-d26a-4b21-80ef-caeb081b878e/will-c-60-ever-be-supported-by-vs-2013?forum=roslyn
So, please, read it and install VS2015.

Categories