如何使 WCF RESTful 服务异步工作?

问题描述:

我正在构建 WCF 休息服务,它是客户端.我的计划是,那个客户对服务了解不多,只知道调用方法和预期结果的正确 URL.

I'm building WCF rest service and it's client. I plan, that client does not know much about the service, just right URL's to call methods and expected results.

我的服务合同是:

[WebInvoke(Method="POST", UriTemplate="/tasks")]
[OperationContract]
void SubmitTask(Transaction task);

[WebGet(UriTemplate = "/tasks/{taskId}")]
[OperationContract]
[XmlSerializerFormat]
Transaction GetTask(string taskId);

SubmitTask 是这样实现的:

SubmitTask is realized like:

SubmitTask(Transaction task)
{
   DoSomethingWithTask(task);
   task.Status = "SomeStatus";
   DoSomethingElseWithTaks(task);
   task.Status = "SomeOtherStatus";
}

我对客户的期望:

ButtonClick()
{
   SubmitTask(task);
   while(true)
   {
      string status = Transaction GetTask(task.taskId).Status;
      Textbox.Text+= status;
      if(status==ok)
         break;
      Thread.Sleep(1000); 
   }
}

问题是-服务端没有执行GetTask,而所有SubmitTask操作都完成了,所以我只在客户端得到最后一个任务状态.这种情况下如何实现异步操作?

The problem is - GetTask is not performed on service side, while all SubmitTask operations are completed, so I get only last task status on client side. How to realize asynchronos operation performing in this situation?

提前致谢!

您是否阅读过这篇有趣的小文章?调整 WCF 以构建高度可扩展的异步REST API 和以下非常好的文章,希望能提供您想要的答案 修复 WCF 以构建高度可扩展的异步 REST API

Have you read this interesting little article? Tweaking WCF to build highly scalable async REST API and the following article that is very good and which will hopefully provide the answer you desire Fixing WCF to build highly scalable async REST API