从 WCF 服务调用基于任务的异步单向回调方法

Iva*_*oev 3 c# wcf asynchronous callback oneway

我有一个 WCF 服务,它使用回调合同来通知客户端,类似于这个

public interface IClientCallback
{
    [OperationContract(IsOneWay = true)]
    void NotifySomething();
}
Run Code Online (Sandbox Code Playgroud)

和调用它的服务代码与此类似

void NotifySomething()
{
    try
    {
        this.callback.NotifySomething();
    }
    catch (Exception ex)
    {
        // Log the exception and eat it
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,通过设计回调通知是可选的,即很高兴拥有,但不是必需的。这就是为什么它被标记为OneWay并且实现会处理异常。

由于一些误解,我们认为这足以让我们拥有一个非阻塞的开火即忘方法。但当然这不是真的,所以在某些情况下它会阻塞一段时间,这会导致问题,因为它是从线程同步块内部调用的。所以我们决定通过如下更改定义使其异步

public interface IClientCallback
{
    [OperationContract(IsOneWay = true)]
    Task NotifySomething();
}
Run Code Online (Sandbox Code Playgroud)

我对客户端实现没有问题,我的问题是如何从服务中调用它。这是我想要做的

async void NotifySomething()
{
    try
    {
        await this.callback.NotifySomething();
    }
    catch (AggregateException ex)
    {
        // Unwrap, log the exception(s) and eat it
    }
    catch (Exception ex)
    {
        // Log the exception and eat it
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,既然大家都说async void这不是一个好习惯,那么在这里使用它可以吗?我还有什么其他选择?在 WCF 服务上下文中执行此操作的推荐方法是什么?

Ned*_*nov 5

您编写它的方式非常安全,因为它可以处理异常。您还可以编写一个可重用的扩展方法来做到这一点,这样您就不需要重复它了。

也许是这样的:

public static class Extensions
{
    public static void FireAndForget(this Task task)
    {
        task.ContinueWith(t => 
        {
            // log exceptions
            t.Exception.Handle((ex) =>
            {
                Console.WriteLine(ex.Message);
                return true;
            });

        }, TaskContinuationOptions.OnlyOnFaulted);
    }
}

public async Task FailingOperation()
{
    await Task.Delay(2000);

    throw new Exception("Error");
}   

void Main()
{
    FailingOperation().FireAndForget();

    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)