From 0849571dd7d1696b698ef367245161af2fdfd41d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAref?= Date: Thu, 2 Jan 2025 12:39:54 +0330 Subject: [PATCH] add support for .net 9 --- .github/workflows/nuget.yml | 2 +- KafkaStorm.Test/KafkaStorm.Test.csproj | 8 ++++---- KafkaStorm.Test/TestBase.cs | 2 +- .../Exceptions/ConsumerNotConfiguredException.cs | 7 +------ KafkaStorm/Exceptions/DuplicateConsumerException.cs | 7 +------ KafkaStorm/Exceptions/MessageNullException.cs | 8 ++------ KafkaStorm/Exceptions/ProducerConfigNullException.cs | 7 +------ KafkaStorm/KafkaStorm.csproj | 12 ++++++------ KafkaStorm/Registration/Extensions.cs | 2 +- .../Registration/ProducerRegistrationFactory.cs | 2 +- KafkaStorm/Services/ConsumerHostedService.cs | 5 +++-- KafkaStorm/Services/MessageStore.cs | 2 +- KafkaStorm/Services/Producer.cs | 7 ++----- README.md | 2 +- 14 files changed, 26 insertions(+), 47 deletions(-) diff --git a/.github/workflows/nuget.yml b/.github/workflows/nuget.yml index c118c82..7690b32 100644 --- a/.github/workflows/nuget.yml +++ b/.github/workflows/nuget.yml @@ -16,7 +16,7 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@v1 with: - dotnet-version: '8.0.x' + dotnet-version: '9.0.x' - name: Install dependencies run: dotnet restore - name: Build diff --git a/KafkaStorm.Test/KafkaStorm.Test.csproj b/KafkaStorm.Test/KafkaStorm.Test.csproj index b3eaf3c..bbd5e51 100644 --- a/KafkaStorm.Test/KafkaStorm.Test.csproj +++ b/KafkaStorm.Test/KafkaStorm.Test.csproj @@ -1,7 +1,7 @@ - net8.0 + net9.0 enable enable @@ -10,13 +10,13 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/KafkaStorm.Test/TestBase.cs b/KafkaStorm.Test/TestBase.cs index 8db5284..0e3e7aa 100644 --- a/KafkaStorm.Test/TestBase.cs +++ b/KafkaStorm.Test/TestBase.cs @@ -32,7 +32,7 @@ private IServiceCollection ConfigureServices() { prf.ConfigProducer(new ProducerConfig {BootstrapServers = "localhost:29092",}); - prf.InMemoryQueue(); + ProducerRegistrationFactory.InMemoryQueue(); prf.SetQueueLimit(65536); }); diff --git a/KafkaStorm/Exceptions/ConsumerNotConfiguredException.cs b/KafkaStorm/Exceptions/ConsumerNotConfiguredException.cs index 11208f0..ecf9e75 100644 --- a/KafkaStorm/Exceptions/ConsumerNotConfiguredException.cs +++ b/KafkaStorm/Exceptions/ConsumerNotConfiguredException.cs @@ -2,9 +2,4 @@ namespace KafkaStorm.Exceptions; -public class ConsumerNotConfiguredException : Exception -{ - public ConsumerNotConfiguredException() : base("Consumer is not configured") - { - } -} \ No newline at end of file +public class ConsumerNotConfiguredException() : Exception("Consumer is not configured"); \ No newline at end of file diff --git a/KafkaStorm/Exceptions/DuplicateConsumerException.cs b/KafkaStorm/Exceptions/DuplicateConsumerException.cs index ceb6624..063fbf0 100644 --- a/KafkaStorm/Exceptions/DuplicateConsumerException.cs +++ b/KafkaStorm/Exceptions/DuplicateConsumerException.cs @@ -2,9 +2,4 @@ namespace KafkaStorm.Exceptions; -public class DuplicateConsumerException : Exception -{ - public DuplicateConsumerException(string name) : base($"Consumer added more than once ({name})") - { - } -} \ No newline at end of file +public class DuplicateConsumerException(string name) : Exception($"Consumer added more than once ({name})"); \ No newline at end of file diff --git a/KafkaStorm/Exceptions/MessageNullException.cs b/KafkaStorm/Exceptions/MessageNullException.cs index 2e1db6c..aeb0306 100644 --- a/KafkaStorm/Exceptions/MessageNullException.cs +++ b/KafkaStorm/Exceptions/MessageNullException.cs @@ -2,9 +2,5 @@ namespace KafkaStorm.Exceptions; -public class MessageNullException : ArgumentNullException -{ - public MessageNullException() : base("Message", $"Message of type {typeof(TMessage)} was null") - { - } -} \ No newline at end of file +public class MessageNullException() + : ArgumentNullException("Message", $"Message of type {typeof(TMessage)} was null"); \ No newline at end of file diff --git a/KafkaStorm/Exceptions/ProducerConfigNullException.cs b/KafkaStorm/Exceptions/ProducerConfigNullException.cs index 01934a6..d160e6b 100644 --- a/KafkaStorm/Exceptions/ProducerConfigNullException.cs +++ b/KafkaStorm/Exceptions/ProducerConfigNullException.cs @@ -2,9 +2,4 @@ namespace KafkaStorm.Exceptions; -public class ProducerConfigNullException : Exception -{ - public ProducerConfigNullException() : base("Producer Config is null") - { - } -} \ No newline at end of file +public class ProducerConfigNullException() : Exception("Producer Config is null"); \ No newline at end of file diff --git a/KafkaStorm/KafkaStorm.csproj b/KafkaStorm/KafkaStorm.csproj index 801e279..e7664ee 100644 --- a/KafkaStorm/KafkaStorm.csproj +++ b/KafkaStorm/KafkaStorm.csproj @@ -1,11 +1,11 @@ - net8.0 + net9.0 enable latest ArefAzizian - 8.1.0 + 9.0.0 KafKaStorm latest true @@ -22,9 +22,9 @@ - - - - + + + + diff --git a/KafkaStorm/Registration/Extensions.cs b/KafkaStorm/Registration/Extensions.cs index 789a6ee..1f9f70f 100644 --- a/KafkaStorm/Registration/Extensions.cs +++ b/KafkaStorm/Registration/Extensions.cs @@ -32,7 +32,7 @@ public static void AddConsumersFromAssembly(this ConsumerRegistrationFactory crf var method = typeof(ConsumerRegistrationFactory).GetMethod("AddConsumer"); var methodInfos = (from messageType in messageTypes let consumerType = GetConsumerType(assembly, messageType) - where consumerType != default + where consumerType != null select method!.MakeGenericMethod(consumerType, messageType)) .ToList(); foreach (var generic in methodInfos) diff --git a/KafkaStorm/Registration/ProducerRegistrationFactory.cs b/KafkaStorm/Registration/ProducerRegistrationFactory.cs index c85bac9..73bf5c2 100644 --- a/KafkaStorm/Registration/ProducerRegistrationFactory.cs +++ b/KafkaStorm/Registration/ProducerRegistrationFactory.cs @@ -38,7 +38,7 @@ public void SetQueueLimit(uint count) MaximumQueueMessageCount = count; } - public void InMemoryQueue(bool activate = true) + public static void InMemoryQueue(bool activate = true) { UseInMemoryQueue = activate; } diff --git a/KafkaStorm/Services/ConsumerHostedService.cs b/KafkaStorm/Services/ConsumerHostedService.cs index 02d1ce1..dca6a42 100644 --- a/KafkaStorm/Services/ConsumerHostedService.cs +++ b/KafkaStorm/Services/ConsumerHostedService.cs @@ -21,10 +21,11 @@ public ConsumerHostedService(IConsumer myConsumer) _myConsumer = myConsumer; var fullName = _myConsumer.GetType().FullName; + ArgumentException.ThrowIfNullOrEmpty(fullName); var succeeded = ConsumerRegistrationFactory.ConsumerConfigs.TryGetValue(fullName, out var config) && - ConsumerRegistrationFactory.ConsumerTopics.TryGetValue(fullName, out _topicName); + ConsumerRegistrationFactory.ConsumerTopics.TryGetValue(fullName, out _topicName!); _consumer = new ConsumerBuilder(succeeded ? config @@ -53,7 +54,7 @@ private void Handle(CancellationToken cancellationToken) var result = _consumer.Consume(ConsumerRegistrationFactory.ConsumingPeriod); if (result == null) return; - + var message = JsonSerializer.Deserialize(result.Message.Value) ?? throw new MessageNullException(); _myConsumer.Handle(message, cancellationToken); diff --git a/KafkaStorm/Services/MessageStore.cs b/KafkaStorm/Services/MessageStore.cs index b49138d..91de78e 100644 --- a/KafkaStorm/Services/MessageStore.cs +++ b/KafkaStorm/Services/MessageStore.cs @@ -18,7 +18,7 @@ public MessageStore() public (Guid id, StoredMessage? message) GetLastMessage() { - if (!_dictionary.Any()) return (Guid.Empty, default); + if (_dictionary.IsEmpty) return (Guid.Empty, null); var (key, value) = _dictionary.Last(); return (key, value); diff --git a/KafkaStorm/Services/Producer.cs b/KafkaStorm/Services/Producer.cs index 216737a..05a1736 100644 --- a/KafkaStorm/Services/Producer.cs +++ b/KafkaStorm/Services/Producer.cs @@ -66,12 +66,9 @@ public async Task ProduceNowAsync(TMessage message, string? topicName public async Task ProduceNowAsync(StoredMessage message) { - if (message is null) - { - throw new ArgumentNullException(nameof(message)); - } + ArgumentNullException.ThrowIfNull(message); - var dr = await _producer.ProduceAsync(message.Topic, new Message + await _producer.ProduceAsync(message.Topic, new Message { Value = JsonSerializer.Serialize(message.Body) }); diff --git a/README.md b/README.md index f5466af..82c7061 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Simple .net client for Kafka based on **Confluent.Kafka** ### Using package manager: ``` -Install-Package KafkaStorm -Version 8.1.0 +Install-Package KafkaStorm -Version 9.0.0 ``` # Usage/Examples