0

I have to get the result from a web service. This is what I used:

EDIT:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        webService.WebServiceHuscSoapClient a = new webService.WebServiceHuscSoapClient();
        a.InsertNewUserCompleted += a_InsertNewUserCompleted;
        a.InsertNewUserAsync("name", "phonenumber", "address");
    }

    void a_InsertNewUserCompleted(object sender, webService.InsertNewUserCompletedEventArgs e)
    {
        MessageBox.Show(e.Result.ToString());
    }

Is there any way that I could put all this function and even handler to a class and when I want to get data from my webservice I would do something like this:

string json = MyWebService.GetData();
Community
  • 1
  • 1
FlySoFast
  • 1,854
  • 6
  • 26
  • 47

2 Answers2

1

First, your call of DownloadStringAsync suggests passing in username and password, but it doesn't work like that. Check the documentation.

To (not really :-) ) answer your question: a far better approach these days is to use the new 'async/await' functionality available in C# 5. See this link for a comprehensive overview.

Your example then becomes trivial (no need to hook up a separate event handler anymore)

private async void Button_Click(object sender, RoutedEventArgs e)
{
    WebClient webclient = new WebClient();
    // TODO set up credentials
    string result = await webclient.DownloadStringTaskAsync("http://your-url-here");
    textBlock1.Text = str;
}

You could then still extract this in a separate, reusable (async) method:

private async Task<string> GetDataAsync()
{
    WebClient webClient = new WebClient();
    // TODO set up credentials
    string result = await webclient.DownloadStringTaskAsync("http://your-url-here"); 
    return result;
}

If you want to stick with the event-based approach, you could wrap the functionality in a separate class with it's own event, but that wouldn't bring you much gain IMO.

jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • Thanks for your very helpful answer! actually it's just some code block I found on the Internet that kinda looks like my code. I thought DownloadStringAsync was a function created by someone. Does this still work with my own function on webservice? some how all of my int methods became void on client, and await doesn't work with void as it said. – FlySoFast May 06 '14 at 12:54
  • Not entirely sure what you're asking here (what do you mean with "my int methods became void on client"?), but async/await is a feature of the language so it works exactly the same. I'd suggest to familiarize yourself with the feature. Try google "async await introduction". – jeroenh May 06 '14 at 19:31
  • I meant all my methods that return int I have on WCF server become the methods that return void in client. For example: I have a int MyMethod() on server but when I use it my client it became void service.MyMethodAsync(). Thank you for your advice. – FlySoFast May 07 '14 at 01:27
0

I figured it out from this article: How to use async-await with WCF in VS 2010 for WP7?

More: Async CTP - How can I use async/await to call a wcf service?

I wrote this on another class:

 public static Task<int> InsertNewUser(string name, string phonenumber,string address) //can make it an extension method if you want.
    {
        TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
        service.InsertNewUserCompleted += (object sender, WebService.InsertNewUserCompletedEventArgs e) => //change parameter list to fit the event's delegate
        {
            if (e.Error != null) tcs.SetResult(-1);
            else
                tcs.SetResult((int)e.Result);
        };
        service.InsertNewUserAsync(name, phonenumber,address);
        return tcs.Task;
    }

then I could call it from my class:

 int su = await WebServiceHelper.SignUp("blabla", "0123465","huehuehue");
Community
  • 1
  • 1
FlySoFast
  • 1,854
  • 6
  • 26
  • 47