ceth
ceth

Reputation: 45285

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.

Upvotes: 1

Views: 94

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456322

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.

Upvotes: 3

Related Questions