Skip to content

ProducerLinq

Brian Lehnen edited this page Dec 8, 2020 · 2 revisions

Producing Linq statements

There are two options for sending linq statements for execution.

Send a compiled statement
queue.Send((message, workerNotification) => Console.WriteLine(message.MessageId.Id.Value));
Send a dynamic statement, expressed as a string
queue.Send(new LinqExpressionToRun("(message, workerNotification) => new TestClass().RunMe((IWorkerNotification)workerNotification, \"dynamic\", 2, new SomeInput(DateTime.UtcNow.ToString()))",
          new List<string> { "ProducerMethodTestingClasses.dll" }, //additional references
          new List<string> { "ProducerMethodTestingClasses" })); //additional using statements
Creating the queue

Creating the queue is pretty straightforward. You'll need to specify the transport you want to use. Unlike the POCO queues, you do not need to specify the type when creating the queue. Any producer queue can send any Linq statement.

var queueConnection = new QueueConnection(queueName, connectionString);
using (var queueContainer = new QueueContainer<InitModule>())
{
	using (var queue = queueContainer.CreateMethodProducer(queueConnection))
    {
		...
	}
}

If you dispose of the container, all queues created from the container will be disposed as well. If you will be sending many statements over time, you should hang on to the container/queues instead of constantly creating them.

The send commands are thread safe.

Statements can be sent in a few different ways.

A single statement with no additional message metadata
var result = queue.Send((message, workerNotification) => Console.WriteLine("Hello World"));
A single statement with additional metadata, such as a delay
var data = new AdditionalMessageData();
data.SetDelay(TimeSpan.FromMinutes(1));
var result = queue.Send((message, workerNotification) => Console.WriteLine("Hello World"), data);
A collection of statements with or without additional metadata. Note that this operation is not necessarily atomic
var result = queue.Send(messageList);
All of the above commands have async methods as well
var result = await queue.SendAsync((message, workerNotification) => Console.WriteLine("Hello World"));

All methods return the result of the operation. This includes the following:

  • A boolean indicating if an error occurred
  • An exception containing the error, if an error occurred.
  • Information about the sent message, assuming no errors -The message ID -The correlation ID

Methods that take in a collection of messages return a collection of results. The collection contains a boolean named 'HasErrors' that will indicate if any result failed.

Clone this wiki locally