pardahlman / RawRabbit

A modern .NET framework for communication over RabbitMq
MIT License
746 stars 144 forks source link

How create a queue ? #281

Closed karimflaction closed 6 years ago

karimflaction commented 6 years ago

Hello,

I'm doing a project and I don't know how create a queue... I know how receive a queue.

I tested this : return BusClientFactory.CreateDefault( ioc => ioc.AddSingleton<IConfigurationEvaluator, AttributeConfigEvaluator>());

But I cannot specify my configuration if I do this

My configuration : _config = new RawRabbitConfiguration { Username = "guest", Password = "guest", Port = 32780, VirtualHost = "vhost", Hostnames = { "rabbit-mq" } };

I have too a class with my queue : [Queue(Name = "test_queue", Durable = false)] [Exchange(Name = "test_exchange", Type = RawRabbit.Configuration.Exchange.ExchangeType.Topic)] [Routing(RoutingKey = "test_key", NoAck = true)] public class BasicMessage { public string Message { get; set; } }

How can I create a queue and set a configuration ?

Somebody can help me ?

Thank you,

pardahlman commented 6 years ago

Hello @karimflaction,

All topology features needed to perform an operation (such as publish or subscribe) will be created when performing the operation. So, for your example, the exchange test_exchange and test_queue will be declared when a message of type BasicMessage is subscribed to.

It looks like you are using version 1.10.x of the client, so you could look at the sample projects here: https://github.com/pardahlman/RawRabbit/tree/stable/sample. There is also documentation available here http://rawrabbit.readthedocs.io/en/master/Getting-started.html#messaging-pattern

Hope this helps!

karimflaction commented 6 years ago

Hello @pardahlman

Tank you for you answer ;) I had already read the documentation. I followed your example and I did this :

image

I have my configuration in the file .json but as you can see I have a problem, the method .SetBasePath and .AddJsonFile doesn't work.

Do you have an idea ?

Thank you

karimflaction commented 6 years ago

Oh I found this problem myself :)

But I have an another problem, when I start the application stop when I call the json file. I have this error :

_System.AggregateException occurred HResult=0x80131500 Message=One or more errors occurred. (One or more errors occurred. (The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text="PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'test_queue_testrabbitmq_receiver' in vhost 'vhost': received 'false' but current is 'true'", classId=50, methodId=10, cause=)) Source= StackTrace: at System.ThrowHelper.ThrowAggregateException(List1 exceptions) at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken) at RawRabbit.Operations.Subscriber1.SubscribeAsync[T](Func3 subscribeMethod, SubscriptionConfiguration config) at RawRabbit.Common.BaseBusClient1.SubscribeAsync[T](Func3 subscribeMethod, Action1 configuration) at TestRabbitMq.Receiver.ConsoleAppRunner.d0`1.MoveNext() in C:_TestRabbitMq-RawRabbit\rawrabbit-playground\TestRabbitMq.Receiver\ConsoleAppRunner.cs:line 28 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.Internal.WebHost.d26.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.WebHostExtensions.d5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.WebHostExtensions.d4.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host) at TestRabbitMq.Receiver.Program.Main(String[] args) in C:_TestRabbitMq-RawRabbit\rawrabbit-playground\TestRabbitMq.Receiver\Program.cs:line 35

Inner Exception 1: AggregateException: One or more errors occurred. (The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text="PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'test_queue_testrabbitmq_receiver' in vhost 'vhost': received 'false' but current is 'true'", classId=50, methodId=10, cause=)

Inner Exception 2: OperationInterruptedException: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text="PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'test_queue_testrabbitmqreceiver' in vhost 'vhost': received 'false' but current is 'true'", classId=50, methodId=10, cause=

Can you help me ?

If you must have something to understand my problem i can give you all :)

pardahlman commented 6 years ago

Hello again, Reading the exception closely it says: _"PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'test_queue_testrabbitmqreceiver' in vhost 'vhost': received 'false' but current is 'true'". This means that the queue test_queue_testrabbitmq_receiver is already declared in the broker. If you are new to RabbitMQ I suggest that you take a look at the C# tutorial that explains some basic concepts.

karimflaction commented 6 years ago

Thank you for your help :)

I have a last question. I create my queue and I receive it, but it create a lot of connections and I don't understand why.

This is my code to receive :

image

But it create a lot of connections and channel like this :

image

How can I do to have one channel for a queue ? Thank you :)

pardahlman commented 6 years ago

It is difficult to answer that question based on your code snippet, is RabbitHelper a wrapper class? I'm guessing that you are creating multiple instances of IBusClient, which should be registered as a singleton if resolved from the factory.

karimflaction commented 6 years ago

Yes,

That is my class :

image

I change this : image

I add the var test but it changed nothing

My BasiscMessage class image

And my configuration in .json image

pardahlman commented 6 years ago

What you are actually doing is setting up multiple subscriptions within your while-loop. If you want to perform multiple operations at the same time, have a look at these tests: https://github.com/pardahlman/RawRabbit/blob/2.0/test/RawRabbit.IntegrationTests/PublishAndSubscribe/MultipleOperationsTests.cs#L11

Note that the setup only does SubscribeAsync once.

karimflaction commented 6 years ago

I test it like that:

image

But nothing :(

pardahlman commented 6 years ago

Yeah, so what I'm saying is: check how it is done in the tests I pointed to. If you have SubscribeAsync within an infinite while loop, you are going to get multiple consumers.