Johan
Johan

Reputation: 8256

info about WCF and ASYNC

I am busy creating a WCF Web Service, that needs an ASYNC call. I have done this so far:

public async Task<bool> sendData(Data data) {
    bool sent = await Task<bool>.Factory.StartNew(() => {
        return someNameSpace.ReceiveData(data);
    });

    return sent;
}

So my question is, does the call to the dll also need to be marked as async?

ie. within the dll

public async Task<bool> ReceiveData(Data data) {
    // some async code
    // some code here
}

Upvotes: 0

Views: 462

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456507

You may find my async/await intro helpful. I also have a post specifically dealing with async WCF.

Some general points:

  • Don't use StartNew (or Task.Run) unless you explicitly want to run some code on a background thread.
  • You can await any Task, regardless of where it came from. A method that returns a Task may or may not be async - it only makes a difference within that method.

So, if your ReceiveData is either of these (they look the same to SendData):

public async Task<bool> ReceiveData(Data data);
public Task<bool> ReceiveData(Data data);

Then you can just await it in SendData:

public async Task<bool> SendData(Data data)
{
  bool sent = await someNameSpace.ReceiveData(data);
  ... // do other stuff
  return sent;
}

Upvotes: 6

Related Questions