I'm trying to make a simple class for my REST calls so I won't have the same code in multiple places in my application.
The problem is, I don't know how to notify an object that called the UploadStringAsync() method from inside the UploadStringCompleted() event handler.
Here is my class, I have added comments to mark the place where I want to notify the object from which the Post method has been called:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MyProject.Utils
{
public class Rest
{
private WebClient client = new WebClient();
private void Init()
{
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
}
public void Post(string uri, string postRequest, object objectToNotify)
{
Init();
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
client.UploadStringAsync(new Uri(uri,
UriKind.RelativeOrAbsolute), "POST", postRequest, objectToNotify);
}
private void client_UploadStringCompleted(object sender,
UploadStringCompletedEventArgs e)
{
// here I would like to notify an object from
// which the Post() method has been called
// I don't know how to do this as this method has no idea
// which object called the method
// this doesn't work:
// (e.UserState.GetType())e.UserState.RestCallback(e);
}
}
}
EDIT:
My modified code after Jacob's suggestions:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
namespace MyProject.Utils
{
public class Rest
{
private WebClient client = new WebClient();
private void Init()
{
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
}
public void Post(
string uri, string postRequest,
Action<UploadStringCompletedEventArgs> callback)
{
HtmlPage.Window.Alert("bbb");
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
client.UploadStringAsync(new Uri(uri,
UriKind.RelativeOrAbsolute), "POST", postRequest, callback);
}
private void client_UploadStringCompleted(object sender,
UploadStringCompletedEventArgs result)
{
HtmlPage.Window.Alert("aaa");
var callback = (Action<UploadStringCompletedEventArgs>)result.UserState;
callback(result);
}
}
}
This is how I'm calling the Post method:
string postRequest = "id=5":
Rest restClient = new Rest();
restClient.Post("http://mywebsite.com/", postRequest, RestCallback);
.......................
private void RestCallback(UploadStringCompletedEventArgs result)
{
if (result.Error != null)
{
ContactFormSuccess.Text = "There was an error sending email message.";
}
else
{
ContactFormSuccess.Text = "Email message successfully sent.";
}
}