I'm trying to run ASP.NET Core (.NET 4.6.1) application as self hosting by means of weblistener
I've add a command:
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
},
and necessary dependencies in project.json
When I start my application via Visual Studio it's works fine
src\ProjectName\bin\Release\net461\win7-x64\ProjectName.exe
ProjectName.exe starts as web server with Hosting environment: Development
But when I run it by hand ProjectName.exe runs with Hosting environment: Production and it's not working properly.
First of all I want find way to run application by hand with Development environment
In VS I have this option:
When you start the application Visual Studio will automatically set the environment variables for you (based on settings from launchSettings.json in the properties folder).
When you run from commandline or double-click, you need to set the environment variable yourself using setx ASPNETCORE_ENVIRONMENT "Development" (Commandline) or $Env:ASPNETCORE_ENVIRONMENT = "Development" (Powershell).
Related
Normally I have hosted my Asp.Net Application from Visual Studio using IISExpress profile by just starting debug.
How can I host the same Asp.Net project from the command Prompt, so that the hosted application was active and use appsettings.Development.json configuration?
You can chop and hack, and suffer great pains. You CAN get IIS Express to work, but this begs the quesiton why suffer so much?
Just get a copy of IIS and install it. You get the FULL configuration, and that is a FANTASTIC learning experience if you actually THEN have to setup a running web server.
Really, but really, do yourself a BIG nice favor:
Just install the full version of IIS - that's what it is for, and messing around and playing with IIS express while possible? it just a lot of pain and hassle, and the WHOLE HUGE provided GUI you get/have with the full edition of IIS allows you not only with ease to "configure" IIS, but also get a good feel l for setup and running IIS.
I kind of wish like sql server vs sql server express? You can download the SSMS (sql server management studio and use SSMS for both SQL server, and SQL server express. Then comes the day you have to do some work on a server will SQL server, and you not skip a beat).
The same applies to IIS express. (but, the IIS management system, tools and GUI to manage IIS is not a separate download. I would simple install IIS on that computer if you want to run/test/have/enjoy a running web server. You can as noted, mess around with IIS Express, and mess around with config files, but really, it not worth the time as compared to installing IIS full edition.
Save world poverty, save your suffering, save your time, and invest a bit of time to use IIS, and you not only find overall it less pain, but now will have learned and adopted a new skill set, and will become comfortable with running IIS as a result.
Visual Studio uses launchSettings.json file which allows you to specify different launch profiles for your app: IIS Express, Project, Docker, etc.
You can select which profile to use in the Visual Studio menu just near the start debug button:
For each profile, you can specify various startup settings including environment variables:
{
"profiles": {
"EnvironmentsSample": {
"commandName": "Project", // launches Kestrel web server
"launchBrowser": false,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" // specifies environment
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:80"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
If you want to start the application manually from the command prompt:
dotnet run --environment Development
By default, ASP.NET Core reads configuration from appsettings.{environment}.json file. So appsettings.Development.json file will be used.
I have a .net 5.0 web api application. I want to publish build it to run it on a windows server 2019.
I build it locally in commandline using "dotnet run --launch-profile CMDLINE"
CMDLINE is one of my profile in my launchSettings.json
I then try to publish my app with the following command.
I then try "dotnet application.dll"
in an attempt to run the dll localy.
the application seems to be started but when I try to access my public endpoint (returning a string with tag [Anonymous] doesn't require authentication, then I get the error 500 in postman
My project is always compiling in /debug/net5.0/ or in /debug/net5.0/publish/
Am I missing an essential step please help. I basically want to give my build/released application to my system admin, which is going to publish the API on a windows server 2019.
He just told me that running "dotnet application.dll" is not working as expected.
(never publish an app on a custom server like this before, always used online hosting.)
I finally noticed my hosting environment wasn't the same as my specified ASPNETCORE_ENVIRONMENT variable in my launchsettings.json!
I simply specified my hosting environment in commandline with "set ASPNETCORE_ENVIRONMENT=Staging" and built it up with "dotnet application.dll --launch-profile CMDLINE" and it worked. "dotnet application.dll" is also working.
Is the launchsettings.json meant to be used in production or is it only for development purposes?
The one that is created by default has ASPNETCORE_ENVIRONMENT set to Development and also a localhost applicationUrl. Am I suppose to create separate production/staging profile or is this more of a development tool?
The launchSettings.json file is only used by Visual Studio during debugging and when running the app via dotnet run command.
See the quote from the official documentation:
The launchSettings.json file:
Is only used on the local development machine.
Is not deployed.
contains profile settings.
So for everyone who is looking for the short confirmed answer:
No, it's not used in production.
I have just upgraded an app to RC2 and am testing all the hosting scenarios. I'm having trouble figuring out the best practice of how to setup the configuration of each scenario that they would all work.
Here are the scenarios:
1. Local debugging with WebListener (we need Windows Authentication)
2. Local debugging with Kestrel
3. Local debugging with IIS Express (+ Kestrel)
4. Published to IIS
I'm not sure if we need all these, but you know... developers and their preferences :)
So, when you publish the app to IIS, I've set up IIS application physical path to wwwroot-folder. The appsettings.json file is outside the wwwroot so I can load the configuration file using
new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), #"..\"))
This works fine. I have also setup the local self-hosted Kestrel and WebListener working directory to wwwroot so that the same logic applies there.
launchsettings.json
"workingDirectory": "wwwroot",
The problem is now with IIS Express. With IIS Express the current directory is the application root (same where appsettings.json is) so with the current setup, it cannot find the settings file. It seems that I cannot set a working directory for IIS Express(?) and this lead me to think that what is the preferred way to setup this that it would work in all of the mentioned scenarios?
I am developing an ASP.NET 5 Web API app using Visual Studio code on Mac. I manually modified my Properties/launchSettings.json file to set environment to Staging for all profiles using ASPNET_ENV environment variable:
However, when I run dnx web in Mac terminal to start the app, I still get Production environment:
Can I use launchSettings.json to specify environment variables (and, thus, environment types) if I use Visual Studio Code on Mac? Or is it specific to full Visual Studio?
ASP.NET 5 docs suggest that launchSettings.json can be used to inject environment variables. However, this SO discussion suggests passing them through commands.
LaunchSettings.json is strictly a VS concept. In other cases, you will have to configure environment variables as commands below:
For standard command line run, use:
set ASPNET_ENV=Development
dnx web
For powershell, use:
$env:ASPNET_ENV='Development'
dnx web
Shorter version: dnx web ASPNET_ENV=Development
On a Mac, using Terminal, type:
export ASPNETCORE_ENVIRONMENT=Development && dotnet run
Requirements:
.NET Core (rc2)
.NET command line interface
Adding to #Chrysalis answer, you could also avoid "messing" with current environment by passing needed variables on command line.
Inside project.json file, say that you have a web-dev command specific for development environment:
"commands": {
"web-dev": "Microsoft.AspNet.Server.Kestrel
--ASPNET_ENV Development --Hosting:Environment Development
--config hosting.Development.json",
},
where you can see how both ASPNET_ENV, Hosting:Environment are set, as well as invoking a specific hosting.json configuration.
NOTE: command is split on several lines just for readability, join again before actually pasting in JSON file.