简体   繁体   English

ASP.NET MVC 4 Web API:如何异步处理需要从异步WCF服务回调中提取结果的请求

[英]ASP.NET MVC 4 Web API: How to Asynchronously Process a Request that Needs to Extract Results from an Asynchronous WCF Service Callback

Suppose I implemented a webservice using ASP.NET MVC 4 that has a controller that exposes one method: 假设我使用ASP.NET MVC 4实现了一个Web服务,该服务的控制器公开了一种方法:

Response Process(Request request);

This webservice communicates with a middle-tier service for processing requests. 该Web服务与中间层服务进行通信以处理请求。 The middle-tier service contract service looks something like this: 中间层服务合同服务如下所示:

[ServiceContract(CallbackContract=typeof(IServiceCallback))]
interface IService
{
    [OperationContract(IsOneWay=true)]
    void Process(Request request);
}

interface IServiceCallback
{
    [OperationContract(IsOneWay=true)]
    void Processed( Response response );
}

This service asynchronously processes a request via the matching Process/Processed methods. 该服务通过匹配的Process / Processed方法异步处理请求。

What's the best way to asynchronously make a call to from the web service to this middle-tier service? 从Web服务异步调用到此中间层服务的最佳方法是什么? I'm having trouble seeing how to get the response from the callback thread to return to the calling client, when the callback method obviously returns in a different worker thread? 当回调方法显然在另一个工作线程中返回时,我在查看如何从回调线程获取响应以返回到调用客户端时遇到麻烦?

Thanks for any help given. 感谢您提供的任何帮助。

You have to use async and await. 您必须使用异步并等待。 Read this link http://msdn.microsoft.com/en-us/library/vstudio/hh300224.aspx 阅读此链接http://msdn.microsoft.com/en-us/library/vstudio/hh300224.aspx

Try this in your calling code. 在您的调用代码中尝试此操作。

public void DoStuff()
{

    using(var proxy = new Proxy())
    {
        var result = await proxy.Process();
        Task.Run(() => Processed(result));
    }
}

Or this...which works with .net 4 或者这个...与.net 4一起使用

public void DoStuff()
{

    using(var proxy = new Proxy())
    {
        proxy.Process().ContinueWith(result => Processed(result), TaskCreationOptions.None);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM