From 3cf141f6aa16502f7a5fca13135c7ea7badd8920 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Mon, 27 Dec 2021 14:01:21 +0800 Subject: [PATCH] Enable #nullable for NATS transport. --- src/DotNetCore.CAP.NATS/CAP.NATSOptions.cs | 4 ++-- .../CAP.Options.Extensions.cs | 3 +-- .../DotNetCore.CAP.NATS.csproj | 8 +------- src/DotNetCore.CAP.NATS/NATSConsumerClient.cs | 20 ++++++++++--------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/DotNetCore.CAP.NATS/CAP.NATSOptions.cs b/src/DotNetCore.CAP.NATS/CAP.NATSOptions.cs index 8ba4f0a..4761244 100644 --- a/src/DotNetCore.CAP.NATS/CAP.NATSOptions.cs +++ b/src/DotNetCore.CAP.NATS/CAP.NATSOptions.cs @@ -27,9 +27,9 @@ namespace DotNetCore.CAP /// /// Used to setup all NATs client options /// - public Options Options { get; set; } + public Options? Options { get; set; } - public Action StreamOptions { get; set; } + public Action? StreamOptions { get; set; } public Func NormalizeStreamName { get; set; } = origin => origin.Split('.')[0]; } diff --git a/src/DotNetCore.CAP.NATS/CAP.Options.Extensions.cs b/src/DotNetCore.CAP.NATS/CAP.Options.Extensions.cs index 743aafd..c6be74b 100644 --- a/src/DotNetCore.CAP.NATS/CAP.Options.Extensions.cs +++ b/src/DotNetCore.CAP.NATS/CAP.Options.Extensions.cs @@ -3,7 +3,6 @@ using System; using DotNetCore.CAP; -using JetBrains.Annotations; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -15,7 +14,7 @@ namespace Microsoft.Extensions.DependencyInjection /// /// CAP configuration options /// NATS bootstrap server urls. - public static CapOptions UseNATS(this CapOptions options, [CanBeNull] string bootstrapServers = null) + public static CapOptions UseNATS(this CapOptions options, string? bootstrapServers = null) { return options.UseNATS(opt => { diff --git a/src/DotNetCore.CAP.NATS/DotNetCore.CAP.NATS.csproj b/src/DotNetCore.CAP.NATS/DotNetCore.CAP.NATS.csproj index 2557ace..72b7718 100644 --- a/src/DotNetCore.CAP.NATS/DotNetCore.CAP.NATS.csproj +++ b/src/DotNetCore.CAP.NATS/DotNetCore.CAP.NATS.csproj @@ -2,16 +2,10 @@ netstandard2.1 - DotNetCore.CAP.NATS + enable $(PackageTags);NATS - - NU1605;NU1701 - NU1701;CS1591 - bin\$(Configuration)\netstandard2.1\DotNetCore.CAP.NATS.xml - - diff --git a/src/DotNetCore.CAP.NATS/NATSConsumerClient.cs b/src/DotNetCore.CAP.NATS/NATSConsumerClient.cs index f67d67a..a7ef802 100644 --- a/src/DotNetCore.CAP.NATS/NATSConsumerClient.cs +++ b/src/DotNetCore.CAP.NATS/NATSConsumerClient.cs @@ -21,7 +21,7 @@ namespace DotNetCore.CAP.NATS private readonly string _groupId; private readonly NATSOptions _natsOptions; - private IConnection _consumerClient; + private IConnection? _consumerClient; public NATSConsumerClient(string groupId, IOptions options) { @@ -29,15 +29,17 @@ namespace DotNetCore.CAP.NATS _natsOptions = options.Value ?? throw new ArgumentNullException(nameof(options)); } - public event EventHandler OnMessageReceived; + public event EventHandler? OnMessageReceived; - public event EventHandler OnLog; + public event EventHandler? OnLog; - public BrokerAddress BrokerAddress => new BrokerAddress("NATS", _natsOptions.Servers); + public BrokerAddress BrokerAddress => new ("NATS", _natsOptions.Servers); public ICollection FetchTopics(IEnumerable topicNames) { - var jsm = _consumerClient.CreateJetStreamManagementContext(); + Connect(); + + var jsm = _consumerClient!.CreateJetStreamManagementContext(); var streamGroup = topicNames.GroupBy(x => _natsOptions.NormalizeStreamName(x)); @@ -80,7 +82,7 @@ namespace DotNetCore.CAP.NATS Connect(); - var js = _consumerClient.CreateJetStreamContext(); + var js = _consumerClient!.CreateJetStreamContext(); foreach (var subject in topics) { @@ -106,7 +108,7 @@ namespace DotNetCore.CAP.NATS private void Subscription_MessageHandler(object sender, MsgHandlerEventArgs e) { - var headers = new Dictionary(); + var headers = new Dictionary(); foreach (string h in e.Message.Header.Keys) { @@ -126,7 +128,7 @@ namespace DotNetCore.CAP.NATS } } - public void Reject(object sender) + public void Reject(object? sender) { if (sender is Msg msg) { @@ -153,7 +155,7 @@ namespace DotNetCore.CAP.NATS if (_consumerClient == null) { var opts = _natsOptions.Options ?? ConnectionFactory.GetDefaultOptions(); - opts.Url = _natsOptions.Servers ?? opts.Url; + opts.Url ??= _natsOptions.Servers; opts.ClosedEventHandler = ConnectedEventHandler; opts.DisconnectedEventHandler = ConnectedEventHandler; opts.AsyncErrorEventHandler = AsyncErrorEventHandler;