-
-
Notifications
You must be signed in to change notification settings - Fork 811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a RaiseAsync
method for raising Func<..., Task>
events?
#1310
Comments
Your issue is essentially a duplicate of #1285. (You need to cast Given that you're also enquiring about a |
RaiseAsync
method for raising Func<..., Task>
events?
Quoting from #977 (comment):
|
Thanks very much @stakx, makes sense, this feature would be greatly appreciated. With regards to the in series vs parallel question, I'd be inclined to copy what the .net framework would do. That's not 100% clear to me in the case of events with a Task return type, but it would appear "in series" is the answer:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/ |
I think it would be a safe bet to execute them in series, for the reason you've mentioned. The downside being worse performance than if we executed them in parallel. I've given this some thought too today. I think it's generally good practice that multiple event handlers should not be interdependent on one another. They should therefore not have to make any assumptions about the order in which they get invoked (even though the .NET runtime pretty much guarantees that they will execute in the order in which they were subscribed to the event). So I think we could start by invoking all event handlers in parallel (which also happens to be easier to implement), but officially leave the order in which Makes sense? Btw. I've created a draft implementation of |
Thanks @stakx. I found this blog post interesting and might be useful for making your decision - https://blog.stephencleary.com/2013/02/async-oop-5-events.html, perhaps eventually you could give the consumer the option of in series vs parallel. It's also interesting that returning a Task from an event was / is debatable. To give some extra context I'm trying to work out the best way to write component tests that make use of Azure.Messaging.ServiceBus (the latest Azure Service Bus package), in particular the ServiceBusProcessor that has a public event Func<ProcessMessageEventArgs, Task> ProcessMessageAsync. It's already been pointed out that this library is harder to test. |
Thanks for the link to that blog post. Quoting the second solution mentioned there:
It's reassuring to see Stephen Cleary (who is very knowledgeable about all things async) suggest the exact same thing that I've already implemented as a draft. So we're probably on the right track! |
Given that C# and VB.NET's syntax for raising an event does not give you any way of retrieving the event handlers' individual return values, it's understandable — what would be the point of returning values when they're almost certain to be ignored / get dropped? Noone wants to tinker around with So much for the historical reason. Then the TPL was introduced in C# 4 and
The non-generic |
Let's do this! I'm leaving the exakt invocation order of subscribed event handlers (in series vs. in parallel) unspecified for now. |
E.g.
mock.Raise(x => x.ProcessMessageAsync += null, args);
throws aSystem.Reflection.TargetParameterCountException : Parameter count mismatch
. I noticeRaise
accepts anAction
rather thanFunc.
Is there aRaiseAsync
method or similar?The text was updated successfully, but these errors were encountered: