Skip to content

Commit db8a521

Browse files
committed
URLs in a hosted service example
1 parent bbaf740 commit db8a521

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.AspNetCore.Hosting.Server;
2+
using Microsoft.AspNetCore.Hosting.Server.Features;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
builder.Services.AddHostedService<TestHostedService>();
6+
var app = builder.Build();
7+
8+
app.MapGet("/", () => "Hello World!");
9+
10+
app.Run();
11+
12+
public class TestHostedService: BackgroundService
13+
{
14+
private readonly IServiceProvider _services;
15+
private readonly IHostApplicationLifetime _lifetime;
16+
public TestHostedService(IServiceProvider services, IHostApplicationLifetime lifetime)
17+
{
18+
_services = services;
19+
_lifetime = lifetime;
20+
21+
}
22+
23+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
24+
{
25+
if (!await WaitForAppStartup(_lifetime, stoppingToken))
26+
{
27+
return;
28+
}
29+
30+
PrintAddresses(_services);
31+
32+
_lifetime.StopApplication();
33+
}
34+
35+
static async Task<bool> WaitForAppStartup(IHostApplicationLifetime lifetime, CancellationToken stoppingToken)
36+
{
37+
var startedSource = new TaskCompletionSource();
38+
var cancelledSource = new TaskCompletionSource();
39+
using var reg1 = lifetime.ApplicationStarted.Register(() => startedSource.SetResult());
40+
using var reg2 = stoppingToken.Register(() => cancelledSource.SetResult());
41+
42+
Task completedTask = await Task.WhenAny(startedSource.Task, cancelledSource.Task).ConfigureAwait(false);
43+
44+
return completedTask == startedSource.Task;
45+
}
46+
47+
void PrintAddresses(IServiceProvider services)
48+
{
49+
Console.WriteLine("Checking addresses...");
50+
var server = services.GetRequiredService<IServer>();
51+
var addressFeature = server.Features.Get<IServerAddressesFeature>();
52+
foreach(var address in addressFeature!.Addresses)
53+
{
54+
Console.WriteLine("Listing on address: " + address);
55+
}
56+
}
57+
58+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"profiles": {
3+
"urls_in_a_hosted_service": {
4+
"commandName": "Project",
5+
"dotnetRunMessages": true,
6+
"launchBrowser": true,
7+
"applicationUrl": "https://[::1]:0;http://[::1]:0",
8+
"environmentVariables": {
9+
"ASPNETCORE_ENVIRONMENT": "Development"
10+
}
11+
},
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>urls_in_a_hosted_service</RootNamespace>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30114.105
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "urls-in-a-hosted-service", "urls-in-a-hosted-service.csproj", "{D16B9DA4-DBBE-4569-A695-58E7A97CCBBE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(SolutionProperties) = preSolution
14+
HideSolutionNode = FALSE
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{D16B9DA4-DBBE-4569-A695-58E7A97CCBBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{D16B9DA4-DBBE-4569-A695-58E7A97CCBBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{D16B9DA4-DBBE-4569-A695-58E7A97CCBBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{D16B9DA4-DBBE-4569-A695-58E7A97CCBBE}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal

0 commit comments

Comments
 (0)