SyndiMessage 1.0.11

dotnet add package SyndiMessage --version 1.0.11
                    
NuGet\Install-Package SyndiMessage -Version 1.0.11
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SyndiMessage" Version="1.0.11" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyndiMessage" Version="1.0.11" />
                    
Directory.Packages.props
<PackageReference Include="SyndiMessage" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SyndiMessage --version 1.0.11
                    
#r "nuget: SyndiMessage, 1.0.11"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SyndiMessage@1.0.11
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SyndiMessage&version=1.0.11
                    
Install as a Cake Addin
#tool nuget:?package=SyndiMessage&version=1.0.11
                    
Install as a Cake Tool

N|Solid

SyndiMessage

SyndiMessage was developed for easy usage of RabbitMq.Client main library of RabbitMq. Using this project, you will enjoy publishing and consuming messages.

Usage

Adding Broker

To use this library, the first thing you should do is inject the broker configuration that is beign used. However, don't forget that the class being injected must inherit from the RabbitMqConfig class.

After that, you must meet this configuration using the following method :


serviceCollection.AddBroker(broker);

AddBroker method is used to configure brokers in the library.

If you need to use multiple brokers, you can use this extension method as many times as you want to configure each broker.

Publishing Message

"Now that I know you're excited about publishing messages, there's just one last step remaining.


serviceCollection.AddPublish();

After adding the Publisher specification to your application, you should define your message type.


[MessageDeclaration(queueName:"SyndiQueue",exchangeName:"SyndiExchange",routingKey:"",exchangeType:ExchangeTypes.topic,prefetchCount:0)]
public class SyndiMessage
{
    public string Hello { get; set; }
}

Also you can add BindingDeclarationAttribute to your message type like MessageDeclarationAttribute. This attribute will set up binding configurations which are those AutoDelete,AutoAck,Exclusive,Durable.

For example ,in the above, we added new class named SyndiMessage and use MessageDeclaration Attribute to specify information that it will be going to.

Now you can publish your message to any broker you want.

To publish a message, you should inject IPublish instance and use the PublishMessage or PublishMessages methods.

We wrote a console app as an example of publishing using a provider, but you can inject an IPublish instance into constructors, methods, etc.

using Microsoft.Extensions.DependencyInjection;
using PublishTest;
using SyndiMessage;
using SyndiMessage.Contracts;

ServiceCollection serviceCollection = new ServiceCollection();

SyndiBroker broker = new SyndiBroker("localhost",5672,"syndi","syndi");

SyndiMessageInstance syndiMessageInstance = new SyndiMessageInstance(AppDomain.CurrentDomain.FriendlyName);

serviceCollection.AddBroker(broker).AddPublisher();

var provider = serviceCollection.BuildServiceProvider();

var publisher = provider.GetRequiredService<IPublish<SyndiBroker>>();

publisher.PublishMessage(syndiMessageInstance);

That is all.

Consuming messages

As you saw message publishing is quite easy. Consumer might been seen more complex than publishing but don't be afraid of. We are all here to assist you 😃.

As you did at publishing message, you need a message type that specifies message information.

After that, you will need a consumer class that inherits Consumer<TMessage,TBroker>. For example :


public class SyndiConsumer : Consumer<SyndiMessageInstance, SyndiBroker>
    {
        public SyndiConsumer(IModelGenerator<SyndiBroker> model) : base(model)
        {
        }

        public override async Task Handle()
        {
            await Task.FromResult(Message.FirstMessage);
        }
    }

Consumer<TMessage,TBroker> base class constructor receives an instance of IModelGenerator<SyndiBroker> instance as a parameter, which you have aldready injected it using the AddBroker method.

If you want to add your services, etc., you can inject them using the constructor.

Now you process your message in Handle method using Message property. Message property type is TBroker that you choose already. You don't need to Deserialize the consumed message.

Now you need an instance of your consumer. If you are writing a console application and not using IoC for your services, you must create instances of your services and consumer.

In our opinion, the best practice for creating a Consumer application is to build a worker application. In a worker application, you can use the Reflection library to instantiate your consumers.

For example :

var consumers = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName.StartsWith("xxx.Consumer")).First().GetTypes().Where(x=>x.BaseType!.IsGenericType && x.BaseType!.GetGenericTypeDefinition()==typeof(Consumer<,>) ).ToList();
var ctorInfos = consumers.Select(x =>
{
    return x.GetConstructors().Single();
});
object current = null;
foreach (ConstructorInfo info in ctorInfos)
{
    object[] parameters = new object[info.GetParameters().Length];
    for (int a = 0; a < info.GetParameters().Length; a++)
    {
        parameters[a] = _serviceProvider.GetService(info.GetParameters()[a].ParameterType);
    
    current = info.Invoke(parameters);
}

Logging

SyndiMessage uses the ILogger interface. If you wish to use another logging library, you should configure it in accordance with the ILogger.

Any exception that occurs is handled in the Consumer base class. It is logged using Log.LogError(). In the Publish interface, there is no any exception handler, so you will need to handle errors yourself.

Retry message

If an exception occurs in Consumer, the consumed message is not lost. The consumer base class has RetryCount property for sake of retry message to queue. The default value of this property is 3, but you can adjust it to any value greater than 0. If you don't want to retry the message, you can set it to 1.

For example:


public class SyndiConsumer : Consumer<SyndiMessageInstance, SyndiBroker>
{
    public SyndiConsumer(IModelGenerator<SyndiBroker> model) : base(model)
    {
        RetryCount = 1;
    }
}

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.11 249 4/19/2024
1.0.10 214 4/3/2024
1.0.9 197 4/2/2024
1.0.8 204 4/2/2024
1.0.7 230 12/16/2023
1.0.5 208 12/16/2023
1.0.4 199 12/16/2023
1.0.3 265 10/25/2023
1.0.2 177 10/13/2023
1.0.1 206 9/28/2023
1.0.0 203 9/28/2023