Browse Source

Enable #nullable for NATS transport.

master
Savorboard 2 years ago
parent
commit
3cf141f6aa
4 changed files with 15 additions and 20 deletions
  1. +2
    -2
      src/DotNetCore.CAP.NATS/CAP.NATSOptions.cs
  2. +1
    -2
      src/DotNetCore.CAP.NATS/CAP.Options.Extensions.cs
  3. +1
    -7
      src/DotNetCore.CAP.NATS/DotNetCore.CAP.NATS.csproj
  4. +11
    -9
      src/DotNetCore.CAP.NATS/NATSConsumerClient.cs

+ 2
- 2
src/DotNetCore.CAP.NATS/CAP.NATSOptions.cs View File

@@ -27,9 +27,9 @@ namespace DotNetCore.CAP
/// <summary> /// <summary>
/// Used to setup all NATs client options /// Used to setup all NATs client options
/// </summary> /// </summary>
public Options Options { get; set; }
public Options? Options { get; set; }


public Action<StreamConfiguration.StreamConfigurationBuilder> StreamOptions { get; set; }
public Action<StreamConfiguration.StreamConfigurationBuilder>? StreamOptions { get; set; }


public Func<string, string> NormalizeStreamName { get; set; } = origin => origin.Split('.')[0]; public Func<string, string> NormalizeStreamName { get; set; } = origin => origin.Split('.')[0];
} }

+ 1
- 2
src/DotNetCore.CAP.NATS/CAP.Options.Extensions.cs View File

@@ -3,7 +3,6 @@


using System; using System;
using DotNetCore.CAP; using DotNetCore.CAP;
using JetBrains.Annotations;


// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
@@ -15,7 +14,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary> /// </summary>
/// <param name="options">CAP configuration options</param> /// <param name="options">CAP configuration options</param>
/// <param name="bootstrapServers">NATS bootstrap server urls.</param> /// <param name="bootstrapServers">NATS bootstrap server urls.</param>
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 => return options.UseNATS(opt =>
{ {


+ 1
- 7
src/DotNetCore.CAP.NATS/DotNetCore.CAP.NATS.csproj View File

@@ -2,16 +2,10 @@


<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>DotNetCore.CAP.NATS</AssemblyName>
<Nullable>enable</Nullable>
<PackageTags>$(PackageTags);NATS</PackageTags> <PackageTags>$(PackageTags);NATS</PackageTags>
</PropertyGroup> </PropertyGroup>


<PropertyGroup>
<WarningsAsErrors>NU1605;NU1701</WarningsAsErrors>
<NoWarn>NU1701;CS1591</NoWarn>
<DocumentationFile>bin\$(Configuration)\netstandard2.1\DotNetCore.CAP.NATS.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup> <ItemGroup>
<PackageReference Include="NATS.Client" Version="0.14.1" /> <PackageReference Include="NATS.Client" Version="0.14.1" />
</ItemGroup> </ItemGroup>


+ 11
- 9
src/DotNetCore.CAP.NATS/NATSConsumerClient.cs View File

@@ -21,7 +21,7 @@ namespace DotNetCore.CAP.NATS
private readonly string _groupId; private readonly string _groupId;
private readonly NATSOptions _natsOptions; private readonly NATSOptions _natsOptions;


private IConnection _consumerClient;
private IConnection? _consumerClient;


public NATSConsumerClient(string groupId, IOptions<NATSOptions> options) public NATSConsumerClient(string groupId, IOptions<NATSOptions> options)
{ {
@@ -29,15 +29,17 @@ namespace DotNetCore.CAP.NATS
_natsOptions = options.Value ?? throw new ArgumentNullException(nameof(options)); _natsOptions = options.Value ?? throw new ArgumentNullException(nameof(options));
} }


public event EventHandler<TransportMessage> OnMessageReceived;
public event EventHandler<TransportMessage>? OnMessageReceived;


public event EventHandler<LogMessageEventArgs> OnLog;
public event EventHandler<LogMessageEventArgs>? OnLog;


public BrokerAddress BrokerAddress => new BrokerAddress("NATS", _natsOptions.Servers);
public BrokerAddress BrokerAddress => new ("NATS", _natsOptions.Servers);


public ICollection<string> FetchTopics(IEnumerable<string> topicNames) public ICollection<string> FetchTopics(IEnumerable<string> topicNames)
{ {
var jsm = _consumerClient.CreateJetStreamManagementContext();
Connect();

var jsm = _consumerClient!.CreateJetStreamManagementContext();


var streamGroup = topicNames.GroupBy(x => _natsOptions.NormalizeStreamName(x)); var streamGroup = topicNames.GroupBy(x => _natsOptions.NormalizeStreamName(x));


@@ -80,7 +82,7 @@ namespace DotNetCore.CAP.NATS


Connect(); Connect();


var js = _consumerClient.CreateJetStreamContext();
var js = _consumerClient!.CreateJetStreamContext();


foreach (var subject in topics) foreach (var subject in topics)
{ {
@@ -106,7 +108,7 @@ namespace DotNetCore.CAP.NATS


private void Subscription_MessageHandler(object sender, MsgHandlerEventArgs e) private void Subscription_MessageHandler(object sender, MsgHandlerEventArgs e)
{ {
var headers = new Dictionary<string, string>();
var headers = new Dictionary<string, string?>();


foreach (string h in e.Message.Header.Keys) 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) if (sender is Msg msg)
{ {
@@ -153,7 +155,7 @@ namespace DotNetCore.CAP.NATS
if (_consumerClient == null) if (_consumerClient == null)
{ {
var opts = _natsOptions.Options ?? ConnectionFactory.GetDefaultOptions(); var opts = _natsOptions.Options ?? ConnectionFactory.GetDefaultOptions();
opts.Url = _natsOptions.Servers ?? opts.Url;
opts.Url ??= _natsOptions.Servers;
opts.ClosedEventHandler = ConnectedEventHandler; opts.ClosedEventHandler = ConnectedEventHandler;
opts.DisconnectedEventHandler = ConnectedEventHandler; opts.DisconnectedEventHandler = ConnectedEventHandler;
opts.AsyncErrorEventHandler = AsyncErrorEventHandler; opts.AsyncErrorEventHandler = AsyncErrorEventHandler;


Loading…
Cancel
Save