Phase 2: Konfiguration externalisieren (appsettings.json + IOptions)

- Neue Core-Klasse DatabaseOptions (Sektion "Database"), gebunden via IOptions.
- appsettings.json im App-Projekt (Mongo-Connection + DB-Name), wird in den
  Output kopiert.
- Program.cs: hart codierte "mongodb://localhost:27017" und "PolyTraderDB"
  durch konfigurierte Werte ersetzt.
- Startup-Cleanup-Hack aus dem Kopf von Main() entfernt und gekapselt nach
  dem Host-Build über die konfigurierte DB neu verankert.
- Solution-Build 0 Fehler; appsettings.json im Output verifiziert.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
bergm
2026-07-01 16:22:37 +02:00
co-authored by Claude Opus 4.8
parent 119697a0c0
commit e312fbbaa7
4 changed files with 47 additions and 8 deletions
+20 -8
View File
@@ -6,6 +6,8 @@ using System.Threading.Channels;
using System.Windows.Forms;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using PolyTrader.Core.Configuration;
using PolyTraderSharp.Models;
using PolyTraderSharp.Services;
@@ -20,18 +22,17 @@ internal static class Program
{
ApplicationConfiguration.Initialize();
try
{
var cleanupClient = new MongoClient("mongodb://localhost:27017");
var cleanupCol = cleanupClient.GetDatabase("PolyTraderDB").GetCollection<MongoDB.Bson.BsonDocument>("closed_trades");
cleanupCol.DeleteMany(Builders<MongoDB.Bson.BsonDocument>.Filter.Type("_id", MongoDB.Bson.BsonType.ObjectId));
}
catch { }
Channel<CopySignal> copySignalChannel = Channel.CreateUnbounded<CopySignal>();
Channel<ClosedTrade> closedTradeChannel = Channel.CreateUnbounded<ClosedTrade>();
AppHost = Host.CreateDefaultBuilder().ConfigureServices(delegate(HostBuilderContext context, IServiceCollection services)
{
services.AddSingleton((Func<IServiceProvider, IMongoDatabase>)((IServiceProvider sp) => { var client = new MongoClient("mongodb://localhost:27017"); return client.GetDatabase("PolyTraderDB"); }));
services.Configure<DatabaseOptions>(context.Configuration.GetSection(DatabaseOptions.SectionName));
services.AddSingleton<IMongoDatabase>((IServiceProvider sp) =>
{
var dbOptions = sp.GetRequiredService<IOptions<DatabaseOptions>>().Value;
var client = new MongoClient(dbOptions.ConnectionString);
return client.GetDatabase(dbOptions.DatabaseName);
});
services.AddSingleton((IServiceProvider sp) => ServerSettings.Load("server_settings.xml"));
services.AddSingleton<TradingState>();
services.AddSingleton(copySignalChannel.Writer);
@@ -85,6 +86,17 @@ internal static class Program
try
{
var db = AppHost.Services.GetRequiredService<IMongoDatabase>();
// Einmal-Bereinigung (Alt-Bug): entferne closed_trades mit ObjectId-_id.
// Das _id muss die int-TradeId sein; fehlerhafte Auto-ObjectIds werden verworfen.
// Läuft jetzt über die konfigurierte DB statt über einen hart codierten Client.
try
{
var cleanupCol = db.GetCollection<MongoDB.Bson.BsonDocument>("closed_trades");
cleanupCol.DeleteMany(Builders<MongoDB.Bson.BsonDocument>.Filter.Type("_id", MongoDB.Bson.BsonType.ObjectId));
}
catch { }
var state = AppHost.Services.GetRequiredService<TradingState>();
var maxTradeDoc = db.GetCollection<MongoDB.Bson.BsonDocument>("closed_trades")
.Find(Builders<MongoDB.Bson.BsonDocument>.Filter.Empty)