简体   繁体   中英

how to call async web service in c#?

Below is the way i am currently calling the web service to project,

  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() :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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