Before I start, the closest I could find for this here was How do you implement an async action delegate method? however, being a bear of a somewhat small brain, I couldn't get my braincells around the answer and how it might apply to my current issue.
I'm writing a wrapper around the RabbitMq C# Client, and am having a problem with my delegates and the async events.
This bit of code seems fine:
private Action<object, BasicAckEventArgs> _syncHandlerBasicAck;
private Action<object, BasicNackEventArgs> _syncHandlerBasicNack;
Defining the code here:
_syncHandlerBasicAck = delegate(object o, BasicAckEventArgs args)
{
//[Do stuff here]
};
_syncHandlerBasicNack = delegate(object o, BasicNackEventArgs args)
{
//[Do stuff here]
};
then using it with:
Channel.BasicAcks += _syncHandlerBasicAck.Invoke;
Channel.BasicNacks += _syncHandlerBasicNack.Invoke;
The issue I have is with the async BasicDeliver
event. This is my current code:
private Func<object, BasicDeliverEventArgs, Task> _syncHandlerIncomingMessage;
followed by
_syncHandlerIncomingMessage = async delegate(object o, BasicDeliverEventArgs args)
{
//[Do stuff here]
};
however whenever I try to do:
Consumer.Received += _syncHandlerIncomingMessage.Invoke;
I get a System.NullReferenceException
telling me that
Object reference not set to an instance of an object.
I see that there's a BeginInvoke
method available but I'm not sure that applies to my current situation since I'm not using Callbacks and just want to call this asynchronously.
Anyone got any pointers? (I'll even take "Duplicate question: Answer here" type responses as I'm sure I may have missed something).
Edit
I had two issues; one with wiring this up, the other where some refactoring got in my way! Thanks so much to @Coolbots for the answer and @Camilo to show me the error of my ways.