Welcome to Red Hat Enterprise Linux, MSBuild, a build tool for .NET Core CLI!
Microsoft announced the first “alpha” release of the new MSBuild-based .NET Core tools. .NET Core SDK which can be downloaded from the Red Hat Developer Program site consists of .NET Core Runtime and .NET Core command line tools (.NET Core CLI). (Reminder – you must have a Red Hat Enterprise Linux subscription first. If you don’t, you can go here for a no-cost subscription.) The MSBuild tool is included in .NET Core 1.0 preview 3 (not in the latest release .NET Core 1.1). The version number is something complicated because .NET CLI is not GA but still under preview. The MSBuild tool can be used with both .NET Core 1.0 and .NET Core 1.1 runtimes. RHEL is not listed in the .NET Core SDK 1.0 Preview 3 download list. But you can try MSBuild with the .NET Core CLI daily build.
NOTE: Red Hat has just released .NET Core 1.1. However, .NET Core 1.1 doesn’t include the MSBuild tool, you can try MSBuild following this blog.
Install .NET Core daily build
We can get the download link in the dotnet/cli repository. This build is now under development as preview 5. After downloading the latest .NET Core SDK Binaries for RHEL 7.2, please execute the following commands to install.
$ sudo mkdir -p /opt/dotnet $ sudo tar zxf dotnet.tar.gz -C /opt/dotnet $ sudo ln -s /opt/dotnet/dotnet /usr/local/bin
You can update the SDK again with the same commands above after downloading a new binary when a new daily build is available. If you have already installed the .NET Core SDK via Red Hat Software Collections or other places, please be sure to configure the PATH environmental variable. Here is my environment.
$ which dotnet /opt/dotnet/dotnet $ dotnet --info .NET Command Line Tools (1.0.0-preview4-004130) Product Information: Version: 1.0.0-preview4-004130 Commit SHA-1 hash: a0060ac321 Runtime Environment: OS Name: rhel OS Version: 7.3 OS Platform: Linux RID: rhel.7.2-x64 Base Path: /opt/dotnet/sdk/1.0.0-preview4-004130
Working with csproj
The new dotnet CLI tools generate csproj instead of project.json.
$ dotnet new Created new C# project in /home/tanaka-takayoshi/MSBuildSample. $ ls MSBuildSample.csproj Program.cs
“dotnet restore” and “dotnet run” are the same as before.
$ dotnet restore Restoring packages for /home/tanaka-takayoshi/MSBuildSample/MSBuildSample.csproj... Writing lock file to disk. Path: /home/tanaka-takayoshi/MSBuildSample/obj/project.assets.json Generating MSBuild file /home/tanaka-takayoshi/MSBuildSample/obj/MSBuildSample.csproj.nuget.g.targets. Generating MSBuild file /home/tanaka-takayoshi/MSBuildSample/obj/MSBuildSample.csproj.nuget.g.props. Restore completed in 486.995ms for /home/tanaka-takayoshi/MSBuildSample/MSBuildSample.csproj. NuGet Config files used: /home/tanaka-takayoshi/.nuget/NuGet/NuGet.Config Feeds used: https://api.nuget.org/v3/index.json $ dotnet run Hello World!
Now let’s look into the csproj, in the csproj of .NET Core, we can include all the source files with wildcard although we must include all the source files separately in the original csproj of .NET Framework.
<ItemGroup> <Compile Include="**\*.cs" /> <EmbeddedResource Include="**\*.resx" /> </ItemGroup>
Next, if you add NuGet library to your project, you have to add ItemGroup/PackageReference element. Then execute “dotnet restore” command.
<ItemGroup> <PackageReference Include="NewtonSoft.Json"> <Version>9.0.1</Version> </PackageReference> </ItemGroup>
If you have project.json style project, you can migrate it to csproj and only have to execute the “dotnet migrate” command. This command converts without confirmation, but you can restore the original project.json file under backup directory.
$ ls Program.cs project.json $ dotnet migrate Project Migrate migration succeeded (/tmp/Migrate) Summary Total Projects: 1 Succeeded Projects: 1 Failed Projects: 0 $ ls * Migrate.csproj Program.cs backup: project.json
Visual Studio Code Support
Visual Studio Code C# extension now supports csproj file. This is the initial support and I’m looking forward to more features like intelisense support in csproj.
About the author:
Takayoshi Tanaka is a Software Maintenance Engineer at Red Hat. He is mainly in charge of OpenShift, .NET Core on Red Hat Enterprise Linux and Red Hat solutions on Microsoft Azure. He is a Microsoft MVP for Visual Studio and Development Technologies. He writes many articles in his personal blog and web sites, and also provides many technical sessions at community events.
Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets, books, and product downloads.