Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
is it possible to consume rest wcf service in windows form. If so , please give me the direction. I tried like this to consume.

C#
string ServiceUrl = "http://localhost:4664/MEKRest/Service.svc/news/";
          WebRequest theRequest = WebRequest.Create(ServiceUrl);
          WebResponse theResponse = theRequest.GetResponse();
          DataContractSerializer collectionData = new DataContractSerializer(typeof(string));
          var arrEmp = collectionData.ReadObject(theResponse.GetResponseStream());
          label1.Text = arrEmp.ToString();



Error in line 1 position 50. Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'MEKNewsDataResponse', namespace 'http://tempuri.org/'.
Posted
Updated 3-Sep-13 22:51pm
v2

1 solution

void CallBackJSONAddRequest(IAsyncResult ar)
{
    //Contains information of the Async object containing Request Information
    WebRequest req = (WebRequest)ar.AsyncState;
    //Returns stream for writing data to the Web Resource
    Stream reqStream = req.EndGetRequestStream(ar);
    reqStream.Close();

    //Now Collect the response Asynchronously
    req.BeginGetResponse(CallBackJSONResponse, req);
}
//Response Callback, is mandatory to complete operations on the receiving end
void CallBackJSONResponse(IAsyncResult ar)
{
    try
    {
        WebRequest req = (WebRequest)ar.AsyncState;
        //Collect the reponse
        WebResponse res = req.EndGetResponse(ar);
        Stream strResult = res.GetResponseStream();

        strResult.Close();
        res.Close();
    }
    catch (Exception ex)
    {
        string s = ex.Message;
    }
}



private void button1_Click(object sender, EventArgs e)
{
    string uploadUrl = "http://localhost:7794/GetEmployees.svc/CreateEmployee/" + textBox1.Text + "/" + textBox2.Text + "/" + textBox3.Text;
    WebRequest addRequest = WebRequest.Create(uploadUrl);
    addRequest.Method = "POST";
    //Begin the Asynchrnous Request
    addRequest.BeginGetRequestStream(CallBackJSONAddRequest, addRequest);
    label1.Text = "Employee" + textBox1.Text + "Inserted Successfuly";
    textBox1.Text = "";
    textBox2.Text = "";
    textBox3.Text = "";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900