从 WCF 服务调用基于任务的异步单向回调方法
Calling a task-based asynchronous one way callback method from WCF service
我有一个 WCF 服务,它使用回调协定来通知客户端,类似于此
public interface IClientCallback
{
[OperationContract(IsOneWay = true)]
void NotifySomething();
}
调用它的服务代码类似这样
void NotifySomething()
{
try
{
this.callback.NotifySomething();
}
catch (Exception ex)
{
// Log the exception and eat it
}
}
请注意,根据设计,回调通知是可选,即很高兴拥有,但不是必需的。这就是为什么它被标记为 OneWay
并且实现会吃掉异常。
由于一些误解,我们认为这足以实现非阻塞的发射后忘记方法。但这当然不是真的,所以在某些情况下它会阻塞一段时间,这会导致问题,因为它是从线程同步块内部调用的。所以我们决定通过如下更改定义使其异步
public interface IClientCallback
{
[OperationContract(IsOneWay = true)]
Task NotifySomething();
}
我对客户端实现没问题,我的问题是如何从服务中调用它。这是我想做的
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 服务上下文中执行此操作的推荐方法是什么?
您编写它的方式非常安全,因为它可以处理异常。您还可以编写一个可重用的扩展方法来执行此操作,这样您就不需要重复它。
也许是这样的:
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 服务,它使用回调协定来通知客户端,类似于此
public interface IClientCallback
{
[OperationContract(IsOneWay = true)]
void NotifySomething();
}
调用它的服务代码类似这样
void NotifySomething()
{
try
{
this.callback.NotifySomething();
}
catch (Exception ex)
{
// Log the exception and eat it
}
}
请注意,根据设计,回调通知是可选,即很高兴拥有,但不是必需的。这就是为什么它被标记为 OneWay
并且实现会吃掉异常。
由于一些误解,我们认为这足以实现非阻塞的发射后忘记方法。但这当然不是真的,所以在某些情况下它会阻塞一段时间,这会导致问题,因为它是从线程同步块内部调用的。所以我们决定通过如下更改定义使其异步
public interface IClientCallback
{
[OperationContract(IsOneWay = true)]
Task NotifySomething();
}
我对客户端实现没问题,我的问题是如何从服务中调用它。这是我想做的
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 服务上下文中执行此操作的推荐方法是什么?
您编写它的方式非常安全,因为它可以处理异常。您还可以编写一个可重用的扩展方法来执行此操作,这样您就不需要重复它。
也许是这样的:
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();
}