0
Answer

WebServices and EventHandler in C#.

Ephismen

Ephismen

14y
3.3k
1

Hello,


Sorry if I make some grammar mistakes because i'm not a native english speaker but i'll try to be as clear and correct as possible.

I was unsure of where to post this because it was concerning 3 different support: Silverlight, WebClients and C#, but finnaly decided it was more suitable to the last category.

Ok here is my problem:

I'm developping a Silverlight application that needs to consume multiple times a WebService. When I will first consume this Webservice it will return me with a UserID that I need to conserve in order to call other WebServices. For example:

The user enters his "login" and "password", clicks the "Connect" button and the WebService returns the program an array with a value(0 or 1), and a user ID(guid) as a session identifyer. After that, the program needs to retrieve the users settings and infos, in order to do that he will consume another WebService entering as parameter the user ID(guid), and returning the users mails, age, etc... The problem is I would like to be able to retrieve this user ID once and for all out of my first WebClient call and be able to use it whenever I want. But the problem is that when I create a WebClient it generates an EventHandler and I do not know how to extract anything from that except with the UserInterface Thread of Silverlight witch isn't very usefull for my purposes. I tried to use lambda expression but it wasn't really what i needed even though it cleaned my code a litle.

Here is what i'm doing with what I would like to do.


 namespace ButtonTry
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

//When the users clicks on the Connect Button
private void btnID_Click(object sender, RoutedEventArgs e)
{
WS.MainSoapClient proxy = new AfinosWS.MainSoapClient();

WS.ArrayOfString request_tab = new WS.ArrayOfString();
WS.ArrayOfString response_tab = new WS.ArrayOfString();

//Adding the login and password into the array that i will send to the WebService
request_tab.Add(IDcase.Text);
request_tab.Add(PWcase.Text);

proxy.USER_FunctionsCompleted += (s, ea) =>
{
response_tab = (AfinosWS.ArrayOfString)ea.Result;

//Showing the returned data to the screen for debugging purposes
ReturnValue.Text = response_tab[0];
Guid.Text = response_tab[1];
};
proxy.USER_FunctionsAsync("USER_LOGIN", request_tab);

//I would like to get the guid(response_tab[1]) here in order to send it to another Webservice or functions.
}
}
}

The only time I was able to save that value out of my EventHandler was to create a global var, but even then it worked only when i pressed the connect button for a second time.

If you have any question or didn't understand what I said or what I wanted please feel free to ask me.

Thank you, Ephismen.

[EDIT]Just a litle edit to specify simply what i'm trying to do:

1 - Send user login and Password (returns Array with a value in first string and a user id in second), and launch application.
1a - Save the "user id" into a string.
2 - Start a Loop that will consume Webservice every 2 seconds:
    2a - Send "user id" and retrieve the users Information(age, name, e-mails);
    2b - Send "user id" and retrieve users friends(array of strings with friend name, message and avatar)

I could do this a very dirty way by calling my first WebService over and over to retrieve each time the "user id" but i would like to have it for the whole program.