-2

I want to use the new C#6 Language Features. https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6

public class Foo {
  public string ToolTip { get; set; } = "This is my toolip";
}

To that end I've upgraded all of our solutions to be VS2015 solutions.

I'm working in an MVC4 project targeting .NET4.

This compiles fine but fails to build on our build server which caused me to question exactly how all of the pieces fit together for roll-out.

It would seem to me that new language features would necessitate using the new compiler (Roslyn? which comes pre-installed with VS2015?).

The compiler would take my code and transform that into IL which gets executed by the run-time. So as long as the compiler supports the version of the .NET runtime I'm targeting, it seems my front-end webservers will not need to be upgraded for us to take advantage of C#6 features.

The point that makes me pause though is code that is JIT compiled, such as cshtml files.

Where can I go to read more about these inter dependencies?

John Zumbrum
  • 2,786
  • 8
  • 36
  • 60
  • 1
    You probably need to update your Build Server. MSBuild on your build server is probably out-of-date. – Russ Dec 10 '15 at 18:30
  • Marked as offtopic because SO is not a place to ask for tutorials as per the rules. See [ask] – Camilo Terevinto Dec 17 '15 at 20:34
  • @cFrozenDeath Could you give some suggestions on how to rephrase the question to match the answer I provided below? I had difficulty in finding resources that explained how all of these pieces fit together so I think it may be helpful to others searching. – John Zumbrum Dec 17 '15 at 20:39
  • Your answer provides nice information, but there are many long topics in there. I'm not sure this kind of information fits SO style, as it's not really "programming" but how some technologies work. – Camilo Terevinto Dec 17 '15 at 21:25

2 Answers2

2

C# Language

  • C# is a high-level language that gets transformed into MSIL by the compiler
  • MSIL is platform/language agnostic: C#, VBasic, F# all compile to the same MSIL
  • MSIL is executed by the .NET CLR
  • The C# language version is independent of the .NET CLR, so you do not need to update the CLR on the server running the code
  • In your project file, choose the version of the .NET framework to target which determines what MSIL is emitted by the compiler so it can be run on the target server
  • Certain language features in the future may be incompatible with old versions of the .NET Framework, but in that case your project will fail to build at compile time
  • New language features are only available with updates to the compiler(s)
  • ASP.NET is unique in that there is the compilation that happens during development, but also the run-time compilation of view files

Compiler

  • Starting in 2015, Roslyn is the new C# compiler
  • Prior to Roslyn, csc.exe was the main compiler, and it shipped with the .NET Framework
  • The ASP.NET runtime will compile views on-the-fly. To make it use Roslyn for this, install the CodeDOM Provider NuGet Package in your MVC project
  • Roslyn on GitHub

MSBuild

  • Suite of tools around building/deploying .NET applications
  • Leverages a specific compiler under the covers; I don't see a way to force MSBuild to use an older compiler
  • Used to ship with the .NET framework under C:/Windows/Microsoft.NET/Framework/v3.5/MSBuild.exe, leveraging csc.exe compiler
  • Starting in 2013, it started shipping with Visual Studio Releases under C:/Program Files (x86)/MSBuild/14.0/MSBuild.exe, leveraging Roslyn compiler
  • MSBuild on GitHub

Visual Studio 2015

  • The main code editing environment for C# and .NET applications
  • Installs .NET Framework 4.6.1
  • Installs MSBuild 14.0
  • Leverages MSBuild.exe to compile your C# code
  • Immediately allows writing C#6 code in the editor

.NET Framework/CLR

  • The .NET Framework is the Common Language Runtime (CLR) + managed libraries and tools
  • The CLR always ships with the .NET Framework major version
  • Targeting different versions of the .NET Framework gives you access to new/modfied libraries, as well as performance improvements when executing your MSIL code in the new CLR
  • Note: ASP.NET and ASP.NET MVC are part of the .NET Framework, so you
  • Current Versions

To use C#6 Language Features in MVC:

  • Install latest Visual Studio (this uses the latest version of MSBuild under the covers which leverages the latest compiler (Roslyn) to turn C# code into MSIL)
  • Add the new Roslyn CodeDOMProvider to your project via NuGet (this will get deployed to your web server as a referenced dll, and is what the ASP.NET runtime will use to compile views on the fly
  • Update the build server to have the latest version of MSBuild (and compiler) by installing the latest Visual Studio there
  • See MSDN for more details
Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
John Zumbrum
  • 2,786
  • 8
  • 36
  • 60
0

The reason why the buildserver fails is, that it probably uses wrong verion of ms build.

Theres nothing special about roslyn. It's just set of libraries to work with code (parse, compile, etc). In vs2015 and corresponding msbuild is old C# compiler replaced by newer - roslyn.

If you want to know more, there is quite good pluralsight course https://www.pluralsight.com/courses/dotnet-compiler-platform-introduction

When it comes to build server, it depends on what build server you use. In general, you have to install vs2015 and instruct it to use the new msbuild. You can compile C# 6 for different runtimes.

For tfs2013 specify version and change build template TFS 2013 building .NET 4.6 / C# 6.0

Community
  • 1
  • 1
Liero
  • 25,216
  • 29
  • 151
  • 297