diff --git a/Examples/01-PublishSubscribe/Program.cs b/Examples/01-PublishSubscribe/Program.cs index 295de7b..aaf32ad 100644 --- a/Examples/01-PublishSubscribe/Program.cs +++ b/Examples/01-PublishSubscribe/Program.cs @@ -23,7 +23,7 @@ namespace _01_PublishSubscribe { public class Program { - public static void Main(string[] args) + public static void Main() { var dependencyResolver = GetSimpleInjectorDependencyResolver(); @@ -47,7 +47,7 @@ namespace _01_PublishSubscribe .RegisterAllControllers() .Build(); - using (var connection = new TapetiConnection(config) + await using var connection = new TapetiConnection(config) { // Params is optional if you want to use the defaults, but we'll set it // explicitly for this example @@ -63,28 +63,27 @@ namespace _01_PublishSubscribe { "example", "01 - Publish Subscribe" } } } - }) - { - // IoC containers that separate the builder from the resolver (Autofac) must be built after - // creating a TapetConnection, as it modifies the container by injecting IPublisher. - (dependencyResolver as AutofacDependencyResolver)?.Build(); + }; + + // IoC containers that separate the builder from the resolver (Autofac) must be built after + // creating a TapetConnection, as it modifies the container by injecting IPublisher. + (dependencyResolver as AutofacDependencyResolver)?.Build(); - // Create the queues and start consuming immediately. - // If you need to do some processing before processing messages, but after the - // queues have initialized, pass false as the startConsuming parameter and store - // the returned ISubscriber. Then call Resume on it later. - await connection.Subscribe(); + // Create the queues and start consuming immediately. + // If you need to do some processing before processing messages, but after the + // queues have initialized, pass false as the startConsuming parameter and store + // the returned ISubscriber. Then call Resume on it later. + await connection.Subscribe(); - // We could get an IPublisher from the container directly, but since you'll usually use - // it as an injected constructor parameter this shows - await dependencyResolver.Resolve().SendTestMessage(); + // We could get an IPublisher from the container directly, but since you'll usually use + // it as an injected constructor parameter this shows + await dependencyResolver.Resolve().SendTestMessage(); - // Wait for the controller to signal that the message has been received - await waitForDone(); - } + // Wait for the controller to signal that the message has been received + await waitForDone(); } diff --git a/Examples/02-DeclareDurableQueues/Program.cs b/Examples/02-DeclareDurableQueues/Program.cs index 935470a..c98234b 100644 --- a/Examples/02-DeclareDurableQueues/Program.cs +++ b/Examples/02-DeclareDurableQueues/Program.cs @@ -11,7 +11,7 @@ namespace _02_DeclareDurableQueues { public class Program { - public static void Main(string[] args) + public static void Main() { var container = new Container(); var dependencyResolver = new SimpleInjectorDependencyResolver(container); @@ -30,19 +30,18 @@ namespace _02_DeclareDurableQueues .EnableDeclareDurableQueues() .Build(); - using (var connection = new TapetiConnection(config)) + await using var connection = new TapetiConnection(config); + + // This creates or updates the durable queue + await connection.Subscribe(); + + await dependencyResolver.Resolve().Publish(new PublishSubscribeMessage { - // This creates or updates the durable queue - await connection.Subscribe(); + Greeting = "Hello durable queue!" + }); - await dependencyResolver.Resolve().Publish(new PublishSubscribeMessage - { - Greeting = "Hello durable queue!" - }); - - // Wait for the controller to signal that the message has been received - await waitForDone(); - } + // Wait for the controller to signal that the message has been received + await waitForDone(); } } } diff --git a/Examples/03-FlowRequestResponse/Program.cs b/Examples/03-FlowRequestResponse/Program.cs index 50a361b..b83e86f 100644 --- a/Examples/03-FlowRequestResponse/Program.cs +++ b/Examples/03-FlowRequestResponse/Program.cs @@ -12,7 +12,7 @@ namespace _03_FlowRequestResponse { public class Program { - public static void Main(string[] args) + public static void Main() { var container = new Container(); var dependencyResolver = new SimpleInjectorDependencyResolver(container); @@ -33,34 +33,33 @@ namespace _03_FlowRequestResponse .Build(); - using (var connection = new TapetiConnection(config)) + await using var connection = new TapetiConnection(config); + + // Must be called before using any flow. When using a persistent repository like the + // SQL server implementation, you can run any required update scripts (for example, using DbUp) + // before calling this Load method. + // Call after creating the TapetiConnection, as it modifies the container to inject IPublisher. + await dependencyResolver.Resolve().Load(); + + + await connection.Subscribe(); + + + var flowStarter = dependencyResolver.Resolve(); + + var startData = new SimpleFlowController.StartData { - // Must be called before using any flow. When using a persistent repository like the - // SQL server implementation, you can run any required update scripts (for example, using DbUp) - // before calling this Load method. - // Call after creating the TapetiConnection, as it modifies the container to inject IPublisher. - await dependencyResolver.Resolve().Load(); + RequestStartTime = DateTime.Now, + Amount = 1 + }; - await connection.Subscribe(); + await flowStarter.Start(c => c.StartFlow, startData); + await flowStarter.Start(c => c.StartFlow); - var flowStarter = dependencyResolver.Resolve(); - - var startData = new SimpleFlowController.StartData - { - RequestStartTime = DateTime.Now, - Amount = 1 - }; - - - await flowStarter.Start(c => c.StartFlow, startData); - await flowStarter.Start(c => c.StartFlow); - - - // Wait for the controller to signal that the message has been received - await waitForDone(); - } + // Wait for the controller to signal that the message has been received + await waitForDone(); } } } diff --git a/Examples/03-FlowRequestResponse/ReceivingMessageController.cs b/Examples/03-FlowRequestResponse/ReceivingMessageController.cs index 71d1ba6..f9c83d5 100644 --- a/Examples/03-FlowRequestResponse/ReceivingMessageController.cs +++ b/Examples/03-FlowRequestResponse/ReceivingMessageController.cs @@ -9,25 +9,17 @@ namespace _03_FlowRequestResponse public class ReceivingMessageController { // No publisher required, responses can simply be returned + #pragma warning disable CA1822 // Mark members as static - not supported yet by Tapeti public async Task HandleQuoteRequest(QuoteRequestMessage message) { - string quote; - - switch (message.Amount) + var quote = message.Amount switch { - case 1: + 1 => // Well, they asked for it... :-) - quote = "'"; - break; - - case 2: - quote = "\""; - break; - - default: - quote = new string('\'', message.Amount); - break; - } + "'", + 2 => "\"", + _ => new string('\'', message.Amount) + }; // Just gonna let them wait for a bit, to demonstrate async message handlers await Task.Delay(1000); @@ -37,5 +29,6 @@ namespace _03_FlowRequestResponse Quote = quote }; } + #pragma warning restore CA1822 } } diff --git a/Examples/04-Transient/Program.cs b/Examples/04-Transient/Program.cs index 18b84f9..8e8e21a 100644 --- a/Examples/04-Transient/Program.cs +++ b/Examples/04-Transient/Program.cs @@ -13,7 +13,7 @@ namespace _04_Transient { public class Program { - public static void Main(string[] args) + public static void Main() { var container = new Container(); var dependencyResolver = new SimpleInjectorDependencyResolver(container); @@ -34,22 +34,20 @@ namespace _04_Transient .Build(); - using (var connection = new TapetiConnection(config)) - { - await connection.Subscribe(); + await using var connection = new TapetiConnection(config); + await connection.Subscribe(); - Console.WriteLine("Sending request..."); + Console.WriteLine("Sending request..."); - var transientPublisher = dependencyResolver.Resolve(); - var response = await transientPublisher.RequestResponse( - new LoggedInUsersRequestMessage()); + var transientPublisher = dependencyResolver.Resolve(); + var response = await transientPublisher.RequestResponse( + new LoggedInUsersRequestMessage()); - Console.WriteLine("Response: " + response.Count); + Console.WriteLine("Response: " + response.Count); - // Unlike the other example, there is no need to call waitForDone, once we're here the response has been handled. - } + // Unlike the other example, there is no need to call waitForDone, once we're here the response has been handled. } } } diff --git a/Examples/05-SpeedTest/Program.cs b/Examples/05-SpeedTest/Program.cs index 6c399d7..11404af 100644 --- a/Examples/05-SpeedTest/Program.cs +++ b/Examples/05-SpeedTest/Program.cs @@ -21,7 +21,7 @@ namespace _05_SpeedTest private const int ConcurrentTasks = 20; - public static void Main(string[] args) + public static void Main() { var container = new Container(); var dependencyResolver = new SimpleInjectorDependencyResolver(container); @@ -52,34 +52,32 @@ namespace _05_SpeedTest .Build(); - using (var connection = new TapetiConnection(config)) - { - var subscriber = await connection.Subscribe(false); + await using var connection = new TapetiConnection(config); + var subscriber = await connection.Subscribe(false); - var publisher = dependencyResolver.Resolve(); - Console.WriteLine($"Publishing {MessageCount} messages..."); + var publisher = dependencyResolver.Resolve(); + Console.WriteLine($"Publishing {MessageCount} messages..."); - var stopwatch = new Stopwatch(); - stopwatch.Start(); + var stopwatch = new Stopwatch(); + stopwatch.Start(); - await PublishMessages(publisher); + await PublishMessages(publisher); - stopwatch.Stop(); - Console.WriteLine($"Took {stopwatch.ElapsedMilliseconds} ms, {MessageCount / (stopwatch.ElapsedMilliseconds / 1000F):F0} messages/sec"); + stopwatch.Stop(); + Console.WriteLine($"Took {stopwatch.ElapsedMilliseconds} ms, {MessageCount / (stopwatch.ElapsedMilliseconds / 1000F):F0} messages/sec"); - Console.WriteLine("Consuming messages..."); - await subscriber.Resume(); + Console.WriteLine("Consuming messages..."); + await subscriber.Resume(); - stopwatch.Restart(); + stopwatch.Restart(); - await waitForDone(); + await waitForDone(); - stopwatch.Stop(); - Console.WriteLine($"Took {stopwatch.ElapsedMilliseconds} ms, {MessageCount / (stopwatch.ElapsedMilliseconds / 1000F):F0} messages/sec"); - } + stopwatch.Stop(); + Console.WriteLine($"Took {stopwatch.ElapsedMilliseconds} ms, {MessageCount / (stopwatch.ElapsedMilliseconds / 1000F):F0} messages/sec"); } diff --git a/Examples/06-StatelessRequestResponse/Program.cs b/Examples/06-StatelessRequestResponse/Program.cs index 6cc0de3..4e0e83e 100644 --- a/Examples/06-StatelessRequestResponse/Program.cs +++ b/Examples/06-StatelessRequestResponse/Program.cs @@ -12,7 +12,7 @@ namespace _06_StatelessRequestResponse { public class Program { - public static void Main(string[] args) + public static void Main() { var container = new Container(); var dependencyResolver = new SimpleInjectorDependencyResolver(container); @@ -32,20 +32,18 @@ namespace _06_StatelessRequestResponse .Build(); - using (var connection = new TapetiConnection(config)) - { - await connection.Subscribe(); + await using var connection = new TapetiConnection(config); + await connection.Subscribe(); - var publisher = dependencyResolver.Resolve(); - await publisher.PublishRequest( - new QuoteRequestMessage - { - Amount = 1 - }, - c => c.HandleQuoteResponse); + var publisher = dependencyResolver.Resolve(); + await publisher.PublishRequest( + new QuoteRequestMessage + { + Amount = 1 + }, + c => c.HandleQuoteResponse); - await waitForDone(); - } + await waitForDone(); } } } diff --git a/Examples/06-StatelessRequestResponse/ReceivingMessageController.cs b/Examples/06-StatelessRequestResponse/ReceivingMessageController.cs index ea947c8..9133713 100644 --- a/Examples/06-StatelessRequestResponse/ReceivingMessageController.cs +++ b/Examples/06-StatelessRequestResponse/ReceivingMessageController.cs @@ -8,31 +8,23 @@ namespace _06_StatelessRequestResponse public class ReceivingMessageController { // No publisher required, responses can simply be returned + #pragma warning disable CA1822 // Mark members as static - not supported yet by Tapeti public QuoteResponseMessage HandleQuoteRequest(QuoteRequestMessage message) { - string quote; - - switch (message.Amount) + var quote = message.Amount switch { - case 1: + 1 => // Well, they asked for it... :-) - quote = "'"; - break; - - case 2: - quote = "\""; - break; - - default: - // We have to return a response. - quote = null; - break; - } + "'", + 2 => "\"", + _ => null + }; return new QuoteResponseMessage { Quote = quote }; } + #pragma warning restore CA1822 } } diff --git a/Examples/07-ParallelizationTest/ParallelizationMessageController.cs b/Examples/07-ParallelizationTest/ParallelizationMessageController.cs index 66377a7..209ba39 100644 --- a/Examples/07-ParallelizationTest/ParallelizationMessageController.cs +++ b/Examples/07-ParallelizationTest/ParallelizationMessageController.cs @@ -16,9 +16,11 @@ namespace _07_ParallelizationTest } + #pragma warning disable IDE0060 // Remove unused parameter public async Task HandleSpeedTestMessage(SpeedTestMessage message) { await messageParallelization.WaitForBatch(); } + #pragma warning restore IDE0060 } } diff --git a/Examples/07-ParallelizationTest/Program.cs b/Examples/07-ParallelizationTest/Program.cs index 99917d2..4464a55 100644 --- a/Examples/07-ParallelizationTest/Program.cs +++ b/Examples/07-ParallelizationTest/Program.cs @@ -105,8 +105,8 @@ namespace _07_ParallelizationTest private readonly Func done; private readonly Action timeout; private int count; - private readonly object waitLock = new object(); - private TaskCompletionSource batchReachedTask = new TaskCompletionSource(); + private readonly object waitLock = new(); + private TaskCompletionSource batchReachedTask = new(); private Timer messageExpectedTimer; private readonly TimeSpan messageExpectedTimeout = TimeSpan.FromMilliseconds(5000); @@ -124,7 +124,7 @@ namespace _07_ParallelizationTest lock (waitLock) { if (messageExpectedTimer == null) - messageExpectedTimer = new Timer(state => + messageExpectedTimer = new Timer(_ => { timeout(count); }, null, messageExpectedTimeout, Timeout.InfiniteTimeSpan); diff --git a/Examples/08-MessageHandlerLogging/Program.cs b/Examples/08-MessageHandlerLogging/Program.cs index 2937dd9..3066d50 100644 --- a/Examples/08-MessageHandlerLogging/Program.cs +++ b/Examples/08-MessageHandlerLogging/Program.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using ExampleLib; using Messaging.TapetiExample; using Serilog; -using Serilog.Events; using SimpleInjector; using Tapeti; using Tapeti.Serilog; diff --git a/Tapeti.Flow/Default/FlowContinuationMiddleware.cs b/Tapeti.Flow/Default/FlowContinuationMiddleware.cs index e516667..9acbb99 100644 --- a/Tapeti.Flow/Default/FlowContinuationMiddleware.cs +++ b/Tapeti.Flow/Default/FlowContinuationMiddleware.cs @@ -1,5 +1,4 @@ using System; -using System.Reflection; using System.Threading.Tasks; using Tapeti.Config; using Tapeti.Flow.FlowHelpers; diff --git a/Tapeti.Flow/Default/FlowProvider.cs b/Tapeti.Flow/Default/FlowProvider.cs index 8b6d416..808ccc2 100644 --- a/Tapeti.Flow/Default/FlowProvider.cs +++ b/Tapeti.Flow/Default/FlowProvider.cs @@ -325,7 +325,7 @@ namespace Tapeti.Flow.Default } - public IFlowParallelRequestBuilder InternalAddRequest(object message, Delegate responseHandler) + private IFlowParallelRequestBuilder InternalAddRequest(object message, Delegate responseHandler) { requests.Add(new RequestInfo { diff --git a/Tapeti.Flow/Tapeti.Flow.csproj b/Tapeti.Flow/Tapeti.Flow.csproj index e5eb6b8..5f4cf71 100644 --- a/Tapeti.Flow/Tapeti.Flow.csproj +++ b/Tapeti.Flow/Tapeti.Flow.csproj @@ -17,6 +17,11 @@ 1701;1702 + + + IDE0066 + + diff --git a/Tapeti/Config/ControllerMessageContextPayload.cs b/Tapeti/Config/ControllerMessageContextPayload.cs index 700d8a3..7666433 100644 --- a/Tapeti/Config/ControllerMessageContextPayload.cs +++ b/Tapeti/Config/ControllerMessageContextPayload.cs @@ -7,7 +7,7 @@ public class ControllerMessageContextPayload : IMessageContextPayload { /// - /// An instance of the controller referenced by the binding. Note: can be null during Cleanup. + /// An instance of the controller referenced by the binding. Note: can be null during Cleanup or when bound to static methods. /// public object Controller { get; } diff --git a/Tapeti/Connection/TapetiClient.cs b/Tapeti/Connection/TapetiClient.cs index 17ccd30..c465944 100644 --- a/Tapeti/Connection/TapetiClient.cs +++ b/Tapeti/Connection/TapetiClient.cs @@ -663,7 +663,7 @@ namespace Tapeti.Connection } catch (WebException e) { - if (!(e.Response is HttpWebResponse response)) + if (e.Response is not HttpWebResponse response) throw; if (!TransientStatusCodes.Contains(response.StatusCode)) @@ -714,7 +714,7 @@ namespace Tapeti.Connection ? publishChannelModel : consumeChannelModel; - if (channel != null && channel.IsOpen) + if (channel is { IsOpen: true }) return channel; } diff --git a/Tapeti/Default/JsonMessageSerializer.cs b/Tapeti/Default/JsonMessageSerializer.cs index 4ca8ae4..bad6c47 100644 --- a/Tapeti/Default/JsonMessageSerializer.cs +++ b/Tapeti/Default/JsonMessageSerializer.cs @@ -49,7 +49,7 @@ namespace Tapeti.Default /// public object Deserialize(byte[] body, IMessageProperties properties) { - if (properties.ContentType == null || !properties.ContentType.Equals(ContentType)) + if (properties.ContentType is not ContentType) throw new ArgumentException($"content_type must be {ContentType}"); var typeName = properties.GetHeader(ClassTypeHeader); diff --git a/Tapeti/Helpers/ConnectionstringParser.cs b/Tapeti/Helpers/ConnectionstringParser.cs index f49f35a..c872a79 100644 --- a/Tapeti/Helpers/ConnectionstringParser.cs +++ b/Tapeti/Helpers/ConnectionstringParser.cs @@ -1,5 +1,7 @@ using System.Text; +// ReSharper disable UnusedMember.Global - public API + namespace Tapeti.Helpers { /// diff --git a/Tapeti/TapetiConfig.cs b/Tapeti/TapetiConfig.cs index ec2234c..8d225e8 100644 --- a/Tapeti/TapetiConfig.cs +++ b/Tapeti/TapetiConfig.cs @@ -189,7 +189,7 @@ namespace Tapeti /// protected void RegisterDefaults() { - if (!(DependencyResolver is IDependencyContainer container)) + if (DependencyResolver is not IDependencyContainer container) return; if (ConsoleHelper.IsAvailable()) diff --git a/Tapeti/TapetiConfigControllers.cs b/Tapeti/TapetiConfigControllers.cs index 9573512..f2890de 100644 --- a/Tapeti/TapetiConfigControllers.cs +++ b/Tapeti/TapetiConfigControllers.cs @@ -79,7 +79,7 @@ namespace Tapeti } var methodQueueInfo = GetQueueInfo(method) ?? controllerQueueInfo; - if (methodQueueInfo == null || !methodQueueInfo.IsValid) + if (methodQueueInfo is not { IsValid: true }) throw new TopologyConfigurationException( $"Method {method.Name} or controller {controller.Name} requires a queue attribute"); diff --git a/Tapeti/TapetiConnection.cs b/Tapeti/TapetiConnection.cs index f26e3ca..6b9ef0a 100644 --- a/Tapeti/TapetiConnection.cs +++ b/Tapeti/TapetiConnection.cs @@ -164,7 +164,7 @@ namespace Tapeti var reconnectedEvent = Reconnected; if (reconnectedEvent != null) - Task.Run(() => reconnectedEvent?.Invoke(this, e)); + Task.Run(() => reconnectedEvent.Invoke(this, e)); } ///