Visual Studio 2017 web project: can't use C#7 - c#

I've created a brand new web project in Visual Studio 2017, but I can't use any C#7 features in this web project. No errors are reported in Visual Studio, but the build fails and this is verified by the output window which shows the CSC task failing.
Here is a project which reproduces the issue.
https://drive.google.com/open?id=0B-mqMIMqm_XHQkxONjRLYzA3SUk
It's an out of the box VS web project with the exception of the addition of the following code in HomeController.cs:
var i = 1_000_000;
This uses _ which is a C#7 feature. Remove this line and the build succeeds.
Non-web projects compile fine with C#7 features, and I've tracked down the difference to a different compiler being used for web projects. Non-web projects use the following compiler:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn\csc.exe
Web projects use this compiler:
[Path to solution root]\packages\Microsoft.Net.Compilers.1.3.2\build..\tools\csc.exe
How can I resolve this?

I tried it on my machine and I had the same problem.
The solution is to install the newest Microsoft.Net.Compilers package.
For example by Tools->NuGet package Manager -> Package Manager Console
and then type:
Install-Package Microsoft.Net.Compilers

Related

How to build OPC Foundation .NET library with Visual Studio

I am trying to build the OPC foundation provided SDK by visual studio. I face lots of builds error.
I tried VS2013, VS2017 community version, and VS2019 professional version.
Does anybody have experience building this SDK?
The source code can be downloaded from Github.
https://github.com/OPCFoundation/UA-.NETStandard
Following images show the dot net version and the errors
Which solution are you trying to build?
As astrowalker indicated can you share the error messages?
This worked for me:
cloned the repository
opened solution "UA Core Library" within VS2019
restore nuget packages
rebuild solution
run all test of project "Opc.Ua.Core.Tests"

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.

Using C# 7.1 with MSBuild

To use the new C# 7.1 language features with Visual Studio 2017, you add the setting <LangVersion>latest</LangVersion> to your project file(s).
However, building such projects from MSBuild (version 15.3.409.57025, located at C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin) results in an error:
CSC : error CS1617: Invalid option 'latest' for /langversion;
must be ISO-1, ISO-2, Default or an integer in range 1 to 6.
Is this feature just not yet supported by MSBuild, or is it possible to get this working?
This covers 200+ projects that were originally created variously in Visual Studio 2013 and 2015. They were all re-targeted to .NET 4.7 using the Target Framework Migrator tool (which saved lots of clicking and appears - based on inspecting .csproj file changes - to do the job correctly).
The projects all build successfully from Visual Studio 2017.
Nuget packages
Microsoft.Net.Compilers nuget package does not work and needn't to be installed.
Set the following project/build settings
Set at least C# 7.1 or higher in the Debug and Release build properties. (via: Project menu > [ProjectName] Properties > Build tab > [Advanced] button > Language Version).
Setting it to latest does not work.
Also make sure that you are running the latest MSBuild version.
Add a reference to the Microsoft.Net.Compilers package (version 2.3.x, to get C# 7.1).
A short description of the package:
Microsoft.Net.Compilers
This package not only includes the C# and
Visual Basic compilers, it also modifies MSBuild targets so that the
included compiler versions are used rather than any system-installed
versions. Once installed, this package requires Microsoft Build Tools
2015.
Make sure you have changed for "All Configuration" and not just "Debug"
else you will be baffling why it is failing at production.
In case you land here because you get the error as the OP mentioned, running msbuild via command line (e.g. from a build agent such as jenkins), the solution may be as easy as to upgrade Microsoft Build Tools 2015.
You can do that via choco install microsoft-build-tools or manually via the official Microsoft Build Tools 2015 or by updating your Visual Studio 2017 installation.
I've got a solution with a C# console app using C# 7.1 here.
Using the VS 2017 command line (and thus MSBuild 15.3.409.57025) it worked fine. (The .csproj does contain <LangVersion>latest</LangVersion>.)
Is this feature just not yet supported by MSBuild, or is it possible to get this working?
Yes it is.
Which instance of csc.exe is being run and what's its version? Because it looks like, despite quoting the version, you have the wrong version of csc.exe (the error message says 1-6 so not even C# 7 would work).
We discovered that our MVC projects were triggering this issue.
To fix, we updated the Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package used by these projects from version 1.0.0 to 1.0.7.

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.

Visual Studio 2015 enable MVC 4 support

I have an ASP NET MVC 4 project that "works on my machine(old)". When I load a project in a fresh installation of VS 2015(on a new laptop) - a lot of assemblies are missing and when I try to build the project it complains that classes like ActionResult, Controller are missing.
I checked that on the old laptop the missing dependencies are coming from C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies. On the new laptop I have only the xml descriptors of the packages but not the dlls.
What I have to install to use MVC 4 in Visual Studio 2015?
Edit: missing dependencies are:
System.Web.Helpers
System.Web.Mvc
System.Web.WebPages
System.Web.WebPages.Deployment
System.Web.WebPages.Razor
I Recommend getting them again from Nuget for your ActionResult Issue its Microsoft.AspNet.Mvc which is making issue.
Open Package Manager Console and insert the command below:
Install-Package Microsoft.AspNet.Mvc
Nuget has this feature that allows you to restore all the missing Assemblies which exists in nuget. you can try that out as well.
Anyway for the rest of them install these packages below as well:
Install-Package Microsoft.AspNet.WebHelpers
Install-Package Microsoft.AspNet.WebPages
Install-Package Microsoft.AspNet.Razor
Check if you have install update 3 for VS2015
also check if Microsoft web developer tools are installed correctly
I have had the same issues when using source control or moving projects from one PC to another.
There is a work around that I use quite often and it works.
Go to YourProjectFolder -> packages.
Remove all the folders except for the xml, repositories.xml, file which is the xml config file for packages downloaded from Nuget.
On your solution in Visual Studio Right click and select Enable Nuget Packages Restore.
Then build the application it will re-download all the required packages.
Hope this helps
For Me what solved the issue is to install ASP.NET MVC 4 for Visual Studio 2010 from Microsoft Web Page

Categories

Resources