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:
@@ -28,6 +28,12 @@
|
|||||||
<Content Include="favicon.ico" />
|
<Content Include="favicon.ico" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="agentspace\antigravity\" />
|
<Folder Include="agentspace\antigravity\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
+20
-8
@@ -6,6 +6,8 @@ using System.Threading.Channels;
|
|||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using PolyTrader.Core.Configuration;
|
||||||
using PolyTraderSharp.Models;
|
using PolyTraderSharp.Models;
|
||||||
using PolyTraderSharp.Services;
|
using PolyTraderSharp.Services;
|
||||||
|
|
||||||
@@ -20,18 +22,17 @@ internal static class Program
|
|||||||
{
|
{
|
||||||
ApplicationConfiguration.Initialize();
|
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<CopySignal> copySignalChannel = Channel.CreateUnbounded<CopySignal>();
|
||||||
Channel<ClosedTrade> closedTradeChannel = Channel.CreateUnbounded<ClosedTrade>();
|
Channel<ClosedTrade> closedTradeChannel = Channel.CreateUnbounded<ClosedTrade>();
|
||||||
AppHost = Host.CreateDefaultBuilder().ConfigureServices(delegate(HostBuilderContext context, IServiceCollection services)
|
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((IServiceProvider sp) => ServerSettings.Load("server_settings.xml"));
|
||||||
services.AddSingleton<TradingState>();
|
services.AddSingleton<TradingState>();
|
||||||
services.AddSingleton(copySignalChannel.Writer);
|
services.AddSingleton(copySignalChannel.Writer);
|
||||||
@@ -85,6 +86,17 @@ internal static class Program
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var db = AppHost.Services.GetRequiredService<IMongoDatabase>();
|
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 state = AppHost.Services.GetRequiredService<TradingState>();
|
||||||
var maxTradeDoc = db.GetCollection<MongoDB.Bson.BsonDocument>("closed_trades")
|
var maxTradeDoc = db.GetCollection<MongoDB.Bson.BsonDocument>("closed_trades")
|
||||||
.Find(Builders<MongoDB.Bson.BsonDocument>.Filter.Empty)
|
.Find(Builders<MongoDB.Bson.BsonDocument>.Filter.Empty)
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"Database": {
|
||||||
|
"ConnectionString": "mongodb://localhost:27017",
|
||||||
|
"DatabaseName": "PolyTraderDB"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
namespace PolyTrader.Core.Configuration
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Datenbank-Konfiguration, gebunden an die "Database"-Sektion in appsettings.json.
|
||||||
|
/// Ersetzt die frühere Hartcodierung von Connection-String und DB-Name.
|
||||||
|
/// </summary>
|
||||||
|
public class DatabaseOptions
|
||||||
|
{
|
||||||
|
public const string SectionName = "Database";
|
||||||
|
|
||||||
|
public string ConnectionString { get; set; } = "mongodb://localhost:27017";
|
||||||
|
|
||||||
|
public string DatabaseName { get; set; } = "PolyTraderDB";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user