.net 7 Unable to connect to web server 'Local'. The web server is no longer running - visual-studio-2022

There is a lot of info on this problem, but none of them match what I'm seeing.
Here is the relevant info:
Visual Studio 2022, net 7
I'm working on setting up specific profiles that match our pre-existing environments. I believe I have a fundamental misunderstanding of "launchsettings.json" perhaps. Something is going wrong and I have been unable to figure it out.
Launch settings:
{
"profiles": {
"Local": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Local"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:9059;"
}
//"Green": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "Green"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//},
//"Yellow": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "Yellow"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//},
//"YellowVariant": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "YellowVariant"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//},
//"Umber": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "Umber"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//},
//"White": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "White"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5709",
"sslPort": 44372
}
}
}
You'll see the following environments: Local, Green, Yellow, YellowVariant, Umber, White (local, test server, qa, alternate qa, uat, prod)
They are all set as commandName: "Project" because - that's what they were before under the "Development" setting. My understanding is this uses Kestrel instead of IIS Express.
It does load when I change commandName to "IISExpress" but loads as SSL and we do not currently use SSL for our service layer. Though the browser screen sits there on a blank screen and nothing ever loads.
Here are the steps I followed (taken from another stack overflow article):
>Clean and Rebuild Projects dependencies
>Clean and Rebuild the primary project
>Verify launchsettings and project build properties
>Close VS
>Clear bin and obj folders from the project
>Go to C:\Users\username\Documents\IISExpress\config and delete all files from this folder
>Remove the.VS folder from the solution directory
>Change port numbers (tried many different, but none of these are in use according to netstat/netsh)
>Reboot
It continues to occur. As you can see, I have everything commented out except local, to reduce complexity. If I change it back to "Development" it does not work either.
I appreciate any assistance. We're moving from net45 to net7 and the confusion is extensive. So there are quite possibly fundamentals I'm not understanding here.
I did read through https://dotnettutorials.net/lesson/asp-net-core-launchsettings-json-file/ to see if there was anything I could glean and nothing really seemed to help me solve this issue.
Thank you!

Related

VSCode webapi default implementation does not run Swagger interface by default, 404 error occurs

When installing and running a new webapi template in Visual Studio Code:
dotnet new webapi -n weatherapi
the framework is set up to default to launch the Swagger interface, but subsequently executing dotnet run or launching with F5 will attempt to browse to the applicationUrl property specified in launchSettings.json. This subsequently throws a 404 error since this is not the Swagger definition url.
Also within launchSettings.json, launchBrowser defaults to true, and launchUrl defaults to swagger.
According to the documentation, the first entry in the profiles section of launchSettings.json will be the profile used unless specified otherwise in launch.json (which it is not).
So the question is, why does the default implementation throw a 404 when it looks like it should be set up by default to land on the Swagger interface?
The out-of-the-box launchSettings.json implementation is:
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:21895",
"sslPort": 44320
}
},
"profiles": {
"weatherapi": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7205;http://localhost:5111",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

How to add HTTP endpoint to ASP.NET Core 6 app?

I created an ASP.NET Core 6 app with "Configure for HTTPS" checked during project creation. For testing purposes I now need a plain HTTP endpoint. How can I add such an endpoint?
My code currently looks like this:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World! - use /graphql");
app.Run();
Serhii and Serge pointed me to the launchSettings.json (note that it's inside the Properties folder!). That's how it looks like:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:20380",
"sslPort": 44329
}
},
"profiles": {
"TestProject": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7090;http://localhost:5090",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Note the line
"applicationUrl": "https://localhost:7090;http://localhost:5090",
and note that the second URL is plain HTTP. I could call this URL without any issues (started project normally using F5, HTTPS link is called automatically, but one can just switch manually to the HTTP link in the browser).
So, nothing to do for me, no configuration change was needed etc :-)

Possibility to debug both HTTP and HTTPS in Kestrel and .NET Core like in .Net 4.x under IIS

I need to debug external authentication and it requires HTTPS. At the same time for most internal requests http is enough. There was no problem to listen on 80 and 443 ports when hosting my web app on IIS, but with ASP.NET Core hosted by Kesterl as I see it, the port is strictly bind to specific configuration in launchSettings.json such as:
"localCalls": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:40000"
},
"externalIdentity": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:50000"
}
I'm curiuse, is it possible to have listeners on both ports at a time without necessarity to relaunch in the other configuration to change the protocol.
According to the documentation you can define the endpoints within the same configuration:
"localCalls": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:5000;https://localhost:5100"
}
}
Asp.Net Core 2.1 and onwards makes it super easy to use local SSL. When you create the project using Visual Studio, it asks you if you need to enable the SSL or not. If you selected that option before creating the project, you should see your launchSettings.json file something like below:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61110",
"sslPort": 44377
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"YourProjectName": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
The iisSettings section is for IISExpress with an sslPort defined i.e. 44377 in this case. So when your project runs under IISExpress, it uses that settings
The YourProjectName section is for Kestrel. You can see that applicationUrl is using both http and https endpoints. So when you do dotnet run you should see
Hosting environment: Development
Content root path: C:\users\...\YourProjectName
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
In addition, under Configure method, you should see a line below, so it will automatically redirect HTTP to HTTPS
app.UseHttpsRedirection();
If your launchSettings.json file doesn't look like the above. Try changing it in the project properties and enable the SSL, like in the screenshot below. When you save the settings, the launchSettings.json file will be updated
If the above doesn't work, try changing the launchSettings.json file manually. I hope that helps

After set ASPNETCORE_ENVIRONMENT Production in Startup file IHostingEnvironment environment not getting updated

I am using ASP.NET Core WebSite which targets the .NET Framework 4.6.1 and what all way i tried to update Environment.
1.Updated in Project properties Debug ASPNETCORE_ENVIRONMENT value as Production
2.Manually setting in launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "http://localhost/MyApp",
"sslPort": 0
},
"iisExpress": {
"applicationUrl": "http://localhost:53996/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "Executable",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"MyApp": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "http://localhost:53997/"
}
}
}
3.Windows Environment Variable
4.Setting through command prompt where csproj file is present.
using the command set ASPNETCORE_ENVIRONMENT=PRODUCTION
but after setting Production in above configuration setps still Enviroment is coming Development in Startup.cs file IHostingEnvironment.
I don't know i'm new Any other configuration i need to configure.
Any help.
Edit:
1.After Setting Windows Environment Variable under system level.
2.And the After while Restarting Computer System,Project is taking Environment Setting value whether it Production or Development.

Windows auth in aspnet core is unpredictable using IISExpress or HttpSys

In my asp.net core web app I need to find a login name for the user making a request. This is done in the middleware using a name claim from HttpContext.
public static string GetUserName(HttpContext ctx)
{
var claim = ctx.User.FindFirst(ClaimTypes.NameIdentifier);
if (claim != null)
return claim.Value;
claim = ctx.User.FindFirst(ClaimTypes.Name);
var arr = claim?.Value.Split('\\');
return arr?[arr.Length - 1];
}
But sometimes Context.User.UserClaims is empty. It looks like this mostly happens when multiple requests come almost at the same time. For some of those requests claims are empty and for some they are not.
It never happens to me under IIS but does happen under IISExpress and HttpSys. I hope someone has an idea why it behaves this way.
Here's my launch.json
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"use64Bit": true
},
"HttpSys": {
"commandName": "Project",
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}