I want to start using Microsoft.Net.Compilers
to simplify work with our build server. However, I could only get it to work at a per-project level, by adding the package to all projects.
This is problematic because the package would have to be added to each new created project. This could lead to a case where the code compiles on the developer's machine (which has the latest compiler), but would fail on the build server. We have many projects (over 100), so this is relatively common.
Is there a way of using Microsoft.Net.Compilers
at solution level?
If there is no supported way, is there a command line tool I would not have to install on the build server? Alternatively, is this not an intended usage of these tools?
Typically, . NET apps are compiled to intermediate language (IL). At run time, the just-in-time (JIT) compiler translates the IL to native code.
NET Compilers Toolset Package. Referencing this package will cause the project to be built using the C# and Visual Basic compilers contained in the package, as opposed to the version installed with MSBuild. This package is primarily intended as a method for rapidly shipping hotfixes to customers.
NET Compiler Platform, also known by its codename Roslyn, is a set of open-source compilers and code analysis APIs for C# and Visual Basic (VB.NET) languages from Microsoft.
The . NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs. You can build code analysis tools with the same APIs that Microsoft is using to implement Visual Studio! Also includes the Syntax Visualizer, a Visual Studio ... Download.
If in VS 2017 (update 1, build numbers >= 15.1.*) you can use the MSBuild integrated PackageReference
mechanism instead of packages.config
which was previously only available for .net core and .net standard project types. See the PackageReference documentation as well as the NuGet blog post announcing the support, especially the section "What about other project types that are not .NET Core?".
The idea is to switch from installing a package and adding it to packages.config for restore to just specifying an MSBuild items in the csproj file. This can be set up for new projects in VS: (animation is from the NuGet blog post linked above)
A new feature of MSBuild 15 is that it supports automatically including files in the directory hierarchy that have special names. Those are Directory.Build.props
and Directory.Build.targets
which will get included before (props) and after (targets) your project file's content (there is a bug with the .targets
version for multi targeting projects for which a fix is about to be released).
If you create a Directory.Build.props
file with the following content at the solution level, all projects in the directory hierarchy below it will inherit it's content and you can force a NuGet dependency onto each project:
<Project>
<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers" Version="2.1.0"/>
</ItemGroup>
</Project>
Right-click your solution > Manage NuGet Packages for Solution...
... Or:
Tools > Library Package Manager > Manage NuGet Packages for Solution...
Then add it to all the projects by browsing for your package, then checking the top checkbox, and clicking install.
Source : https://stackoverflow.com/a/8653312/7007466
Create a template mimicking the original one for each type of project you need (Console, Library etc...) and adding the package to it.
Note: Use only valid identifier characters when naming a project that will be the source for a template. A template exported from a project named with invalid characters can cause compilation errors in future projects based on the template.
If you have the Visual Studio SDK installed, you can wrap the finished template in a .vsix file for deployment by using the VSIX Project template.
Source : https://msdn.microsoft.com/en-us/library/xkh1wxd8.aspx
If someone has an easier way than creating templates, I'll gladly take it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With