0

I'am trying to consume a wcf in windows 8: My code to consume the method generated by the WCF service:

public System.Threading.Tasks.Task<Maquette_MyAirport_Win8.FlightService.CitiesResponse> 
    GetAllCitiesAsync(Maquette_MyAirport_Win8.FlightService.BaseRequest request)
{
    return base.Channel.GetAllCitiesAsync(request);
}

is

public testproxy()
{
    _client = new FlightInfoServiceClient(Maquette_MyAirport_Win8.FlightService.FlightInfoServiceClient.EndpointConfiguration.wsHttpBindingConfiguration);
    BaseRequest req = new BaseRequest();
    System.Threading.Tasks.Task<Maquette_MyAirport_Win8.FlightService.CitiesResponse> CitiesResponse = _client.GetAllCitiesAsync( new BaseRequest());
    CitiesResponse.ContinueWith(task => citiesL = task.Result.Cities.FirstOrDefault(););

}

The status of the task citiesResponse is "WaitingForACtivation" and Result="Not yet Computed"

How can I Activate the task and how can i obtain the result??

dtb
  • 213,145
  • 36
  • 401
  • 431
user1428798
  • 1,534
  • 3
  • 24
  • 50
  • I think there is something wrong with your generated code. Normaly the task should run as soon as you got it back - maybe you can try to step through the generated code in your debuger to find out where the problem is – Random Dev Sep 20 '12 at 08:26
  • You may have a look at [this question](http://stackoverflow.com/questions/9255187/task-status-waitingforactivation-what-does-this-mean) too... – Yannick Blondeau Sep 20 '12 at 08:43
  • When is the status of the `Task` the way you describe? Inside `testproxy()` or in the continuation? If it's inside `testproxy()`, then that could be correct behavior and you don't have to do anything to activate the `Task`. – svick Sep 20 '12 at 08:59

1 Answers1

3

Don't you have to add async and await to get the result type instead of Task type??

It should be something like this:

var result = await _client.GetAllCitiesAsync( new BaseRequest());

And in the method you should add async

margabit
  • 2,924
  • 18
  • 24
  • See and example here: http://blogs.msdn.com/b/piyushjo/archive/2011/09/22/wcf-in-win8-metro-styled-apps-absolutely-supported.aspx – margabit Sep 20 '12 at 08:28
  • 1
    `Task` predates `async`-`await`, you don't have to use it to get the result of the `Task`, there are other ways to do it. – svick Sep 20 '12 at 09:00
  • I know there are other ways to get the result of the Task. I think it's a common issue that developers with .net 4 background don't know yet that the proper way to do it is with async - await.. – margabit Sep 20 '12 at 09:02
  • But that's not the “proper” way, it's just one of the ways to do it. – svick Sep 20 '12 at 09:05
  • Ok.. Then "another way to do it". – margabit Sep 20 '12 at 09:07
  • I think s/he was looking for the await - async approach. Thanks for the -1.. http://stackoverflow.com/questions/12509992/how-can-i-set-cilentcredentials-in-wcf – margabit Sep 20 '12 at 09:51
  • The code in the question was fine. Using `async` might improve it, but it doesn't fix it. I do think that proposing an alternative without explaining what's actually going on is not a good answer. – svick Sep 20 '12 at 09:54
  • Of course it fixes it. Read the question: "How can I Activate the task and how can i obtain the result??" If you use the async-await approach, it returns the objects once the Tasks have finished, so you don't get "Not Yet computed". – margabit Sep 20 '12 at 10:11
  • By the way.. You are right, maybe I should have explained this in my answer. – margabit Sep 20 '12 at 10:13