Reputation: 2527
I've been going through a tutorial on how to use the RabbitMQ client for C#. I'm trying to send a hello world message to the server and also receive it on my end so that I know the transmission was successful. The tutorial gave the following class file definitions for Send and Receive classes:
Send.cs
using System;
using RabbitMQ.Client;
using System.Text;
class Send
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "hello",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
Receive.cs
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
class Receive
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
};
channel.BasicConsume(queue: "hello",
noAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
When I run the project (in visual studio), I'm getting the error that there is more than one entry point that has been defined which is being caused by the fact that both classes have main()
methods. I tried specifying a startup project so that it knows which main to use but I didn't see an option for it.
I'm not sure which main should be used as both files were intentionally structured this way in the tutorial. I'm somewhat rusty with C# as I've been programming mostly in C++ more recently.
Is it assumed that I should have a main file where I instantiate both classes? If not, how should the project be structured?
Upvotes: 2
Views: 3449
Reputation: 72858
The send and receive code were not meant to be in the same C# project.
Although it is not explicitly called out in the sample, these should be separate projects and separate executables.
Split send.cs and receive.cs into their own projects. Run the send project first. You'll see the message sitting in your queue in the rabbitmq server.
After that, run the receive project. At this point, you'll see the message is consumed, as expected.
Upvotes: 3
Reputation: 10202
Disclamer: I don't know C#.
First: This is really basic programming. You can't have two entry points, otherwise known in some languages (like java and c++) as "the main function", in any program. I'm not going to elaborate this any further because as I said, that is pretty basic stuff.
Second: The tutorial on the link you provided in the question WORKS. Just read it follow it and that is it.
Third: If you want to have this as one program here is the code (yes, I know that I don't know c#, but I'll make a leap of faith):
using System;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
class SendReceive
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
};
channel.BasicPublish(exchange: "",
routingKey: "hello",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
channel.BasicConsume(queue: "hello",
noAck: true,
consumer: consumer);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
Addition: You mentioned in the comment that you can't connect in some scenario, and like @VivekChavda said that's another question.
Upvotes: -1