-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathProgram.cs
More file actions
146 lines (125 loc) · 5.29 KB
/
Program.cs
File metadata and controls
146 lines (125 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// <copyright file="Program.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc.
// </copyright>
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Datadog.Demos.Util;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Hosting;
namespace Samples.Website_AspNetCore01
{
public class Program
{
public static async Task Main(string[] args)
{
var appDuration = new Stopwatch();
appDuration.Start();
WriteLine();
WriteLine($"Starting at {DateTime.UtcNow}");
WriteLine(" ==> Will NOT manually call profiler engine entry point.");
EnvironmentInfo.PrintDescriptionToConsole();
var hasTimeout = TryExtractTimeoutFromCommandLineArguments(args, out TimeSpan timeout);
if (hasTimeout)
{
WriteLine($"The application will run non-interactively for {timeout} and will stop after that time.");
}
else
{
timeout = Timeout.InfiniteTimeSpan;
WriteLine("The application will run interactively because no timeout was specified or could be parsed.");
}
var sw = new Stopwatch();
sw.Start();
using (IHost host = CreateHostBuilder(args).Build())
{
sw.Stop();
WriteLine($"host built in {sw.ElapsedMilliseconds} ms");
sw.Restart();
var cts = new CancellationTokenSource();
using (var selfInvoker = new SelfInvoker(cts.Token))
{
sw.Stop();
WriteLine($"SelfInvoker built in {sw.ElapsedMilliseconds} ms");
sw.Restart();
await host.StartAsync();
sw.Stop();
WriteLine($"host started in {sw.ElapsedMilliseconds} ms");
// Read the address Kestrel actually bound (after StartAsync). Configuration["Urls"]
// may be "http://127.0.0.1:0" when the test runner asks for a dynamic port, so the
// bound address is the only reliable source of the real listening URL.
var rootUrl = GetBoundRootUrl(host);
WriteLine($"Listening to {rootUrl}");
WriteLine();
WriteLine($"Started at {DateTime.UtcNow}.");
Task selfInvokerTask = selfInvoker.RunAsync(rootUrl);
if (hasTimeout)
{
Thread.Sleep(timeout);
}
else
{
WriteLine();
WriteLine("Press enter to finish.");
Console.ReadLine();
}
WriteLine($"Stopping... ");
cts.Cancel();
await selfInvokerTask;
await host.StopAsync();
}
}
appDuration.Stop();
WriteLine($"The application run {appDuration.Elapsed} and exited {DateTime.UtcNow}");
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
private static string GetBoundRootUrl(IHost host)
{
var server = (IServer)host.Services.GetService(typeof(IServer));
var address = server?.Features.Get<IServerAddressesFeature>()?.Addresses.FirstOrDefault();
return string.IsNullOrEmpty(address) ? "http://localhost:5000" : address.TrimEnd('/');
}
// Helper method to output both to the console (when the app runs in the console)
// and via Trace to see them while running under IISExpress both in Visual Studio
// or with SysInternals DebugView (https://docs.microsoft.com/en-us/sysinternals/downloads/debugview)
private static void WriteLine(string line = null)
{
if (line == null)
{
Trace.WriteLine(string.Empty);
Console.WriteLine();
}
else
{
Trace.WriteLine($" ########### {line}");
Console.WriteLine($" ########### {line}");
}
}
private static bool TryExtractTimeoutFromCommandLineArguments(string[] args, out TimeSpan timeout)
{
if (args.Length > 0)
{
if (args.Length > 1 && "--timeout".Equals(args[0]))
{
if (int.TryParse(args[1], out var timeoutInSecond))
{
timeout = TimeSpan.FromSeconds(timeoutInSecond);
return true;
}
}
}
timeout = TimeSpan.MinValue;
return false;
}
}
}