ASP.NET Core publishing to Azure (Staging) - asp.net-core

VS 2019
ASP.NET Core 3.1
I have developed a Web App locally and now I am ready to deploy to an Azure Staging environment.
My Web App was originally .Net (not Core) and I had not problems deploying it.
How to I tell the deployment process to use the "Staging" environment?
My launchSettings.json contains the following:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59000",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
I have a appSettings.Staging.json pointing to the Staging database...
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning"
}
},
"ConnectionStrings": {
"DbConnection": "Data Source=myapp.database.windows.net;Initial Catalog=MyAppCoreStaging;user id=myappstepadmin;password=mypassword;MultipleActiveResultSets=True"
}
}
But I am not sure how to tell it to use Staging when I deploy.
At the moment when I deploy, the browser starts up on the page and I get:
HTTP Error 500.30 - ANCM In-Process Start Failure
Common solutions to this issue:
The application failed to start
The application started but then stopped
The application started but threw an exception during startup
Troubleshooting steps:
Check the system event log for error messages
Enable logging the application process' stdout messages
Attach a debugger to the application process and inspect
For more information visit: https://go.microsoft.com/fwlink/?LinkID=2028265
Is there something I need to configure on Azure to use the Staging?

Since you are deploying to Azure and didn't specify that you are using a CI/CD Pipeline as your method of publishing, I assume that you are using the publishing profiles provided from Azure portal directly in Visual Studio.
In the Publish dialog, click on Edit -> settings -> Configuration and select Stage
In your Program.cs, your CreateWebHostBuilder (assuming you are using ASP.NET Core 3.0+; it's also possible for 2.2 but it's not IWebHostBuilder), you can specify that the appsettings file should be dependent on your solution configuration:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseEnvironment(Environment);
where Environment can be a property with preprocessor directives:
public static string Environment
{
get
{
string environmentName;
#if DEBUG
environmentName = "development";
#elif STAGE
environmentName = "staging";
#elif RELEASE
environmentName = "production";
#endif
return environmentName;
}
}

If you use a build pipeline, you should look at this.
steps:
- task: DotNetCoreCLI#2
inputs:
command: 'publish'
publishWebProjects: true
arguments: '-o $(build.artifactstagingdirectory) /p:EnvironmentName=Staging'

Related

Blazor (WASM) and Web Api separate Project Debugging

I'm having problems while debugging a blazor web assembly project.
Setup:
WASM Blazor UI project
Web API project which reference the WASM Project
They both share the same port.
I Only launch the Web API project and also get access to the blazor site. (which is odd to me)
I have those 2 line in my Web Api Project.
app.UseWebAssemblyDebugging();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
Launch Settings Web API:
"API": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Launch Settings Blazor WASM:
"Lab.Web": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
When I try to put a breakpoint somewhere in my Blazor project. I get this message:
"The breakpoint will not currently be hit. No symbols have been loaded for this document."
I don't get this message if I run the Blazor project alone but then I don't have access to my Web API.
What is the correct way to share same port for Web API and Blazor WASM while being able to debug the WASM project??
ps: If this helps I'm using VS Studio 16.9.3.
Many thanks for your help!
I'm assuming:
You're running the project in AspNetCore and not IIS.
You have the API Server project as your startup project.
You are in Debug mode.
Your setup is like the Web Assembly hosted on ASPNetCore template.
The only place you need inspectUri is in LaunchSettings.json is in the API project, which is where you don't have it.
When starting up in debug mode it can be slooooooow to get going, so give it time.

Creating an empty asp.net core project in vscode

In visual studio we can create an empty asp.net core project. Do we have anything like that in visual studio code, or we only have to create an asp.net core MVC with this command : dotnet new mvc -o MyProjectName ?
In this doc about dotnet new command, it lists the templates that come pre-installed with the .NET SDK.
And we can run dotnet new --list or dotnet new -l to see a list of all installed templates.
I got the answer. We have to use : dotnet new web instead of dotnet new mvc. For more information we can write dotnet new in the command. This Show us more CLI and help to know how to create projects in visual studio code.
Here is an alternative you can try. I know of no advantage for doing this except for the fun of it. One possible advantage is that we can customize anything during this process if we know what we need to do for the customization.
Create a folder for the project, of course. In it create a csproj file with the following; change the TargetFramework as appropriate.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Create an appsettings.json file with the following (note the AllowedHosts).
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Create an appsettings.Development.json file with the following (note there is no AllowedHosts). VS Code however will not allow use of the name appsettings.Development.json. A workaround is to (in VS Code) copy and paste the appsettings.jsonfile then rename that. Be sure to remove the AllowedHosts.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Outside of VS Code, create a Properties folder (VS Code says that Properties is not a valid folder name). Add a launchSettings.json file to the folder with the following. The ports can be customized.
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:28210",
"sslPort": 44341
}
},
"profiles": {
"AspNetCoreEmpty": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7126;http://localhost:5126",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Create a Program.cs with the following.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
Go to the project directory in a terminal window and issue:
dotnet build
It should build. The project will essentially be the same as if it was built using dotnet new web.

dotnet core excluding tests project from building in launchSettings.json

In JetBrains Rider, we can either manually create a profile to run a .NET Core Web app or use launchSettings.json file if it is included in the project. However, when I manually create a profile it does not build the tests projects but when I use the launchSettings.json it tries to build the tests projects (it's probably just running dotnet build without any filters). I am wondering what do I need to add to my launchSettings.json to exclude building the tests projects. Thank you
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:20169",
"sslPort": 44329
}
},
"profiles": {
"Web": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Localhost"
}
}
}
}
Unfortunately, that's just a bug in Rider: https://youtrack.jetbrains.com/issue/RIDER-23780
It should work as you describe (e.g. it should only build the projects required for the program to run if you click the "Run" button), but it doesn't work for Launch Settings yet.
The workaround is to either create a ".NET Project" run configuration instead of the "Launch Settings" one, or to manually exclude the projects from build.

Removing Kestrel binding warning

When using Kestrel in an ASP.NET Core 2.1 project and specifying a binding in UseKestrel(), a message is logged at warning level:
Overriding address(es) 'http://localhost:50000/'. Binding to endpoints defined in UseKestrel() instead.
This adds noise to our logs, reports a default (and therefore confusing) URL, and shouldn't be a warning. Short of filtering the message by the logger itself, is there a way to configure the web host builder so it doesn't log this on startup?
This is an old question but you can resolve that conflict between launchSettings.json and appSettings.json with "externalUrlConfiguration": true
Kestrel configuration in appSettings.json:
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http1AndHttp2"
},
"Endpoints": {
"HTTPS": {
"Url": "https://localhost:4433"
}
}
And this is launchSettings.json content:
{
"profiles": {
"TestApplication": {
"commandName": "Project",
"launchBrowser": true,
"externalUrlConfiguration": true, //add this line
"applicationUrl": "https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
First of all, that “noise” is not really that noise if you consider that you only get this once when the application starts. So unless you are doing something weird where you need to restart your app, then you will probably almost never see that line in your logs, compared to all those other (much noisier) messages.
That being said, it is actually a useful warning because it tells you that you have configured the binding URL in multiple locations. So the proper action is not to ignore that message but to actually remove the duplicate configurations.
In this case, you are using explicit listen options with UseKestrel() so that trumps everything. So you should remove the other configurations. There are a few locations where you should look:
ASPNETCORE_URLS environment variable.
Legacy ASPNETCORE_SERVER.URLS environment variable.
Properties/launchSettings.json.
Had the same slightly annoying warning and checked the environment variables and configuration files, but was unable to locate the source of the url list. In the end I used IWebHostBuilder.UseUrls() with an empty list of urls to get rid of the warning.
IWebHostBuilder builder = new WebHostBuilder()
.UseUrls()
.UseKestrel()
.ConfigureKestrel(...
Edit: For .net6
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls();
It can also be a conflict between the configuration contained in launchsettings.json and appsetings.json
In my case, while developing locally, the Kestrel entry in appsetings.json:
"kestrel": {
"endpoints": {
"http": {
"url": "http://localhost:5000"
},
"https": {
"url": "https://localhost:5001"
}
}
was conflicting with a profile in the launchsettings.json:
"MyProject-Dev": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000;https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
I believe the right place for this is in the launchsettings.json file, so removing the kestrel entry in the appsettings.json solved my problem.
None of the the previous answers worked for me. I defined my urls in builder.WebHost.UseKestrel(async options => )
I removed the following from appsettings.json and then the warning was gone.
"AllowedHosts": "*"
If can help someone, I was looking for something for my webapi in .NET 6
What solved for me was using the code below in launch.json
"env": {
"ASPNETCORE_URLS": "http://localhost:5050"
}

ASP.NET Core Default debugging launch URL

When using the ASP.NET Core Web API template, the default debugging start up URL is somehow set to api/values. Where is this default configured and how do I change it?
There was very little documentation that I could find regarding where this start up URL was declared. There is a brief mention of it in this blog post on MSDN. I eventually stumbled upon it in the launchSettings.json file under the Properties node of the project as shown below:
Here are the contents of this file:
{
"profiles": {
"IIS Express": {
"commandName" : "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables" : {
"ASPNET_ENV": "Development"
}
}
}
}
You can change the launchURL to your desired default route.