簡體   English   中英

正確調用WCF服務的異步方法

[英]Correct method of calling WCF service asynchronously

我有WCF服務,它返回對象集合。

他正在編寫我開始使用的代碼(我不確定它是否正確):

List<AxaptaServiceReference.Inspection> remoteInspections = (await proxy.GetInspectionsByInspectorAsync(App._winlogin)).ToList<AxaptaServiceReference.Inspection>();

我需要將此調用移到單獨的類:

public class AxGateWay
{
    public async List<AxaptaServiceReference.Inspection> GetInspections(string _inspector) 
    {
        AxaptaServiceReference.AxaptaWebServiceClient proxy = new AxaptaServiceReference.AxaptaWebServiceClient();

        List<AxaptaServiceReference.Inspection> remoteInspections = (await task).ToList<AxaptaServiceReference.Inspection>();
        return remoteInspections;
    }
}

編譯器說我異步方法必須返回Task(或Task <>)。

如果我想確保我的代碼在Web Service返回數據時等待,我該如何重寫我的方法以及此方法調用的方式。

編譯器說我異步方法必須返回Task(或Task <>)。

然后讓你的async方法返回Task<>

public async Task<List<AxaptaServiceReference.Inspection>> GetInspectionsAsync(string _inspector) 
{
  List<AxaptaServiceReference.Inspection> remoteInspections = ...;
  return remoteInspections;
}

是的,這將導致你的調用者使用await ,這意味着他們必須變得async ,等等。 async將通過你的代碼庫“增長”,就像這樣; 這種“增長”是自然的,應該被接受。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM