Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6938279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:28:20+00:00 2026-05-27T12:28:20+00:00

I am able to consume a service asynchronously like below: public void PostMethodResponse() {

  • 0

I am able to consume a service asynchronously like below:

public void PostMethodResponse()
{
    try
    {
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(_url);
        myRequest.Method = "POST";
        myRequest.Headers["SOAPAction"] = _action;
        myRequest.ContentType = "text/xml; charset=utf-8";
        myRequest.Accept = "text/xml";
        myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult);
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(_postData);                
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();
        // Start the asynchronous operation to get the response
        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void GetResponseCallback(IAsyncResult asynchronousResult)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        // Close the stream object
        streamResponse.Close();
        streamRead.Close();
        // Release the HttpWebResponse
        response.Close();
        _response = responseString;                    

    }
    catch (Exception ex)
    {
        _response = ex.Message;

    }
}

I am calling the PostMethodResponse() function (which is in Model Class) from a ViewModel class. I am able get the response in GetResponseCallback function but how can i return that Response to ViewModel and then to View(Front End .xaml). To get the Response, We can fire an event GetResponseCallback function and then catch it ViewModel Class and Fire the same event ViewModel and Catch it View, But this is a not a right way.

Please help me in understanding the MVVM architecture call web services.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:28:21+00:00Added an answer on May 27, 2026 at 12:28 pm
    1. Add the callback to your viewmodel to the BeginGetRequestStream object state parameter.
    2. Get the callback out of the asynchronousResult in GetResponseCallback. Cast it to your call back type and call it back with your response.

    Like this:

        class HttpRequest<T>
    {
        internal HttpRequest(HttpWebRequest webRequest, Action<T> callback)
        {
            WebRequest = webRequest;
            Callback = callback;
        }
    
        internal HttpWebRequest WebRequest { get; private set; }
        internal Action<T> Callback { get; private set; }
    }
    class Class1
    {
        private Uri _url;
        private string _action;
        private string _postData;
    
        public void PostMethodResponse(Action<string> callback)
        {
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(_url);
                myRequest.Method = "POST";
                myRequest.Headers["SOAPAction"] = _action;
                myRequest.ContentType = "text/xml; charset=utf-8";
                myRequest.Accept = "text/xml";
                myRequest.BeginGetRequestStream(GetRequestStreamCallback, new HttpRequest<string>(myRequest, callback));
            }
            catch (Exception ex)
            {
                // log blah
            }
        }
    
        private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpRequest<string> request = (HttpRequest<string>)asynchronousResult.AsyncState;
    
                System.IO.Stream postStream = request.WebRequest.EndGetRequestStream(asynchronousResult);
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(_postData);
                postStream.Write(byteArray, 0, byteArray.Length);
                postStream.Close();
                // Start the asynchronous operation to get the response
                request.WebRequest.BeginGetResponse(GetResponseCallback, request);
            }
            catch (Exception ex)
            {
                // nothing to see, move along
            }
        }
    
        private void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpRequest<string> request = (HttpRequest<string>)asynchronousResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.WebRequest.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                if (streamResponse != null)
                {
                    StreamReader streamRead = new StreamReader(streamResponse);
                    string responseString = streamRead.ReadToEnd();
                    // Close the stream object
                    streamResponse.Close();
                    streamRead.Close();
                    // Release the HttpWebResponse
                    response.Close();
                    request.Callback(responseString);
                }
            }
            catch (Exception )
            {
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You should be able to create a generic form: public partial class MyGenericForm<T> :
I have a question: When I create my custom domain service (to consume 3rd
I am trying to consume a rest service which returns XMI data like this:
I am able to consume the WCF Service in my front end ASP MVC
I need to be able to create basic MS Project items (tasks, projects, resources,
I'm trying to use WCF to consume a web service provided by a third-party's
I need to consume an external web service from my VB6 program. I want
I'm trying to consume a web service created in .NET which needs SOAP authentication.
I've developed a web service in asp.net and am able to test it from
I have to consume data from axis2 webservice. I added it's URI to Service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.