如何使 WCF图像上传服务异步而非同步。我的SaveImage
函数直接来自异步javascript调用。
合同(IImageWCFService.cs):
[ServiceContract(Namespace = "MyServices.org")]
public interface IImageWCFService
{
[OperationContract(AsyncPattern = true)]
[WebInvoke(Method = "POST", ResponseFormat=WebMessageFormat.Json, UriTemplate="SaveImage")]
void SaveImage(Stream stream);
}
服务(ImageWCFService.svc.cs):
public void SaveImage(Stream stream)
{
// long running task up manipulating the image
// some lines of code contains image upload to Amazon S3 functions
}
我返回void,因为这应该是单向调用,用户不需要从函数中获取任何结果或是否成功。
现在,当此WCF服务运行时,它将使用主线程。我希望它以异步方式运行。我读过推荐的方法是使用:
[OperationContract(AsyncPattern = true)]
或 WCF的基于任务的异步模式。
我读了很多文章,但仍然很困惑如何在我的函数中实现它。我需要一个代码示例来显示我的图像上传功能的实现。
注意:关于各种异步实现in this question之间差异的一个很好的来源 - 它表示实现它的最佳方法是使用基于任务的WCF异步模式。
同样,调用是直接从JavaScript进行的,但我没有问题创建一个web服务(asmx),如果这是它需要的那个将在服务器端调用WCF服务的保存服务器,但是我再次,我是对这一切感到困惑。
感谢。