The AsyncEventHandler attribute can only be used under the following conditions:
- The attribute only applies to methods.
- The method must be declared within a class (or record/struct) and a namespace.
- The method must be async, have a
Taskreturn type, and ends withAsync. - The
Task<T>return type is not supported. - The method can have parameters, but no modifier is allowed (e.g.,
ref,out,in,params). - Supported attribute arguments are
WaitUntilCompletionandUseDispatcheronly. - An empty list of attribute arguments
()is not supported.
[AsyncEventHandler()] // CSL1016: Unsupported use of the 'AsyncEventHandler' attribute.
public Task FooAsync() // Not 'async'.
{
await Task.Delay(0);
}[AsyncEventHandler()] // CSL1016: Unsupported use of the 'AsyncEventHandler' attribute.
public async void FooAsync() // Not 'Task' return type.
{
await Task.Delay(0);
}[AsyncEventHandler()] // CSL1016: Unsupported use of the 'AsyncEventHandler' attribute.
public async Task FooAsynchronous() // Not ending with 'Async'.
{
await Task.Delay(0);
}[AsyncEventHandler()] // CSL1016: Unsupported use of the 'AsyncEventHandler' attribute.
public async Task<int> FooAsync() // 'Task<T>' return type.
{
return await Task.FromResult(0);
}[AsyncEventHandler] // CSL1016: Unsupported use of the 'AsyncEventHandler' attribute.
public async Task FooAsync(ref object sender) // Modifier (here, 'ref').
{
await Task.Delay(0);
}[AsyncEventHandler()] // CSL1016: Unsupported use of the 'AsyncEventHandler' attribute.
public async Task FooAsync()
{
await Task.Delay(0);
}namespace MyNamespace;
public class MyClass
{
[AsyncEventHandler]
public async Task FooAsync()
{
await Task.Delay(0);
}
}