title | description | keywords | author | manager | ms.date | ms.topic | ms.prod | ms.technology | ms.devlang | ms.assetid |
---|---|---|---|---|---|---|---|---|---|---|
Using MSBuild to build .NET Core projects |
Using MSBuild to build .NET Core projects |
.NET, .NET Core |
dsplaisted |
wpickett |
06/20/2016 |
article |
.net-core |
.net-core-technologies |
dotnet |
13c66464-4f14-4db6-aa8b-06f25e7ba894 |
The .NET Core tooling is going to move from project.json to MSBuild based projects. We expect the first version of the .NET Core tools that use MSBuild to ship along with the next version of Visual Studio. However, it is possible to use MSBuild for .NET Core projects today, and this page shows how.
We recommend that most people targeting .NET Core with new projects today use the default tooling experience with project.json because of the following reasons:
- MSBuild doesn't yet support a lot of the benefits of project.json
- A lot of the ASP.NET based tooling doesn't currently work with MSBuild projects
- When we do release the .NET Core tooling that uses MSBuild, it will be able to automatically convert from project.json to MSBuild projects
You may want to use MSBuild to target .NET Core for existing projects that already use MSBuild that you want to port to .NET Core, or if you are using MSBuild's extensibility in your build for scenarios that are not well supported for project.json projects.
- Visual Studio 2015 Update 3 RC or higher
- .NET Core tools for Visual Studio
- NuGet Visual Studio extension v3.5.0-beta or later
- In the Visual Studio menu bar, choose File | New | Project and select Class Library (Portable)
-
Choose a name and location for your project and click OK
-
The "Add Portable Class Library" dialog will appear. Select .NET Framework 4.6 and ASP.NET Core 1.0 as targets and click OK
- In Solution Explorer, right click on your project and choose Properties
- In the Library tab of the project properties, click on the Target .NET Platform Standard link, and click Yes in the dialog that is shown
- Open the
project.json
file in your project, and make the following changes:-
Change the version number of the
NETStandard.Library
package to1.6.0
(this is the .NET Core 1.0 version of the package) -
Add the below
imports
definition inside thenetstandard1.6
framework definition. This will allow your project to reference .NET Core compatible NuGet packages that haven't been updated to target .NET Standard"netstandard1.6": { "imports": [ "dnxcore50", "portable-net452" ] }
-
Building a console application for .NET Core requires some customization of the MSBuild build process. You can find a sample project for a .NET Core console application called CoreApp in the corefxlab repo. Another good option is to start with coretemplate, which uses separate MSBuild targets files to target .NET Core instead of putting the changes directly in the project file.
It is also possible to start by creating a project in Visual Studio and modifying it to target .NET Core. The instructions below show the minimal steps to get this working. In contrast to CoreApp or coretemplate, a project created this way won't include configurations for targeting Linux and macOS.
-
In the Visual Studio menu bar, choose File | New | Project and select Console Application
-
Choose a name and location for your project and click OK
-
In Solution Explorer, right click on your project, choose Properties, and then go to the Build tab
-
In the Configuration dropdown (at the top of the properties page), select All Configurations, and then change the Platform Target to x64
-
Delete the
app.config
file from the project -
Add a
project.json
file to the project with the following contents:{ "dependencies": { "Microsoft.NETCore.App": "1.0.0-rc2-3002702" }, "runtimes": { "win7-x64": { }, "ubuntu.14.04-x64": { }, "osx.10.10-x64": { } }, "frameworks": { "netcoreapp1.0": { "imports": [ "dnxcore50", "portable-net452" ] } } }
-
In Solution Explorer, right click on the project, choose Unload Project, then right click again and choose Edit MyProj.csproj, and make the following changes
-
Remove all the default
Reference
items (toSystem
,System.Core
, etc.) -
Add the following properties to the first
PropertyGroup
in the project:<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier> <TargetFrameworkVersion>v1.0</TargetFrameworkVersion> <BaseNuGetRuntimeIdentifier>win7</BaseNuGetRuntimeIdentifier> <NoStdLib>true</NoStdLib> <NoWarn>$(NoWarn);1701</NoWarn>
-
Add the following at the end of the file (after the import of
Microsoft.CSharp.Targets
):<PropertyGroup> <!-- We don't use any of MSBuild's resolution logic for resolving the framework, so just set these two properties to any folder that exists to skip the GetReferenceAssemblyPaths task (not target) and to prevent it from outputting a warning (MSB3644). --> <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory)</_TargetFrameworkDirectories> <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory)</_FullFrameworkReferenceAssemblyPaths> <!-- MSBuild thinks all EXEs need binding redirects, not so for CoreCLR! --> <AutoUnifyAssemblyReferences>true</AutoUnifyAssemblyReferences> <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects> <!-- Set up debug options to run with host, and to use the CoreCLR debug engine --> <StartAction>Program</StartAction> <StartProgram>$(TargetDir)dotnet.exe</StartProgram> <StartArguments>$(TargetPath)</StartArguments> <DebugEngines>{2E36F1D4-B23C-435D-AB41-18E608940038}</DebugEngines> </PropertyGroup>
-
Close the .csproj file, and reload the project in Visual Studio
-
-
You should be able to run your program with F5 in Visual Studio, or from the command line in the output folder with
dotnet MyApp.exe