How to tell Visual Studio which runtime to build for - c#

How can I tell Visual Studio to build against multiple runtimes?
I've created a simple .NET Core (1.0) console application (Hello World with HTTP download). I want to build it for multiple RIDs (win7-x64, win10-x64, etc) so that I can publish it and include the dependencies for a stand-alone application running on Windows 2008 R2 (win7-x64). But my developer machine where I'm building this is Windows 10, so VS picks win10-x64 so my publish task doesn't find any files for win7-x64. I also want to build specifically for win10-x64, so even if I can force it by removing the rest of the RIDs, I want the option to build against multiple.
I know I can do this with MSBuild and dotnet cli, but I'd like to know how to do it from within VS.
project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": "1.1.0",
"System.Net.Http": "4.1.0"
},
"runtimes": {
"win10-x64": {},
"win81-x64": {},
"win8-x64": {},
"win7-x64": {}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Note that I'm using System.Net.Http which requires different dependencies in win7 vs win10 to test. If I run the win10-x64 on Win2k8 then it will fail because it's missing dependencies for the runtime on win7.
It shouldn't matter, but for reference here's the main code block, program.cs:
using System;
namespace TestOnWindows2008
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World");
using (var httpClient = new System.Net.Http.HttpClient())
{
string result = httpClient.GetStringAsync("https://www.microsoft.com/en-us/").Result;
Console.WriteLine(result.Substring(0, 200));
}
Console.WriteLine("Done");
}
}
}

Although I am not directly answering your question, maybe it will help you.
Since your target platform is win7-x64, which is older than your development platform, win10-x64, you can keep only win7-x64 and use it for both debugging and publishing.
I've just tested you code using Windows 10 x64 Enterprise and everything went ok.
project.json
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": "1.1.0",
"System.Net.Http": "4.1.0"
},
"runtimes": {
"win7-x64": {}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
And Program.cs is the one from your question.
Here is the output:
for run:
C:\temp\test2>dotnet restore && dotnet run
log : Restoring packages for C:\temp\test2\project.json...
log : Writing lock file to disk. Path: C:\temp\test2\project.lock.json
log : C:\temp\test2\project.json
log : Restore completed in 2603ms.
Project test2 (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling test2 for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:01.7257991
Hello World
Done
And for publish
C:\temp\test2>dotnet publish
Publishing test2 for .NETCoreApp,Version=v1.0/win7-x64
Project test2 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
publish: Published to C:\temp\test2\bin\Debug\netcoreapp1.0\win7-x64\publish
Published 1/1 projects successfully

Related

Error loading hostpolicy.dll while deploying .NET Core console app to Azure WebJobs

I have followed this tutorial to deploy a .NET Core console application to an Azure Web Service WebJob.
My app is running locally without any issue (with dotnet 1.0.0-preview2-003131) but when I try to run it from Azure console I have the following error :
Failed to load the dll from [D:\local\VirtualDirectory0\site\wwwroot\app_data\jobs\triggered\PopcornExportWebJob\hostpolicy.dll], HRESULT: 0x800700C1**
An error occurred while loading required library hostpolicy.dll from [D:\local\VirtualDirectory0\site\wwwroot\app_data\jobs\triggered\PopcornExportWebJob]
The version of Azure .Net is 1.0.0-rc4-004771 and the hostpolicy.dll file is the same than I use locally. In fact, when I download the zip of my deploy from Azure and when I run it locally, it is working fine. But it fails on Azure environment.
Also, here is my project.json:
{
"publishOptions": {
"include": [
"run.cmd"
]
},
"buildOptions": {
"emitEntryPoint": true,
"copyToOutput": "appsettings.json"
},
"copyright": "bbougot",
"dependencies": {
"FubarCoder.RestSharp.Portable.Core": "4.0.7",
"FubarCoder.RestSharp.Portable.HttpClient": "4.0.7",
"Microsoft.ApplicationInsights.AspNetCore": "2.0.0",
"Microsoft.Extensions.Configuration": "1.1.0",
"Microsoft.Extensions.Configuration.Json": "1.1.0",
"Microsoft.Extensions.DependencyInjection": "1.1.0",
"Microsoft.Extensions.Logging": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.NETCore.App": "1.1.0",
"MongoDB.Driver": "2.4.2",
"StructureMap.Microsoft.DependencyInjection": "1.3.0"
},
"description": "Popcorn Api Exporter",
"frameworks": {
"netcoreapp1.1": {
"imports": [
"portable-net45+win8"
]
}
},
"runtimes": {
"win10-x64": {}
},
"scripts": {
"postpublish": [ "7za.exe a -tzip PopcornExport.zip .\\bin\\Release\\PublishOutput\\*",
".\\WAWSDeploy.exe .\\PopcornExport.zip .\\popcornexport.PublishSettings /t app_data\\jobs\\triggered\\PopcornExportWebJob /v /d" ]
},
"title": "PopcornExport",
"version": "1.0.0-*"
}
I had to add the node runtimes (win10-x64 otherwise the app can't run locally). But, the Azure Web Service is running on Windows Server 2012. May it be an issue?
What did I miss?
Alright, I've figured it out.
If you want to deploy a dotnet core app to Azure Web Service, include the runtime "win7-x86" if you are running your app in 32-Bit platform mode.
For a Visual Studio 2015 solution, your project.json should include :
"runtimes": {
"win10-x64": {},
"win7-x86": {} //IMPORTANT FOR AZURE DEPLOY
},
Or if you have already migrated to Visual Studio 2017, your .csproj should include this in PropertyGroup:
<RuntimeIdentifiers>win10-x64;win7-x86</RuntimeIdentifiers>
Also, your publish profile should include the same thing:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PublishDir>bin\Release\PublishOutput</PublishDir>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier> // IMPORTANT
</PropertyGroup>
</Project>
I stopped seeing this error when I changed the contents of run.cmd from
dotnet MyWorker.dll
to
MyWorker.exe
This error could happen if the bitness of your application doesn't match the bitness of your App Service (e.g. publishing a 64-bit deployment to an App Service running in 32-bit mode).
To solve this I had to change the bitness to the correct setting in Azure:
To match the bitness of my publish profile in VS:
All the other answers in this topic didn't help me. But then I noticed with dotnet --list-runtimes that I used a newer version of dotnet core on my pc (3.1.18) than was installed by default for the Azure App Service (3.1.16). Once I installed "ASP.NET Core 3.1 (x64) Runtime" with Version 3.1.18 as an Extension in the app service configuration, the problem went away.

create a nuget to share between linux and windows

I have an asp.net form application using .net 4.6.1 running on windows
and a linux application using .netcore 1.1
I want to make a nuget that can be shared between both application.
I created a .net core library and it works pretty fine with my linux application
here is its project.jso
{
"version": "0.1",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"StackExchange.Redis": "1.2.0"
},
"frameworks": {
"netstandard1.4": {
"imports": "netcoreapp1.0"
}
}
}
I can add my nuget package to the windows application and build it with no error, but when I want to run the application I'm getting all erros like:
Could not load file or assembly 'System.Diagnostics.DiagnosticSource,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
i'm using .net 4.6.1 so I believe it's .net core that is nagging
i can check in here and confirm that it's a .net core library.
I also setup the .net library in the production server. but still i'm getting the same error.
I tried all possible combination and version that i thought it would be logical. and have no clue how to solve it.
Im not sure why you say you are using net 4.6.1. Because you are using netstandard1.4 as framework, looking at your project.json file.
Try the following project.json file:
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"StackExchange.Redis": "1.2.0"
},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
}
}
}
Make sure the production server has the runtime installed and Microsoft.NETCore.App-1.1.0

Build .Net Core as an EXE not a DLL

I want to build a .NET Core project as a EXE and not a DLL so it can be executed.
The answer here did not work: How to run a .Net Core dll?
Here is the sample code:
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Here is my project.json:
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
}
}
I'm currently using VSCode, whenever I build the project with the build task, or run dotnet restore I just get a .dll in my bin/Debug folder.
How do you build a .NET Core application as an exe?
Bonus: I do, will that run on a mac or other device?
I think most people got to this page because they picked .net core and they can't get an executable .exe file from their VS 2017/VS 2019 build. vs 2015 always used to make a .exe file for a console application. With vs 2017/vs 2019 when you create the project you get 2 choices for a Console application. One is Console App (.NET Core) and the other choice is Console App (.NET Framework). If you pick the .NET Core option you are going to have to move heaven and earth to get a .exe file from your Build. The (.NET Core) option creates a .dll file from a Build. If you pick the (.NET Framework) option it will build a xxxx.exe executable for you by default.
To produce an EXE instead of a DLL, you need a self-contained deployment. What you are currently doing is a framework-dependent deployment.
To convert yours to self-contained, take the following steps in your project.json file.
Remove "type": "platform".
Add a "runtimes" section for the operating systems your app supports.
When you build, pass in the target operating system. E.g. dotnet build -r osx.10.10-x64.
This is the resultant project.json
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
},
"runtimes": {
"win10-x64": {},
"osx.10.10-x64": {}
}
}
See also: https://learn.microsoft.com/en-us/dotnet/articles/core/deploying/#self-contained-deployments-scd

Why does my .Net Core console application have the wrong build target? [duplicate]

Every time I build a project using the new .NET Core RC2 templates I am not provided with a runnable .EXE file. If I hit F5 for debugging my console application it runs fine through the
C:\Program Files\dotnet\dotnet.exe
application. And if I use the
dotnet run
command in the folder, it runs fine as well. But I see no way to run the application without the .NET Core CLI tools.
The contents of my
bin\Debug\netcoreapp1.0\
folder looks like this:
As you can see there is no .EXE file available. Just the dll.
Am I overlooking something? Or is there something wrong with my project.json file?
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Thanks!
There are actually 2 app models in .NET Core:
Portable apps: heavily inspired by "DNX console apps", these apps don't produce .exe files and are instead executed by the .NET Core shared runtime (whose version is defined by the Microsoft.NETCore.App package, thanks to its special type: platform attribute). The corresponding .NET Core runtime must be installed on the machine to be able to use portable apps. If the exact version cannot be found, an exception is thrown when running dotnet run.
Standalone apps: standalone apps are really similar to good old .NET console apps as they produce .exe files. The .NET Core runtime doesn't have to be installed on the machine, because it is directly embedded with the application itself.
You're currently using the first model. To use the standalone model, you need to tweak your project.json:
Add a runtimes section to list the environments your app will target (e.g win7-x64 or ubuntu.14.04-x64). You can find the complete list here.
Remove the Microsoft.NETCore.App dependency. You can replace it by this package instead: "NETStandard.Library": "1.5.0-rc2-24027".
Here's an example of a standalone app:
{
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"net451": { },
"netcoreapp1.0": {
"dependencies": {
"System.Net.Ping": "4.0.0-rc2-24027"
},
"imports": [
"dnxcore50",
"dotnet5.6",
"portable-net451+win8"
]
}
},
"runtimes": {
"win7-x64": { }
}
}
The answer is in the documentation with complete steps now.
You can create two types of deployments for .NET Core applications:
Framework-dependent deployment
Self-contained deployment
For a runnable .EXE file, the Publish self-contained should be used.
To create a runnable application from a .NET Core console application you can use the dotnet tool. Just run in your project directory:
dotnet publish --runtime win7-x64
This creates a standalone app (self-contained deployment; includes all necessary libraries consuming at least 60MB on your disk). Of course you can also choose other runtimes, like osx.10.11-x64 or ubuntu.16.04-x64.
If you used the default configuration (New Project -> Console App (.NET Core)), there is no modification of any configuration file necessary.
step 1: remove "type": "platform", from Project.json under frameworks section
step 2: add run time section to your project.json. Note each section is separeted by a comma. Add your runtime. below is just an example for win 10.
"runtimes": {
"win10-x64": {}
}
Step 3: dotnet restore command on your project. ( open cmd, go to your project folder wherever src folder is there, run dotnet restor)
step 4: dotnet pack
step 4: dotnet build -r win10-x64 - or just build.
Step 5: you can notice .exe created under debug/netcore/win10/
In ASP.NET Core try changing your app type to default, in project.json:
"Microsoft.NETCore.App": {
"type": "default",
"version": "1.0.0-*"
}

How to run a .Net Core dll?

I've build my console application using dnu build command on my Mac. The output is MyApp.dll.
As it is not MyApp.exe, how can I execute it on windows, or even on Mac?
The code is:
using System;
class Program
{
public static void Main()
{
Console.WriteLine("Hello from Mac");
}
}
Add this to your project.json file:
"compilationOptions": {
"emitEntryPoint": true
},
It will generate the MyApp.exe on Windows (in bin/Debug) or the executable files on other platforms.
Edit: 30/01/2017
It is not enough anymore. You now have the possibility between Framework-dependent deployment and Self-contained deployment as described here.
Short form:
Framework-dependent deployment (.net core is present on the target system)
Run the dll with the dotnet command line utility dotnet MyApp.dll
Self-contained deployment (all components including .net core runtime are included in application)
Remove "type": "platform" from project.json
Add runtimes section to project.json
Build with target operating system dotnet build -r win7-x64
Run generated MyApp.exe
project.json file:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1"
}
}
}
},
"imports": "dnxcore50",
"runtimes": { "win7-x64": {} }
}
You can use dotnet publish to generate .exe output for your console app.
More details: Publish .NET Core apps with the CLI

Categories