Skip to content

ProducerMethod

blehnen edited this page Nov 10, 2026 · 2 revisions

Producing Method Expressions

You can send lambda expressions for execution.

Send a compiled lambda expression
queue.Send((message, workerNotification) => Console.WriteLine(message.MessageId.Id.Value));
Creating the queue

You must specify the transport that you want to use. Unlike the typed-message producer, you do not need to specify the type when you create the queue. Any method producer queue can send any lambda expression.

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

If you dispose of the container, it also disposes all queues that it created. If you send many expressions over time, you should keep the container and queues instead of creating new ones each time.

The send commands are thread safe.

You can send method expressions in a few different ways.

A single expression with no additional message metadata
var result = queue.Send((message, workerNotification) => Console.WriteLine("Hello World"));
A single expression 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 expressions 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 a collection of messages return a collection of results. The result collection has a boolean named 'HasErrors' that shows if any result failed.

For a complete working example, see SQLServerProducerLinq in the samples repository.

Clone this wiki locally