forked from OmniSharp/omnisharp-roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost.cs
More file actions
88 lines (79 loc) · 3.16 KB
/
Host.cs
File metadata and controls
88 lines (79 loc) · 3.16 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OmniSharp.Eventing;
using OmniSharp.Plugins;
using OmniSharp.Services;
using OmniSharp.Utilities;
namespace OmniSharp.Http
{
internal class Host
{
private readonly IOmniSharpEnvironment _environment;
private readonly ISharedTextWriter _sharedTextWriter;
private readonly IEnumerable<string> _commandLinePlugins;
private readonly int _serverPort;
private readonly string _serverInterface;
public Host(
IOmniSharpEnvironment environment,
ISharedTextWriter sharedTextWriter,
IEnumerable<string> commandLinePlugins,
int serverPort,
string serverInterface)
{
_environment = environment;
_sharedTextWriter = sharedTextWriter;
_commandLinePlugins = commandLinePlugins;
_serverPort = serverPort;
_serverInterface = serverInterface;
}
public void Start()
{
var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.AddCommandLine(new[] { "--server.urls", $"http://{_serverInterface}:{_serverPort}" });
var builder = new WebHostBuilder()
.UseKestrel()
.ConfigureServices(serviceCollection =>
{
serviceCollection.AddSingleton(_environment);
serviceCollection.AddSingleton(_sharedTextWriter);
serviceCollection.AddSingleton(NullEventEmitter.Instance);
serviceCollection.AddSingleton(_commandLinePlugins);
serviceCollection.AddSingleton(new HttpEnvironment { Port = _serverPort });
})
.UseUrls($"http://{_serverInterface}:{_serverPort}")
.UseConfiguration(config.Build())
.UseEnvironment("OmniSharp")
.UseStartup(typeof(Startup));
using (var app = builder.Build())
{
app.Start();
var appLifeTime = app.Services.GetRequiredService<IApplicationLifetime>();
Console.CancelKeyPress += (sender, e) =>
{
appLifeTime.StopApplication();
e.Cancel = true;
};
if (_environment.HostProcessId != -1)
{
try
{
var hostProcess = Process.GetProcessById(_environment.HostProcessId);
hostProcess.EnableRaisingEvents = true;
hostProcess.OnExit(() => appLifeTime.StopApplication());
}
catch
{
// If the process dies before we get here then request shutdown
// immediately
appLifeTime.StopApplication();
}
}
appLifeTime.ApplicationStopping.WaitHandle.WaitOne();
}
}
}
}