From 6884802770a4761f14bf3de43aa899cdf94a9757 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 9 Jul 2019 16:28:16 +0800 Subject: [PATCH] Fix SqlServer options bug --- src/DotNetCore.CAP.SqlServer/CAP.EFOptions.cs | 2 +- .../ICollectProcessor.SqlServer.cs | 5 +++-- .../IMonitoringApi.SqlServer.cs | 5 +++-- .../IStorage.SqlServer.cs | 21 ++++++++++--------- .../IStorageConnection.SqlServer.cs | 9 +++++--- .../IStorageTransaction.SqlServer.cs | 1 - .../SqlServerStorageConnectionTest.cs | 2 +- 7 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/DotNetCore.CAP.SqlServer/CAP.EFOptions.cs b/src/DotNetCore.CAP.SqlServer/CAP.EFOptions.cs index c8399c3..abbe3ab 100644 --- a/src/DotNetCore.CAP.SqlServer/CAP.EFOptions.cs +++ b/src/DotNetCore.CAP.SqlServer/CAP.EFOptions.cs @@ -17,7 +17,7 @@ namespace DotNetCore.CAP public string Schema { get; set; } = DefaultSchema; /// - /// EF dbcontext type. + /// EF DbContext /// internal Type DbContextType { get; set; } diff --git a/src/DotNetCore.CAP.SqlServer/ICollectProcessor.SqlServer.cs b/src/DotNetCore.CAP.SqlServer/ICollectProcessor.SqlServer.cs index c734e73..ba147d4 100644 --- a/src/DotNetCore.CAP.SqlServer/ICollectProcessor.SqlServer.cs +++ b/src/DotNetCore.CAP.SqlServer/ICollectProcessor.SqlServer.cs @@ -20,12 +20,13 @@ namespace DotNetCore.CAP.SqlServer "Published", "Received" }; - private readonly TimeSpan _delay = TimeSpan.FromSeconds(1); private readonly ILogger _logger; private readonly SqlServerOptions _options; + private readonly TimeSpan _delay = TimeSpan.FromSeconds(1); private readonly TimeSpan _waitingInterval = TimeSpan.FromMinutes(5); - public SqlServerCollectProcessor(ILogger logger, + public SqlServerCollectProcessor( + ILogger logger, IOptions sqlServerOptions) { _logger = logger; diff --git a/src/DotNetCore.CAP.SqlServer/IMonitoringApi.SqlServer.cs b/src/DotNetCore.CAP.SqlServer/IMonitoringApi.SqlServer.cs index e3e49af..d5498f2 100644 --- a/src/DotNetCore.CAP.SqlServer/IMonitoringApi.SqlServer.cs +++ b/src/DotNetCore.CAP.SqlServer/IMonitoringApi.SqlServer.cs @@ -10,6 +10,7 @@ using DotNetCore.CAP.Dashboard; using DotNetCore.CAP.Dashboard.Monitoring; using DotNetCore.CAP.Infrastructure; using DotNetCore.CAP.Models; +using Microsoft.Extensions.Options; namespace DotNetCore.CAP.SqlServer { @@ -18,9 +19,9 @@ namespace DotNetCore.CAP.SqlServer private readonly SqlServerOptions _options; private readonly SqlServerStorage _storage; - public SqlServerMonitoringApi(IStorage storage, SqlServerOptions options) + public SqlServerMonitoringApi(IStorage storage, IOptions options) { - _options = options ?? throw new ArgumentNullException(nameof(options)); + _options = options.Value ?? throw new ArgumentNullException(nameof(options)); _storage = storage as SqlServerStorage ?? throw new ArgumentNullException(nameof(storage)); } diff --git a/src/DotNetCore.CAP.SqlServer/IStorage.SqlServer.cs b/src/DotNetCore.CAP.SqlServer/IStorage.SqlServer.cs index 4804406..f2eec37 100644 --- a/src/DotNetCore.CAP.SqlServer/IStorage.SqlServer.cs +++ b/src/DotNetCore.CAP.SqlServer/IStorage.SqlServer.cs @@ -17,21 +17,22 @@ namespace DotNetCore.CAP.SqlServer { public class SqlServerStorage : IStorage { - private readonly CapOptions _capOptions; - private readonly DiagnosticProcessorObserver _diagnosticProcessorObserver; - private readonly IDbConnection _existingConnection = null; private readonly ILogger _logger; - private readonly SqlServerOptions _options; + private readonly IOptions _capOptions; + private readonly IOptions _options; + private readonly IDbConnection _existingConnection = null; + private readonly DiagnosticProcessorObserver _diagnosticProcessorObserver; - public SqlServerStorage(ILogger logger, + public SqlServerStorage( + ILogger logger, IOptions capOptions, IOptions options, DiagnosticProcessorObserver diagnosticProcessorObserver) { - _options = options.Value; + _options = options; _diagnosticProcessorObserver = diagnosticProcessorObserver; _logger = logger; - _capOptions = capOptions.Value; + _capOptions = capOptions; } public IStorageConnection GetConnection() @@ -51,9 +52,9 @@ namespace DotNetCore.CAP.SqlServer return; } - var sql = CreateDbTablesScript(_options.Schema); + var sql = CreateDbTablesScript(_options.Value.Schema); - using (var connection = new SqlConnection(_options.ConnectionString)) + using (var connection = new SqlConnection(_options.Value.ConnectionString)) { await connection.ExecuteAsync(sql); } @@ -128,7 +129,7 @@ END;"; internal IDbConnection CreateAndOpenConnection() { - var connection = _existingConnection ?? new SqlConnection(_options.ConnectionString); + var connection = _existingConnection ?? new SqlConnection(_options.Value.ConnectionString); if (connection.State == ConnectionState.Closed) { diff --git a/src/DotNetCore.CAP.SqlServer/IStorageConnection.SqlServer.cs b/src/DotNetCore.CAP.SqlServer/IStorageConnection.SqlServer.cs index c7b0346..72978a1 100644 --- a/src/DotNetCore.CAP.SqlServer/IStorageConnection.SqlServer.cs +++ b/src/DotNetCore.CAP.SqlServer/IStorageConnection.SqlServer.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Dapper; using DotNetCore.CAP.Infrastructure; using DotNetCore.CAP.Models; +using Microsoft.Extensions.Options; namespace DotNetCore.CAP.SqlServer { @@ -15,10 +16,12 @@ namespace DotNetCore.CAP.SqlServer { private readonly CapOptions _capOptions; - public SqlServerStorageConnection(SqlServerOptions options, CapOptions capOptions) + public SqlServerStorageConnection( + IOptions options, + IOptions capOptions) { - _capOptions = capOptions; - Options = options; + _capOptions = capOptions.Value; + Options = options.Value; } public SqlServerOptions Options { get; } diff --git a/src/DotNetCore.CAP.SqlServer/IStorageTransaction.SqlServer.cs b/src/DotNetCore.CAP.SqlServer/IStorageTransaction.SqlServer.cs index 96cc849..d4d38f0 100644 --- a/src/DotNetCore.CAP.SqlServer/IStorageTransaction.SqlServer.cs +++ b/src/DotNetCore.CAP.SqlServer/IStorageTransaction.SqlServer.cs @@ -13,7 +13,6 @@ namespace DotNetCore.CAP.SqlServer public class SqlServerStorageTransaction : IStorageTransaction { private readonly IDbConnection _dbConnection; - private readonly string _schema; public SqlServerStorageTransaction(SqlServerStorageConnection connection) diff --git a/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageConnectionTest.cs b/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageConnectionTest.cs index b7a20b5..0df9649 100644 --- a/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageConnectionTest.cs +++ b/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageConnectionTest.cs @@ -14,7 +14,7 @@ namespace DotNetCore.CAP.SqlServer.Test public SqlServerStorageConnectionTest() { - _storage = new SqlServerStorageConnection(SqlSeverOptions.Value, CapOptions.Value); + _storage = new SqlServerStorageConnection(SqlSeverOptions, CapOptions); } [Fact]