2

I am trying to read a response XML from a service to a class to use it. if i use this URL https://btsvcs.college.edu/HR/Employee/ID/[email protected]

The response XML will be like this:

<Data>
  <Record id="PA01051" rowOrder="0">
    <employee_id>20000343</employee_id>
  </Record>
</Data>

I had create my class to be the same with the response XML.

This is the class:

public class GetEmployeeIdByEmail
{
    [DataContract(Namespace = "")]
    public class Record
    {

        [DataMember(Order = 0)]
        public string employee_id { get; set; }
    }

    [DataContract(Namespace = "")]
    public class Data
    {
        [DataMember(Order = 0)]
        public Record Record { get; set; }
    }
}

After that i had make a function to read the XML using the HttpClient

This is the function:

public async Task<GetEmployeeIdByEmail> GetEmployeeIdByEmail()
{
    var client = new HttpClient();
    var url = "https://btsvcs.college.edu/HR/Employee/ID/[email protected]";
    client.BaseAddress = new Uri(url);
    client.DefaultRequestHeaders.Accept.Clear();

    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
    var responseMessage = await client.GetAsync(url);
    GetEmployeeIdByEmail responseData = null;

    if (responseMessage.IsSuccessStatusCode)
    {
        responseData = await responseMessage.Content.ReadAsAsync<GetEmployeeIdByEmail>();
    }

    return responseData;
}

If I make a break point on this function and tracking it step by step always get back to website to read the xml after this line

var responseMessage = await client.GetAsync(url); and no response back to complete the debugging.

I don't know what is reason for that.

14
  • Does the application throw an error? does it hang? what do you mean by no response back? Clarify. Commented Apr 18, 2017 at 12:31
  • Check your url. Your code works fine. Test with "duckduckgo.com" Could be name resolution for "btsvcs.college.edu". Commented Apr 18, 2017 at 12:32
  • @Nkos their is no exception the application still reload. Commented Apr 18, 2017 at 12:33
  • @ivw the url is correct and the response is so fast Commented Apr 18, 2017 at 12:34
  • 1
    "must be 200", don't "must be" it. Assert it. Is it actually 200 or not? Commented Apr 18, 2017 at 12:39

3 Answers 3

1

The reason here is that client.GetAsync is asynchronous method that makes async I/O to network to perform the request and receive a response. When such async operation occurs and there is an await keyword in the before the method call the control return up to by the stack to the caller of the method. And the when client gets a response method resumes from where it left.

Try set your break point at a next line after await and and will be hit once request is performed.

Sign up to request clarification or add additional context in comments.

2 Comments

It's not going to the next point, but After make it .Result; and remove the await it's working correctly
@Hansy, because you made it synchronous. Have you tried to put a breakpoint on a next line?
1
HttpResponseMessage responseMessage;
try
{
    responseMessage = await client.GetAsync(url);
}
catch (Exception ex)
{
    throw ex;
}

2 Comments

Also the browser still loading
take a look here change exception settings may help get more details on the issue. stackoverflow.com/questions/22024856/…
0

As @ivw answer to use this link

That's helps me to use the code written in the question.

responseMessage = client.GetAsync(url).Result;

Instead of

responseMessage = await client.GetAsync(url);

and remove the async from the function header

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.