-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathDebugAdapterServerServiceCollectionExtensions.cs
109 lines (95 loc) · 5.17 KB
/
DebugAdapterServerServiceCollectionExtensions.cs
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
using System;
using System.Linq;
using DryIoc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;
using OmniSharp.Extensions.DebugAdapter.Server.Configuration;
using OmniSharp.Extensions.DebugAdapter.Shared;
using OmniSharp.Extensions.JsonRpc;
namespace OmniSharp.Extensions.DebugAdapter.Server
{
public static class DebugAdapterServerServiceCollectionExtensions
{
internal static IContainer AddDebugAdapterServerInternals(this IContainer container, DebugAdapterServerOptions options, IServiceProvider? outerServiceProvider)
{
container = container.AddDebugAdapterProtocolInternals(options);
if (options.OnUnhandledException != null)
{
container.RegisterInstance(options.OnUnhandledException);
}
else
{
container.RegisterDelegate(_ => new OnUnhandledExceptionHandler(e => { }), Reuse.Singleton);
}
container.RegisterInstance<IOptionsFactory<DebugAdapterServerOptions>>(new ValueOptionsFactory<DebugAdapterServerOptions>(options));
container.RegisterInstance(options.Capabilities);
container.RegisterInstance(options.RequestProcessIdentifier);
container.RegisterMany<DebugAdapterServerProgressManager>(nonPublicServiceTypes: true, reuse: Reuse.Singleton);
container.RegisterMany<DebugAdapterServer>(
serviceTypeCondition: type => type == typeof(IDebugAdapterServer) || type == typeof(DebugAdapterServer),
reuse: Reuse.Singleton,
setup: Setup.With(condition: req => req.IsResolutionRoot || req.Container.Resolve<IInsanceHasStarted>().Started)
);
container.RegisterMany<DefaultDebugAdapterServerFacade>(
serviceTypeCondition: type => type.IsClass || !type.Name.Contains("Proxy") && typeof(DefaultDebugAdapterServerFacade).GetInterfaces().Except(typeof(DefaultDebugAdapterServerFacade).BaseType!.GetInterfaces()).Any(z => type == z),
reuse: Reuse.Singleton
);
// container.
var providedConfiguration = options.Services.FirstOrDefault(z => z.ServiceType == typeof(IConfiguration) && z.ImplementationInstance is IConfiguration);
container.RegisterDelegate<IConfiguration>(
_ => {
var builder = new ConfigurationBuilder();
if (outerServiceProvider != null)
{
var outerConfiguration = outerServiceProvider.GetService<IConfiguration>();
if (outerConfiguration != null)
{
builder.CustomAddConfiguration(outerConfiguration, false);
}
}
if (providedConfiguration != null)
{
builder.CustomAddConfiguration((providedConfiguration.ImplementationInstance as IConfiguration)!);
}
return builder.Build();
},
Reuse.Singleton
);
return container;
}
public static IServiceCollection AddDebugAdapterServer(this IServiceCollection services, Action<DebugAdapterServerOptions>? configureOptions = null) =>
AddDebugAdapterServer(services, Options.DefaultName, configureOptions);
public static IServiceCollection AddDebugAdapterServer(this IServiceCollection services, string name, Action<DebugAdapterServerOptions>? configureOptions = null)
{
// If we get called multiple times we're going to remove the default server
// and force consumers to use the resolver.
if (services.Any(d => d.ServiceType == typeof(DebugAdapterServer) || d.ServiceType == typeof(IDebugAdapterServer)))
{
services.RemoveAll<DebugAdapterServer>();
services.RemoveAll<IDebugAdapterServer>();
services.AddSingleton<IDebugAdapterServer>(
_ =>
throw new NotSupportedException("DebugAdapterServer has been registered multiple times, you must use DebugAdapterServer instead")
);
services.AddSingleton<DebugAdapterServer>(
_ =>
throw new NotSupportedException("DebugAdapterServer has been registered multiple times, you must use DebugAdapterServer instead")
);
}
services
.AddOptions()
.AddLogging();
services.TryAddSingleton<DebugAdapterServerResolver>();
services.TryAddSingleton(_ => _.GetRequiredService<DebugAdapterServerResolver>().Get(name));
services.TryAddSingleton<IDebugAdapterServer>(_ => _.GetRequiredService<DebugAdapterServerResolver>().Get(name));
if (configureOptions != null)
{
services.Configure(name, configureOptions);
}
return services;
}
}
}