简体   繁体   English

如何在 c# 中调用异步 web 服务?

[英]how to call async web service in c#?

Below is the way i am currently calling the web service to project,以下是我目前调用 web 服务进行项目的方式,

  public JsonResult CruiseBE_Data1(string cruise)
    {
        CruiseService GetPorts = new CruiseService();
        projectCruise.CruiseServiceASD.ServiceAuthHeader serviceAuthHeaderValue_Cruise = new projectCruise.CruiseServiceASD.ServiceAuthHeader();
        serviceAuthHeaderValue_Cruise.username = "xxxxxxxxxx";
        serviceAuthHeaderValue_Cruise.password = "xxxxxxxxxx";
        GetPorts.ServiceAuthHeaderValue = serviceAuthHeaderValue_Cruise;

        string CruisePorts = "";

        CruisePorts = GetPorts.GetPorts();


        return Json(CruisePorts);
    }

What I am trying to do is i want to change above to async way of requesting can any one please guide me how to do that.我想要做的是我想在上面更改为异步请求方式,任何人都可以指导我如何做到这一点。

The easiest way would be to wrap your GetPorts.GetPorts() call in a Task.Run() :最简单的方法是将GetPorts.GetPorts()调用包装在Task.Run()中:

CruisePorts = await Task.Run(() => GetPorts.GetPorts());

But take care, that your method then must be defined as但请注意,您的方法必须定义为

public async Task<JsonResult> CruiseBE_Data1(string cruise)

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

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