diff --git a/RoslynBridge/RoslynBridge.csproj b/RoslynBridge/RoslynBridge.csproj index aa0cbd6..abe1fde 100644 --- a/RoslynBridge/RoslynBridge.csproj +++ b/RoslynBridge/RoslynBridge.csproj @@ -74,12 +74,14 @@ + + @@ -87,6 +89,10 @@ Designer + + Always + true + Always true @@ -95,6 +101,7 @@ + diff --git a/RoslynBridge/Services/ConfigurationService.cs b/RoslynBridge/Services/ConfigurationService.cs new file mode 100644 index 0000000..fa654fd --- /dev/null +++ b/RoslynBridge/Services/ConfigurationService.cs @@ -0,0 +1,85 @@ +using System; +using System.IO; +using System.Reflection; +using System.Text.Json; + +namespace RoslynBridge.Services +{ + /// + /// Service for reading configuration from appsettings.json + /// + public class ConfigurationService + { + private static ConfigurationService? _instance; + private readonly RoslynBridgeConfig _config; + + private ConfigurationService() + { + _config = LoadConfiguration(); + } + + public static ConfigurationService Instance => _instance ??= new ConfigurationService(); + + public string WebApiUrl => _config.RoslynBridge.WebApiUrl; + public int DefaultPort => _config.RoslynBridge.DefaultPort; + public int MaxPortRange => _config.RoslynBridge.MaxPortRange; + public int HeartbeatIntervalSeconds => _config.RoslynBridge.HeartbeatIntervalSeconds; + + private RoslynBridgeConfig LoadConfiguration() + { + try + { + // Get the directory where the extension is installed + var assemblyPath = Assembly.GetExecutingAssembly().Location; + var assemblyDir = Path.GetDirectoryName(assemblyPath); + var configPath = Path.Combine(assemblyDir ?? "", "appsettings.json"); + + if (File.Exists(configPath)) + { + var json = File.ReadAllText(configPath); + var config = JsonSerializer.Deserialize(json, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + if (config != null) + { + System.Diagnostics.Debug.WriteLine($"Loaded configuration from {configPath}"); + return config; + } + } + + System.Diagnostics.Debug.WriteLine($"Configuration file not found at {configPath}, using defaults"); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Error loading configuration: {ex.Message}, using defaults"); + } + + // Return default configuration + return new RoslynBridgeConfig + { + RoslynBridge = new RoslynBridgeSettings + { + WebApiUrl = "http://localhost:5001", + DefaultPort = 59123, + MaxPortRange = 10, + HeartbeatIntervalSeconds = 60 + } + }; + } + } + + public class RoslynBridgeConfig + { + public RoslynBridgeSettings RoslynBridge { get; set; } = new(); + } + + public class RoslynBridgeSettings + { + public string WebApiUrl { get; set; } = "http://localhost:5001"; + public int DefaultPort { get; set; } = 59123; + public int MaxPortRange { get; set; } = 10; + public int HeartbeatIntervalSeconds { get; set; } = 60; + } +} diff --git a/RoslynBridge/appsettings.json b/RoslynBridge/appsettings.json new file mode 100644 index 0000000..95d85e3 --- /dev/null +++ b/RoslynBridge/appsettings.json @@ -0,0 +1,8 @@ +{ + "RoslynBridge": { + "WebApiUrl": "http://localhost:5001", + "DefaultPort": 59123, + "MaxPortRange": 10, + "HeartbeatIntervalSeconds": 60 + } +}