Home > Enterprise >  How to avoid warnings for Caliburn Micro Event Aggregator HandleAsync method names?
How to avoid warnings for Caliburn Micro Event Aggregator HandleAsync method names?

Time:10-08

My problem is simply that if I use Caliburn Micro's Event Aggregator, and implement the interface "IHandle", then that creates a method called "HandleAsync(MyEvent message, CancelleationToken token)".

My problem is that typically those methods are not asynchronous, and don't need marked with "async", or return a Task, therefore really shouldn't have "Async" on the end as they're not asynchronous methods, and raises a warning in Visual Studio.

This isn't causing me any technical problems, it's just irritating to name methods incorrectly just to abide by Caliburn Micro's interface for event aggregator subscription methods.

Simple question is how to either remove the Async from the end of the method name, or otherwise get rid of these warnings?

Thanks

Garry

CodePudding user response:

Unfortunately that is not possible with the current implementation of EventAggregator in Caliburn Micro.

If you were to check the implementation of EventAggregator in the github repository, you would notice that it is searching for method with the specific name.

var interfaces = handler.GetType().GetTypeInfo().ImplementedInterfaces
                    .Where(x => x.GetTypeInfo().IsGenericType && x.GetGenericTypeDefinition() == typeof(IHandle<>));

foreach (var @interface in interfaces)
{
  var type = @interface.GetTypeInfo().GenericTypeArguments[0];
  var method = @interface.GetRuntimeMethod("HandleAsync", new[] { type, typeof(CancellationToken) });

  if (method != null)
  {
    _supportedHandlers[type] = method;
  }
}

One alternative would be to implement your own version of IEventAggregator and introduce two interfaces IHandle<T> and IHandleWithTask<T> to represent synchronous and asynchronous variants.

CodePudding user response:

You should be able to suppress the warning in your source code.

Right-click on it in the error list and choose Suppress->In Source or Suppress->In Suppression File depending on where you want to insert the #pragma directive.

  • Related