I have a method that takes:
public delegate void SubscriberAction(string a, string b = null);
My method
public void Subscribe(SubscriberAction action)
{
var consumer = new EventingBasicConsumer(Channel);
consumer.Received += (model, eventArgs) =>
{
try
{
var body = eventArgs.Body;
action?.Invoke(Encoding.UTF8.GetString(body), eventArgs.RoutingKey);
// The above works when I call it with one or two strings
}
finally
{
Channel.BasicAck(eventArgs.DeliveryTag, false);
}
};
Channel.BasicConsume(Configuration.QueueName, false, consumer);
}
When I invoke this action as shown above it does recognize the optional parameter and I can call it as intended. However, the Subscribe
method that takes this delegate parameter won't let me call it how I want.
So this won't work:
subscriber.Subscribe(x =>
{
// do stuff
});
Only works when I do this
subscriber.Subscribe((x, y) =>
{
// do stuff
});
Initially I was using Action<string> action
as the parameter for Subscribe
but needed to add an optional second string parameter and since Action does not allow optional parameters (correct me if I'm wrong), I found an answer saying to use a custom delegate as I have done above.
How can I call the Subscribe
method with either one or both parameters?