0

I have two methods in my WCF Service:

public interface IPriceService
{
    [OperationContract(Name = "GetPrice")]
    decimal GetPrice(long ID);

    [OperationContract(Name = "GetProductID")]
    long GetProductID(string productName);
}

On a cliect side I want to do two things. I want to call a method GetProductID and then call a method GetPrice. So I do it like this:

PriceService.PriceServiceClient service;
service = new PriceService.PriceServiceClient();
service.PobierzPrzejsciaCompleted += service_GetPriceCompleted;
service.PobierzStacjeBenzynoweCompleted += service_GetProductIDCompleted;

service.GetProductIDAsync(productName);
service.GetPriceAsync(Id);

GetProductID returns an ID, so I get it in GetProductIDCompleted event. Then I pass this ID as a parameter to GetPrice method.

What I want is: GetProductIdAsync method should execute first. GetPriceAsync method should wait until previous method completes.

XardasLord
  • 1,764
  • 2
  • 20
  • 45
  • 2
    Are they `awaitable`? Why not `await service.GetProductIDAsync(productName);`? – gunr2171 Dec 15 '14 at 17:09
  • 1
    Implement [TaskCompletionSource](http://msdn.microsoft.com/en-us/library/dd449174(v=vs.110).aspx). Maybe [this answer](http://stackoverflow.com/q/15316613/2681948) will help. – Romasz Dec 15 '14 at 17:25
  • I don't have available "Generate task-based operations" in Windows Phone 8.1 while adding service reference. – XardasLord Dec 15 '14 at 17:37
  • Build a Task in which you define TCS, then subscribe to your service event, in the complete delegate set result for TCS, return TCS.Task. Then await your new Task. It's [converting EAP to TAP - example](http://stackoverflow.com/a/11450287/2681948). – Romasz Dec 15 '14 at 18:59
  • Oh thanks you. But it's really hard to understand for me... – XardasLord Dec 16 '14 at 01:56

2 Answers2

1

If you want to have them executed synchronous, you can also simply use the synchronous versions:

var productId = service.GetProductID(productName)
var price = service.GetPrice(productId)

our if you would like to stick on the async calls, you can call the service.GetPriceAsync function from the service_GetProductIDCompleted completed function...

Stephen Reindl
  • 5,659
  • 2
  • 34
  • 38
0

You can use a BackgroundWorker object and then subscribe to the run worker completed event. In that event you would be able to run it.

So something like

Worker.RunWorkerAsync()
Worker.RunWorkerCompleted += youractionwhencompletedmethod
gunr2171
  • 16,104
  • 25
  • 61
  • 88
PJC
  • 1
  • 1