从WCF服务调用基于任务的异步单向回调方法 [英] Calling a task-based asynchronous one way callback method from WCF service

查看:154
本文介绍了从WCF服务调用基于任务的异步单向回调方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WCF service which uses a callback contract to notify the clients, similar to this

public interface IClientCallback
{
    [OperationContract(IsOneWay = true)]
    void NotifySomething();
}

以及与此类似的服务代码

and the service code calling it similar to this

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

请注意,根据设计,回调通知是可选,即很高兴,但不是必需的.这就是为什么将其标记为OneWay且该实现会吃掉异常的原因.

Note that by design the callback notification is optional, i.e. nice to have, but not required. That's why it's marked as OneWay and the implementation eats exceptions.

由于某些误解,我们认为采用非阻塞性的忘却"方法就足够了.但是当然那不是真的,因此在某些情况下它会阻塞一段时间,这会引起问题,因为它是从内部线程同步块中调用的.因此,我们决定通过如下更改定义来使其异步

Due to some misunderstanding we were thinking that would be enough for having a non blocking fire and forget method. But of course that wasn't true, so under some circumstances it's blocking for a while, which is causing problems because it's called from inside thread synchronized block. So we decided to make it asynchronous by changing the definition as follows

public interface IClientCallback
{
    [OperationContract(IsOneWay = true)]
    Task NotifySomething();
}

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

I have no problem with client implementation, my question is how to call it from the service. Here is what I'm thinking to do

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
    }
}

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

Now, since everyone is saying async void is not a good practice, is it ok to use it here? What other options do I have? What is the recommended way to perform this in the WCF service context?

推荐答案

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

The way you have written it is pretty safe as it handles the exceptions. You could also write a reusable extension method to do that so that you don't need to repeat it.

也许是这样的:

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();
}

这篇关于从WCF服务调用基于任务的异步单向回调方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆