How can I build a Visual Studio 2017 .NET Core .exe file on a developer machine? - exe

Please see Solution section below for the TLDR.
I used the Visual Studio 2015 Community edition and upgraded to Visual Studio 2017. I am developing a .NET Core console application. I am talking here only about my local development machine, which has Visual Studio installed and it has .NET Core runtimes and SDKs installed.
In Visual Studio 2015, project.json files were used, and by default the build process produced a DLL file in the bin/Debug/netcoreapp1.1, that one could run using the dotnet run command. But there was an option to make a minor tweak in the project.json file:
...
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
...
->
...
"Microsoft.NETCore.App": {
"version": "1.0.1"
},
...
And adding a runtimes section, such as
"runtimes": {
"win10-x64": {}
}
And if you did this and built your project, an EXE file was created that you could run directly, without using dotnet run.
Now the question is, is the same possible in Visual Studio 2017 using .csproj XML files? And more specifically, is it possible without the heavy self-contained deployments? I do want it without the full SDD publish because I use these executables for just local testing during the development. Once the project is to be released, I do the SDD publish, but that is very inconvenient during the development process.
The project was converted to Visual Studio 2017, but it only generates DLL files again. So, I tried to create a completely new and empty Hello, World! program and try to get EXE file from it. The Hello, World! csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
</Project>
This only generates a DLL file that you can run with dotnet run. It is possible to make a change to this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
</PropertyGroup>
</Project>
which is very close to what I want - generating an EXE file. However, this creates EXE file that runs .NET Framework 4.6, not .NET Core. So, it does not solve the problem. What I really want is .NET Core executable without having to do self-contained deployment. Is it possible?
Solution
I just found out that this is indeed possible. I just don't know how to do it in the IDE. What currently happens if I hit Ctrl + Alt + F7 in the IDE produces the same result as the command:
dotnet build --configuration Debug
And what I want can be produced by command:
dotnet build --configuration Debug --runtime win10-x64
So what I really want now is modify the default IDE build command to replicate this behavior with --runtime win10-x64 parameter.

In .NET Core, if you want an .exe produced during dotnet build, you need to supply the runtime that you want the executable built for.
In project.json, you did it as you describe above by removing type: platform and adding a runtimes section.
In .csproj, you do it by specifying the MSBuild property named RuntimeIdentifier. On the command-line, when you say dotnet build --runtime win10-x64, the --runtime value gets passed into MSBuild has the RuntimeIdentifier property.
One option is you can purely set the RuntimeIdentifier in your .csproj:
<PropertyGroup>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
</PropertyGroup>
Now when you build in Visual Studio, it will produce an .exe in the build output folder.
Alternatively
I'm not sure if you are interested in this or not, but it sounds like you just want a way to run your application without calling dotnet run. If this is true, you don't need to set a runtime at all. By default your application gets built into bin\Debug\netcoreapp1.1\AppName.dll. You can run your application by saying dotnet bin\Debug\netcoreapp1.1\AppName.dll, which is basically what dotnet run does under the covers.

Related

Simplest way to build dotnet SDK project requiring net461 on MacOS

I have a dotnet SDK .sln (and a build.proj) with <TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>. It builds on Windows using Visual Studio and dotnet build, but I'd also like it to build as many other places as possible. What do I need to put in my README.md and/or what can I put in the project files to make it build on Rider and/or on from bash on a Mac?
(using .NET Core SDK) The simplest way to build for a .NET Framework TFM when running on either macOS or Linux using the .NET Core CLI, is to utilize the .NET Framework Targeting Pack Nuget Packages from Microsoft (currently in preview):
These packages enable building .NET Framework projects on any machine with at least MSBuild or the .NET Core SDK installed.
The following scenarios and benefits are enabled for .NET Framework projects:
Build without requiring admin operations to install pre-requisites such as Visual Studio or .NET Framework targeting packs.
Build libraries on any operating system supported by the .NET Core SDK.
Build Mono-based projects.
You may either include the Microsoft.NETFramework.ReferenceAssemblies metapackage;
or use just the specific package, which is in your case Microsoft.NETFramework.ReferenceAssemblies.net461.
Add the package to the *.csproj or your Directory.Build.props:
<Project>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
</Project>
Note: The PrivateAssets attribute controls which dependency assets will be consumed but won't flow to the parent project. See the docs.
Update
This is no longer required using the .NET 5 SDK (e.g. 5.0.100), which will now automatically add the PackageReference to the ReferenceAssemblies for .NET Framework.
In order to build via bash on a vanilla Mac, the minimal steps seem to be:
Install Mono 6.0 (5.2 is recommended for VS Mac interop, I dont care about that, and Mono 6.0's interop with Dotnet core is better)
Install dotnet SDK 2.2 (doesn't have to be exactly that, but it works for me)
Put this in a Directory.build.props file (open to improvements if anyone has any)
<Project>
<PropertyGroup>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))'== 'true'">true</IsOSX>
</PropertyGroup>
<PropertyGroup Condition=" '$(IsOSX)' == 'true' ">
<FrameworkPathOverride>/Library/Frameworks/Mono.framework/Versions/Current/Commands/../lib/mono/4.6.1-api</FrameworkPathOverride>
</PropertyGroup>
</Project>
Bash: dotnet build SolutionFileName.sln should now work
Install Rider 2019.1 or later
Rider: should Just Work (it should autodetect msbuild 16.0 in the build tools section)

Unable to build Compiled Azure Function in VSTS (Error MSB4019)

I have created an Azure Function using the latest build of visual studio 2017 (15.3.1).
This build allows me to do the:
New Project > Azure Function App approach
This produces a .csproj (not a .funproj file) and it's doesn't come bundled with a website or anything. I've tried .Net 4.6 and 4.6.1 as the target framework and neither have worked.
I get the following error in VSTS:
C:\Users\Builder.nuget\packages\microsoft.net.sdk.functions\1.0.2\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Publish.props
(13, 3)
C:\Users\Builder.nuget\packages\microsoft.net.sdk.functions\1.0.2\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Publish.props(13,3):
Error MSB4019: The imported project "C:\Program Files (x86)\Microsoft
Visual
Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk.Publish\Sdk\Sdk.Props"
was not found. Confirm that the path in the declaration is
correct, and that the file exists on disk.
There is no node in my .csproj file so I am not sure where it is getting that from.
I'm using the latest NuGet option in VSTS, I'm using the Latest VS Version option as well.
The build steps are:
Get Sources
NuGet Restore
Build Solution (MSBuild.exe exited with code '1' every time)
Is there any way to build (and then release) compiled .Net functions in VSTS at the minute?
I was able to solve this by swapping the build agent from the "Default" group to the latest, "Hosted VS 2017" group, and that has compiled fine.
Looks like the machine is missing the latest dotnet cli. I'm not too familiar with VSTS build machines, but you'd need to get that on there somehow https://www.microsoft.com/net/download/core

Using VSTS Release Definition to run Entity Framework Code Database Migration

I am writing a .NET Core and Entity Framework Core Application using Code-First Migrations.
I want to be able to deploy it to an Azure Web App using Visual Studio Team Services Build and Release Definitions
I want to be able to run the Database Migration as part of the Release Definition using the script
dotnet ef database update
I've done this via a Command Prompt action in the Release Definition
However I always get the message
No executable found matching command "dotnet-ef"
I've tried making sure that this command is running in the same directory as a .cproj file
I've also tried running a
dotnet restore
as a previous command prompt task, and this gives an error
The folder 'C:\a\r1\a\Drop\s\src\xxxxx' does not contain a project to restore even though it does.
Has anybody tried to do a Code First Migration as part of a Team Services Release Definition step?
My other option was to run the migration as part of the Web Application itself but I wanted to run it via the Release process rather than run it in the Application.
You need to use Hosted VS2017 agent instead of Hosted agent. My steps:
Edit csproj file to add this code below and check in changes (Refer to .NET Command Line Tools article)
Code:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
Command line step/task (Tool: dotnet; Arguments: --version)
Command line step/task (Tool: dotnet; Arguments: restore)
Command line step/task (Tool: dotnet; Arguments: ef --version)
Queue build with Hosted VS2017 agent.

Compiling C# .Net from Command Line

I'm trying to compile a c# .net project only using command line so that I could create a bat file to compile and run the Selenium tests. I exported the code from SVN. And I opened the command prompt and am able to restore packages using below command.
And then when I try to compile with msbuild it could not reference the dll added in through the nuget. I know in visual studio through package manager console I can use a command (update -reinstall) and make it work, but how to do this without visual studio.
==== bat file ======
REM Restore External Pacakages
nuget restore packages.config -PacakgesDirectory D:\Testproject\packages"
msbuid TestProject.sln
Basically you need to reference Msbuild executable
C:\Windows\Microsoft.Net\Framework\v4.0.30319\MSBuild.exe
OR
C:\Windows\Microsoft.Net\Framework64\v4.0.30319\MSBuild.exe
then you need to pass the .sln or .csproj file path (depends on your working folder) as parameter.
You can also specify the msbuild's targets ex. Clean, Rebuild, Build => As they exists in Visual Studio.
The final command could look like this:
Template:
{.NetFrameworkPath}\msbuild.exe {pathToSln} /t:{Build,Clean,Rebuild}
Examples
C:\Windows\Microsoft.Net\Framework64\v4.0.30319\MSBuild.exe TestProject.sln /t:Build
C:\Windows\Microsoft.Net\Framework64\v4.0.30319\MSBuild.exe TestProject.sln /t:Clean;Build

How to use msbuild to create csx output for Azure SDK 1.8?

I have a Visual Studio project with an Azure Cloud project that has one Web Role for a WCF Service. Rather than creating a cspkg by using the Package command in Visual Studio or with cspack, I need to create the same output that is in the project's csx folder with msbuild. However, if I run the following command in msbuild, the output doesn't have the same folder structure as the csx folder:
msbuild MyAzureProject.ccproj /p:configuration=debug /maxcpucount /p:outdir="c:\OutDir" /p:overwritereadonlyfiles=true /p:targetprofile="Cloud"
Instead it creates a _PublishedWebsites folder. Does anyone know how to create the contents of the csx folder manually by using the msbuild command-line (i.e. csx/roles/MyAzureRole/approot)?
You're just missing the targets switch.
Here's what works for me.
msbuild MyAzureProject.ccproj /p:configuration=debug /maxcpucount /p:outdir="c:\OutDir" /p:overwritereadonlyfiles=true /p:targetprofile="Cloud" /target:Clean;Publish

Resources