-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathServiceLocator.cs
More file actions
70 lines (58 loc) · 2.93 KB
/
ServiceLocator.cs
File metadata and controls
70 lines (58 loc) · 2.93 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
using DecompilerServer.Services;
using Microsoft.Extensions.DependencyInjection;
namespace DecompilerServer;
/// <summary>
/// Simple service locator for MCP tools to access registered services.
/// Thread-safe to support concurrent test execution and MCP tool calls.
/// </summary>
public static class ServiceLocator
{
private static readonly ThreadLocal<IServiceProvider?> _threadLocalProvider = new ThreadLocal<IServiceProvider?>();
private static volatile IServiceProvider? _globalProvider;
private static readonly object _lock = new object();
/// <summary>
/// Sets the service provider. Uses global storage for production, thread-local for tests.
/// </summary>
public static void SetServiceProvider(IServiceProvider? serviceProvider)
{
// Always set thread-local for test compatibility.
_threadLocalProvider.Value = serviceProvider;
// Refresh the global provider whenever a non-null provider is supplied.
// Tests create and dispose independent service providers, so "first write wins"
// leaves the global fallback pointing at stale containers.
if (serviceProvider != null)
{
lock (_lock)
{
_globalProvider = serviceProvider;
}
}
}
public static T GetRequiredService<T>() where T : notnull
{
// Try thread-local first (for tests)
var provider = _threadLocalProvider.Value ?? _globalProvider;
if (provider == null)
throw new InvalidOperationException("Service provider not initialized for current thread");
return provider.GetRequiredService<T>();
}
public static T? GetService<T>() where T : class
{
var provider = _threadLocalProvider.Value ?? _globalProvider;
return provider?.GetService<T>();
}
public static DecompilerWorkspace? Workspace => GetService<DecompilerWorkspace>();
public static AssemblyContextManager ContextManager => TryGetCurrentSession()?.ContextManager ?? GetRequiredService<AssemblyContextManager>();
public static MemberResolver MemberResolver => TryGetCurrentSession()?.MemberResolver ?? GetRequiredService<MemberResolver>();
public static DecompilerService DecompilerService => TryGetCurrentSession()?.DecompilerService ?? GetRequiredService<DecompilerService>();
public static UsageAnalyzer UsageAnalyzer => TryGetCurrentSession()?.UsageAnalyzer ?? GetRequiredService<UsageAnalyzer>();
public static InheritanceAnalyzer InheritanceAnalyzer => TryGetCurrentSession()?.InheritanceAnalyzer ?? GetRequiredService<InheritanceAnalyzer>();
public static ResponseFormatter ResponseFormatter => GetRequiredService<ResponseFormatter>();
private static DecompilerSession? TryGetCurrentSession()
{
var workspace = GetService<DecompilerWorkspace>();
if (workspace != null && workspace.TryGetCurrentSession(out var session))
return session;
return null;
}
}