简体   繁体   English

控制器动作中的异步Web响应

[英]Async webresponse in controller action

I have an MVC controller. 我有一个MVC控制器。 In that controller I reference a WCF service that accesses a 3rd party request (through webrequest) which returns information which is then saved in the WCf service (very specifically, we're collection data about visits and saving them into a database so that we can target our page organization better). 在该控制器中,我引用了一个WCF服务,该服务访问一个第三方请求(通过webrequest),该请求返回信息,然后将该信息保存在WCf服务中(非常具体地说,我们正在收集有关访问的数据并将其保存到数据库中,以便我们可以更好地定位我们的页面组织)。

The TL;DR is like this: TL; DR如下所示:

 private ActionResult CollectTrackingData(string campaign = "none")
    {
        VanityURLService.VanityURLConnector connector = new VanityURLService.VanityURLConnector();
        connector.SaveTrackingData(Request, campaign); 
        return View(someVarView);
    }

The SaveTrackingData does a bunch of stuff until it calls this method (edited to protect the not-so-innocent): 在调用此方法之前,SaveTrackingData会做很多工作(已进行编辑以保护不是那么纯真的):

 private string TranslateGeolocation(string ipAddress)
    {
        WebRequest pageRequest = System.Net.WebRequest.Create("http://blahblah.com");
        WebResponse resp = pageRequest.GetResponse();

        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        string reqResp = sr.ReadToEnd().Trim();
        return reqResp.ToString();
    }

The part that is likely to cause problems is the WebResponse resp = pageRequest.GetResponse(); 可能引起问题的部分是WebResponse resp = pageRequest.GetResponse(); but I can't help thinking that the best way to do that is to make the call that cascades the entire process asynchoronous (that would be the controller code: connector.SaveTrackingData(Request, campaign); 但是我不禁想到最好的方法是使整个过程异步级联的调用(这将是控制器代码: connector.SaveTrackingData(Request, campaign);

But when I try to make the call async, it whines at me about a few things until, after much tweaking, it demands that I have it return a return type and then still doesn't work because I supposedly can't make that asynchronous at that stage of the page life cycle (which it then offers aspx code which is useless since I'm running with a Razor engine). 但是,当我尝试使呼叫异步时,它会向我抱怨一些事情,直到经过大量调整后,它要求我让它返回一个返回类型,然后仍然不起作用,因为我认为无法使它异步在页面生命周期的那个阶段(然后它提供了aspx代码,因为我使用的是Razor引擎,因此该代码无用)。

This all seems like it's being way more difficult than it needs to be. 这一切似乎都比需要的要困难得多。 I feel like I should be able to make the CollectTrackingData() call async but it doesn't like that. 我觉得我应该能够使CollectTrackingData()异步调用,但事实并非如此。

Can someone point me in the right direction? 有人可以指出我正确的方向吗?

Just change your action method to return Task<ActionResult> . 只需更改您的操作方法即可返回Task<ActionResult>

Everything will then work fine. 然后一切都会正常。

To convert your method to async, the correct method signature is: 要将您的方法转换为异步方法,正确的方法签名为:

private Task<string> TranslateGeolocation(string ipAddress)

Then to call it: 然后调用它:

string str = await TranslateGeolocation(ipAddress);

The await keyword will unwrap the string from the Task<> for you. await关键字将为您解开Task<>string

If you were to want to convert a method that would normally not return a value, ie, 如果要转换通常不会返回值的方法,即

private void DoSomething()

the asynchronous signature would be: 异步签名将是:

private Task DoSomething()

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

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