1

I am Developing a C#/Xaml Metro Application in that As per the Requirement I want to have a Synchronous service call , instead of ASynchronous Service Call.

This what I have used for Async Operation but I want to make a Synchronus Service Call :

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://xxxxxxx");
    request.Method = "POST";
    request.Credentials = new NetworkCredential("xxx", "xxx");
     using (Stream requestStream = await request.GetRequestStreamAsync())
    {
    }

How can I make a Synchronous service Call using HttpWebRequest or HttpClient in C#/XAML Metro App ?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user1516781
  • 973
  • 1
  • 20
  • 42
  • It was part of my requirment can let me know how can do it or let me know how can i wait until that asynchornous call is compleleted (apart from using await ) ?? i want to wait completely and dont do any operation until the asynchronous call is completed. ?? – user1516781 Oct 16 '13 at 06:39
  • 1
    You should use async. It's a better model and means that your application is far less likely to feel "locked" to the user and non-responsive. Add a UI for "authentication" rather than do synch – WiredPrairie Oct 16 '13 at 08:45

3 Answers3

2

I use this method because it allows for doing other work before waiting:

        var t = request.GetRequestStreamAsync();
        //do other work here
        t.Wait();
        using (Stream requestStream = t.Result)
        {
        }
Jon
  • 2,891
  • 2
  • 17
  • 15
0

Do the smaller change. Instead of await request.GetRequestStreamAsync(), use request.GetRequestStreamAsync().Result.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://xxxxxxx");
request.Method = "POST";
request.Credentials = new NetworkCredential("xxx", "xxx");
using (Stream requestStream = request.GetRequestStreamAsync().Result)
{
}

More info : How would I run an async Task<T> method synchronously?

Community
  • 1
  • 1
Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
0

I am Developing a C#/Xaml Metro Application in that As per the Requirement I want to have a Synchronous service call , instead of ASynchronous Service Call.

The correct solution is to change the "requirement". Metro apps are supposed to be asynchronous, not synchronous. If you develop a Metro app with a synchronous UI (which is difficult but possible), it will likely be rejected from the app store.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810