question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implementation of IHandle<T> for synchronous handlers

See original GitHub issue

In earlier versions of Caliburn Micro, we used to have separate implementations of IHandle (and IHandleWithTask). However, in 4.x, this seems to be changed and replaced with a single interface.

public interface IHandle<TMessage>
{
       Task HandleAsync(TMessage message, CancellationToken cancellationToken);
}

One issue I find with this approach is that in the scenarios when there are no asynchronous calls in the handlers, the naming convention looks out of place with the Async suffix. The code style checkers would show these as warnings as well, leaving a clutter of warnings in the solution.

Wouldn’t it be better to bring back the IHandleWithTask to separate the different implementations ?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Jammyocommented, Oct 15, 2021

I don’t think putting both functions into IHandle is an option because then users are forced to implement one no-op for each handler (unless I misunderstood the suggestion?). To the original point, in scenarios with no asynchronous calls, code style checkers should only show a warning if the async keyword is used. Setting the handler as the following should work fine:

public Task HandleAsync(MyEvent @event, CancellationToken cancellationToken)
{
    // Handle event
    return Task.CompletedTask;
}
0reactions
vb2aecommented, Oct 24, 2021

C#8 has support to add default implementations to methods. Updated the IHandle interface to add have default implementation for .net 5, .net 6, and .net core 3.1 #780

Read more comments on GitHub >

github_iconTop Results From Across the Web

Synchronous implementation of interface that returns Task
So let's suppose that implementation looks like this: public class SimplyAwesome : IAmAwesome { public async Task MakeAwesomeAsync() { // Some ...
Read more >
Walkthrough: Creating an Asynchronous HTTP Handler
This walkthrough illustrates how to create an asynchronous HTTP handler. Asynchronous HTTP handlers enable you to start an external process ...
Read more >
Code Tip: How to work with asynchronous event handlers in C ...
This post describes how you can add support for asynchronous event handlers when developing class in C#.
Read more >
Synchronous and asynchronous requests - Web APIs | MDN
Synchronous requests block the execution of code which causes "freezing" on the screen and an unresponsive user experience. Asynchronous request.
Read more >
Asynchronous Handlers • NServiceBus
Handlers and sagas are executed by threads from the thread pool. Depending on the transport implementation the worker thread pool thread or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found