Browse Source

Enable #nullable for AmazonSQS

master
savorboard 3 years ago
parent
commit
43ada6042c
6 changed files with 33 additions and 33 deletions
  1. +2
    -2
      src/DotNetCore.CAP.AmazonSQS/AmazonPolicyExtensions.cs
  2. +13
    -13
      src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs
  3. +4
    -4
      src/DotNetCore.CAP.AmazonSQS/CAP.AmazonSQSOptions.cs
  4. +2
    -2
      src/DotNetCore.CAP.AmazonSQS/DotNetCore.CAP.AmazonSQS.csproj
  5. +8
    -8
      src/DotNetCore.CAP.AmazonSQS/ITransport.AmazonSQS.cs
  6. +4
    -4
      src/DotNetCore.CAP.AmazonSQS/SQSReceivedMessage.cs

+ 2
- 2
src/DotNetCore.CAP.AmazonSQS/AmazonPolicyExtensions.cs View File

@@ -209,7 +209,7 @@ namespace DotNetCore.CAP.AmazonSQS
/// </summary> /// </summary>
/// <param name="arn">Source ARN</param> /// <param name="arn">Source ARN</param>
/// <returns>Group prefix or null if group not present</returns> /// <returns>Group prefix or null if group not present</returns>
private static string GetArnGroupPrefix(string arn)
private static string? GetArnGroupPrefix(string arn)
{ {
const char separator = '-'; const char separator = '-';
if (string.IsNullOrEmpty(arn) || !arn.Contains(separator)) if (string.IsNullOrEmpty(arn) || !arn.Contains(separator))
@@ -235,7 +235,7 @@ namespace DotNetCore.CAP.AmazonSQS
/// </summary> /// </summary>
/// <param name="arn">Source ARN</param> /// <param name="arn">Source ARN</param>
/// <returns>Group name or null if group not present</returns> /// <returns>Group name or null if group not present</returns>
private static string GetGroupName(string arn)
private static string? GetGroupName(string arn)
{ {
const char separator = ':'; const char separator = ':';
if (string.IsNullOrEmpty(arn) || !arn.Contains(separator)) if (string.IsNullOrEmpty(arn) || !arn.Contains(separator))


+ 13
- 13
src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs View File

@@ -27,8 +27,8 @@ namespace DotNetCore.CAP.AmazonSQS
private readonly string _groupId; private readonly string _groupId;
private readonly AmazonSQSOptions _amazonSQSOptions; private readonly AmazonSQSOptions _amazonSQSOptions;


private IAmazonSimpleNotificationService _snsClient;
private IAmazonSQS _sqsClient;
private IAmazonSimpleNotificationService? _snsClient;
private IAmazonSQS? _sqsClient;
private string _queueUrl = string.Empty; private string _queueUrl = string.Empty;


public AmazonSQSConsumerClient(string groupId, IOptions<AmazonSQSOptions> options) public AmazonSQSConsumerClient(string groupId, IOptions<AmazonSQSOptions> options)
@@ -37,9 +37,9 @@ namespace DotNetCore.CAP.AmazonSQS
_amazonSQSOptions = options.Value; _amazonSQSOptions = options.Value;
} }


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("AmazonSQS", _queueUrl); public BrokerAddress BrokerAddress => new BrokerAddress("AmazonSQS", _queueUrl);


@@ -57,7 +57,7 @@ namespace DotNetCore.CAP.AmazonSQS
{ {
var createTopicRequest = new CreateTopicRequest(topic.NormalizeForAws()); var createTopicRequest = new CreateTopicRequest(topic.NormalizeForAws());


var createTopicResponse = _snsClient.CreateTopicAsync(createTopicRequest).GetAwaiter().GetResult();
var createTopicResponse = _snsClient!.CreateTopicAsync(createTopicRequest).GetAwaiter().GetResult();


topicArns.Add(createTopicResponse.TopicArn); topicArns.Add(createTopicResponse.TopicArn);
} }
@@ -92,13 +92,13 @@ namespace DotNetCore.CAP.AmazonSQS


while (true) while (true)
{ {
var response = _sqsClient.ReceiveMessageAsync(request, cancellationToken).GetAwaiter().GetResult();
var response = _sqsClient!.ReceiveMessageAsync(request, cancellationToken).GetAwaiter().GetResult();


if (response.Messages.Count == 1) if (response.Messages.Count == 1)
{ {
var messageObj = JsonSerializer.Deserialize<SQSReceivedMessage>(response.Messages[0].Body); var messageObj = JsonSerializer.Deserialize<SQSReceivedMessage>(response.Messages[0].Body);


var header = messageObj.MessageAttributes.ToDictionary(x => x.Key, x => x.Value.Value);
var header = messageObj!.MessageAttributes.ToDictionary(x => x.Key, x => x.Value.Value);
var body = messageObj.Message; var body = messageObj.Message;


var message = new TransportMessage(header, body != null ? Encoding.UTF8.GetBytes(body) : null); var message = new TransportMessage(header, body != null ? Encoding.UTF8.GetBytes(body) : null);
@@ -119,7 +119,7 @@ namespace DotNetCore.CAP.AmazonSQS
{ {
try try
{ {
_ = _sqsClient.DeleteMessageAsync(_queueUrl, (string)sender).GetAwaiter().GetResult();
_ = _sqsClient!.DeleteMessageAsync(_queueUrl, (string)sender).GetAwaiter().GetResult();
} }
catch (InvalidIdFormatException ex) catch (InvalidIdFormatException ex)
{ {
@@ -127,12 +127,12 @@ namespace DotNetCore.CAP.AmazonSQS
} }
} }


public void Reject(object sender)
public void Reject(object? sender)
{ {
try try
{ {
// Visible again in 3 seconds // Visible again in 3 seconds
_ = _sqsClient.ChangeMessageVisibilityAsync(_queueUrl, (string)sender, 3).GetAwaiter().GetResult();
_ = _sqsClient!.ChangeMessageVisibilityAsync(_queueUrl, (string)sender!, 3).GetAwaiter().GetResult();
} }
catch (MessageNotInflightException ex) catch (MessageNotInflightException ex)
{ {
@@ -237,7 +237,7 @@ namespace DotNetCore.CAP.AmazonSQS
{ {
Connect(initSNS: false, initSQS: true); Connect(initSNS: false, initSQS: true);


var queueAttributes = await _sqsClient.GetAttributesAsync(_queueUrl).ConfigureAwait(false);
var queueAttributes = await _sqsClient!.GetAttributesAsync(_queueUrl).ConfigureAwait(false);


var sqsQueueArn = queueAttributes["QueueArn"]; var sqsQueueArn = queueAttributes["QueueArn"];


@@ -263,12 +263,12 @@ namespace DotNetCore.CAP.AmazonSQS
private async Task SubscribeToTopics(IEnumerable<string> topics) private async Task SubscribeToTopics(IEnumerable<string> topics)
{ {
var queueAttributes = await _sqsClient.GetAttributesAsync(_queueUrl).ConfigureAwait(false);
var queueAttributes = await _sqsClient!.GetAttributesAsync(_queueUrl).ConfigureAwait(false);


var sqsQueueArn = queueAttributes["QueueArn"]; var sqsQueueArn = queueAttributes["QueueArn"];
foreach (var topicArn in topics) foreach (var topicArn in topics)
{ {
await _snsClient.SubscribeAsync(new SubscribeRequest
await _snsClient!.SubscribeAsync(new SubscribeRequest
{ {
TopicArn = topicArn, TopicArn = topicArn,
Protocol = "sqs", Protocol = "sqs",


+ 4
- 4
src/DotNetCore.CAP.AmazonSQS/CAP.AmazonSQSOptions.cs View File

@@ -10,19 +10,19 @@ namespace DotNetCore.CAP
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
public class AmazonSQSOptions public class AmazonSQSOptions
{ {
public RegionEndpoint Region { get; set; }
public RegionEndpoint Region { get; set; } = default!;


public AWSCredentials Credentials { get; set; }
public AWSCredentials? Credentials { get; set; }


/// <summary> /// <summary>
/// Overrides Service Url deduced from AWS Region. To use in local development environments like localstack. /// Overrides Service Url deduced from AWS Region. To use in local development environments like localstack.
/// </summary> /// </summary>
public string SNSServiceUrl { get; set; }
public string? SNSServiceUrl { get; set; }


/// <summary> /// <summary>
/// Overrides Service Url deduced from AWS Region. To use in local development environments like localstack. /// Overrides Service Url deduced from AWS Region. To use in local development environments like localstack.
/// </summary> /// </summary>
public string SQSServiceUrl { get; set; }
public string? SQSServiceUrl { get; set; }


} }
} }

+ 2
- 2
src/DotNetCore.CAP.AmazonSQS/DotNetCore.CAP.AmazonSQS.csproj View File

@@ -2,12 +2,12 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>DotNetCore.CAP.AmazonSQS</AssemblyName>
<Nullable>enable</Nullable>
<PackageTags>$(PackageTags);AmazonSQS;SQS</PackageTags> <PackageTags>$(PackageTags);AmazonSQS;SQS</PackageTags>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<DocumentationFile>bin\$(Configuration)\netstandard2.1\DotNetCore.CAP.AmazonSQS.xml</DocumentationFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1701;1702;1705;CS1591</NoWarn> <NoWarn>1701;1702;1705;CS1591</NoWarn>
</PropertyGroup> </PropertyGroup>




+ 8
- 8
src/DotNetCore.CAP.AmazonSQS/ITransport.AmazonSQS.cs View File

@@ -22,8 +22,8 @@ namespace DotNetCore.CAP.AmazonSQS
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOptions<AmazonSQSOptions> _sqsOptions; private readonly IOptions<AmazonSQSOptions> _sqsOptions;
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private IAmazonSimpleNotificationService _snsClient;
private IDictionary<string, string> _topicArnMaps;
private IAmazonSimpleNotificationService? _snsClient;
private IDictionary<string, string>? _topicArnMaps;


public AmazonSQSTransport(ILogger<AmazonSQSTransport> logger, IOptions<AmazonSQSOptions> sqsOptions) public AmazonSQSTransport(ILogger<AmazonSQSTransport> logger, IOptions<AmazonSQSOptions> sqsOptions)
{ {
@@ -41,7 +41,7 @@ namespace DotNetCore.CAP.AmazonSQS


if (TryGetOrCreateTopicArn(message.GetName().NormalizeForAws(), out var arn)) if (TryGetOrCreateTopicArn(message.GetName().NormalizeForAws(), out var arn))
{ {
string bodyJson = null;
string? bodyJson = null;
if (message.Body != null) if (message.Body != null)
{ {
bodyJson = Encoding.UTF8.GetString(message.Body); bodyJson = Encoding.UTF8.GetString(message.Body);
@@ -59,7 +59,7 @@ namespace DotNetCore.CAP.AmazonSQS
MessageAttributes = attributes MessageAttributes = attributes
}; };


await _snsClient.PublishAsync(request);
await _snsClient!.PublishAsync(request);


_logger.LogDebug($"SNS topic message [{message.GetName().NormalizeForAws()}] has been published."); _logger.LogDebug($"SNS topic message [{message.GetName().NormalizeForAws()}] has been published.");
return OperateResult.Success; return OperateResult.Success;
@@ -117,7 +117,7 @@ namespace DotNetCore.CAP.AmazonSQS
{ {
_topicArnMaps = new Dictionary<string, string>(); _topicArnMaps = new Dictionary<string, string>();
string nextToken = null;
string? nextToken = null;
do do
{ {
var topics = nextToken == null var topics = nextToken == null
@@ -143,15 +143,15 @@ namespace DotNetCore.CAP.AmazonSQS
} }
} }
private bool TryGetOrCreateTopicArn(string topicName, out string topicArn)
private bool TryGetOrCreateTopicArn(string topicName,[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out string? topicArn)
{ {
topicArn = null; topicArn = null;
if (_topicArnMaps.TryGetValue(topicName, out topicArn))
if (_topicArnMaps!.TryGetValue(topicName, out topicArn))
{ {
return true; return true;
} }


var response = _snsClient.CreateTopicAsync(topicName).GetAwaiter().GetResult();
var response = _snsClient!.CreateTopicAsync(topicName).GetAwaiter().GetResult();


if (string.IsNullOrEmpty(response.TopicArn)) if (string.IsNullOrEmpty(response.TopicArn))
{ {


+ 4
- 4
src/DotNetCore.CAP.AmazonSQS/SQSReceivedMessage.cs View File

@@ -4,15 +4,15 @@ namespace DotNetCore.CAP.AmazonSQS
{ {
class SQSReceivedMessage class SQSReceivedMessage
{ {
public string Message { get; set; }
public string? Message { get; set; }


public Dictionary<string, SQSReceivedMessageAttributes> MessageAttributes { get; set; }
public Dictionary<string, SQSReceivedMessageAttributes> MessageAttributes { get; set; } = default!;
} }


class SQSReceivedMessageAttributes class SQSReceivedMessageAttributes
{ {
public string Type { get; set; }
public string? Type { get; set; }


public string Value { get; set; }
public string? Value { get; set; }
} }
} }

Loading…
Cancel
Save