launchSettings.json not working after publishing Server using WebApplicationBuilder - c#

I'm having a problem, when I publish my webServer, the launchSettings.json file no longer changes the server execution settings like: URL, PORT... It only works in the Visual Studio Editor. I also didn't find a way change the same json options in C#, no docs about it... When I launch the exe, it opens with default settings.
launchSettings.json
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://0.0.0.0:43231",
"sslPort": 44364
}
},
"profiles": {
"Chapter": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://0.0.0.0:7196;http://0.0.0.0:5196",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Shipping"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Shipping"
}
}
Program.cs:
using Chapter.Contexts;
using Chapter.Repositories;
using Microsoft.AspNetCore.Hosting;
WebApplicationBuilder? builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<ChapterContext, ChapterContext>();
builder.Services.AddTransient<LivroRepository, LivroRepository>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseFileServer();
app.UseDefaultFiles();
app.UseAuthorization();
app.MapControllers();
app.Run();
Project file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Content Include="Properties\launchSettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="ViewModels\" />
<Folder Include="Repositories\" />
</ItemGroup>
</Project>

Related

Getting Database ConnectionString from appsettings.{Environement}.json in .Net Core 7 inside Program.cs?

In older versions of ASP.Net core you had the startup.cs file where you would do a lot of the work including reading and setting the application environment then based on that you could read different version of the appsettings.json file. In the new ASP.Net Core 7 they got rid of Startup.cs and greatly streamlined the program.cs files. Now I can't figure out how to read the environment and then pass Entity Framework 7 my connection string. Looking around all the answers I find don't apply to version 7 or tell you to undo all the work in 7 by remaking the entire Startup.cs file. How are we supposed to inject the connection string based off the environment in .Net 7?
I do have the code to read from the base appsettings.json file and that works, there is also a section to read the environment but it get setup AFTER the DbContext injection. Here is my program.cs file and I am just lost on what needs to be updated. I looked at the microsoft docs but did not see anything applying to environements and injection of the string.
var builder = WebApplication.CreateBuilder(args);
//Add Services (builder.Services.AddScoped<IService, Service>();
builder.Services.AddScoped<INavigationHelper, NavigationHelper>();
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme).AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
builder.Services.AddRazorPages().AddMicrosoftIdentityUI();
builder.Services.AddDbContext<SiteDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("Database")));
var app = builder.Build();
if (!app.Environment.IsDevelopment()) { app.UseHsts(); }
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
app.Run();
appsettings.json:
{
"AzureAd":
{
"Instance": "https://login.microsoftonline.com/",
"Domain": "",
"TenantId": "",
"ClientId": "",
"CallbackPath": "",
"ClientSecret": "Client secret from app-registration. Check user secrets/azure portal.",
"ClientCertificates":
[
]
},
"Logging":
{
"LogLevel":
{
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings":
{
"Database": ""
}
}
appsettings.development.json
{
"Logging":
{
"LogLevel":
{
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"ConnectionStrings":
{
"Database": ""
}
}
}
builder.Configuration.GetConnectionString("Database")
will use the appropriate appsettings.json file based on the chosen environment. If you look in Properties folder in your project and examine the launchsettings.json file you should see something like this:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:30787",
"sslPort": 44345
}
},
"profiles": {
"Cosmos.Application.Server": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7138;http://localhost:5138",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
In the above, this means that the ASPNETCORE_ENVIRONMENT is set to Development and this will determine which appsettings file is used in your program.cs when calling:
builder.Configuration ...
We can read configs from environments like below. Keys within latest added file will get the priority when reading the values.
For example if "Default" connection string was present in both appsettings.json and appsettings.Development.json, then the value in latter file will be the resulting value.
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.Build();
var defaultConnectionString = config.GetConnectionString("Default");
Console.WriteLine($"Default ConnectionString: {defaultConnectionString}");

Weird redirection problem with ASP.NET Core 6 Razor pages application

When I run the application application redirects to the /dashboard page instead of /index page.
It's weird because that application don't a page with that name and no redirection code to that page. I checked index.cshtml, index.cshtml.cs, _layout.cshtml, program.cs and also searched solution wide for page name dashboard.
But it's also weird and confusing another project in another solution has this redirection.
How this can be? Am I missing some point? That is very annoying.
project 1 launchsettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:39662",
"sslPort": 44359
}
},
"profiles": {
"Yoneticim.EmailProviders.Web": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7179;http://localhost:5179",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
project 2 launchsettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:39662",
"sslPort": 44359
}
},
"profiles": {
"Fabrikafa.CC.Web": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7178;http://localhost:5178",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Here is the related part of startup.cs
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
Any help appreciated. Thanks in advance.

Web-app hot-reload when running by IIS Express with SSL

Created project by Micosoft Visual Studio "ASP.NET with React+Redux" template. In that case app running by IIS Express. But hot-reload for react-app doesn't work and shows error in console: "WebSocket connection to 'wss://localhost:54691/ws' failed:"
After some research, I found a solution: disable SSL in IIS settings. But how can I achieve using hot reload when SSL is enabled?
package.json dependencies:
"dependencies": {
"#emotion/react": "^11.8.2",
"#emotion/styled": "^11.8.1",
"#mui/material": "^5.5.2",
"#reduxjs/toolkit": "^1.8.1",
"history": "4.10.1",
"node-sass": "^6.0.1",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "5.0.0",
"react-redux": "~7.2.6",
"react-router-dom": "6.2.1",
"redux": "4.1.2",
"redux-thunk": "2.4.1",
"typescript": "4.4.4"
},
launchSettings.json generated by template (SSL enabled)
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:48763",
"sslPort": 44373
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"MyProject": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
error message:
WebSocketClient.js:16 WebSocket connection to 'wss://localhost:54691/ws' failed:
WebSocketClient # WebSocketClient.js:16
initSocket # socket.js:24
./node_modules/webpack-dev-server/client/index.js?protocol=ws%3A&hostname=0.0.0.0&port=54691&pathname=%2Fws&logging=none&reconnect=10 # index.js:273
options.factory # react refresh:6
__webpack_require__

HTTP Error 500.30 - ANCM In-Process Start Failure - Production Environment

I was configuring my application for deployment. The first step I did was changing the environment from Development to Production and stumbled in this error. I noticed that I can launch my application only in the Development Environment. When I change them to Staging, Production I get the mentioned error. Also, I tried the solutions from this post, and still no success.
Here are the related configurations:
launchSettings.json
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64845",
"sslPort": 44375
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"BingoAPI": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
xx.csproj
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

VS2017 Debug: no Launch URL setting

Project (full net461, core 1.1) converted from VS2015:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:17901/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApplication1": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Also no browsers opens automatically after F5. (But site opens when I go to url manually).
And I also cannot create not Core web project in 2017 to see how it works - VS just writes something about "failed to create project" in bottom line and empty solution explorer, only folder created, nothing in Output, nothing in devenv /Log C:/log.xml log.
To fix: enable "Azure App service Tools"

Categories