简体   繁体   中英

Correct method of calling WCF service asynchronously

I have the WCF service which return the collection of objects.

He is working code I started with (I'm not sure it is correct):

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

I need to move this call to separate class:

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;
    }
}

Compiler say me that async method must return Task (or Task<>).

How do I need to rewrite my method and how this method call must be if I want to be sure that my code will wait when the Web Service returns the data.

Compiler say me that async method must return Task (or Task<>).

Then you make your async method return Task<> :

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

Yes, this will cause your callers to use await , which means they must become async , etc., etc. async will "grow" through your code base like this; this "growth" is natural and should be embraced.

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