Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have wrote an application which calls REST service using Web API.

When I debug it works fine
however when I deploy the app to program files/double click the exe from the bin/release folder
call to the REST service fails and I always get 500 status code as response.

I have posted full Source Code: Ultra News[^]

I am confused whether following code is causing the bug(this code works fine when debugging the app)

C#
public IEnumerable<T> Get()
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(_endpoint);
    //Add an Accept header for JSON format.
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    // call the REST method
    HttpResponseMessage response = client.GetAsync(MethodURL).Result;  // Blocking call!
    if (response.IsSuccessStatusCode)
    {
        // Parse the response body. Blocking!
        return response.Content.ReadAsAsync<IEnumerable<T>>().Result;
    }
    else
    {
        throw new Exception(string.Format("Data access faild,{0} ({1}) method:{2}", (int)response.StatusCode, response.ReasonPhrase, MethodURL));
    }
}




Thanks & Regards,
Nitin
Posted
Updated 22-Nov-12 16:50pm
v2
Comments
Soroush Falahati 22-Nov-12 7:59am    
500 mean internal server error, So it must be a problem from server side.
Jason Gleim 22-Nov-12 22:59pm    
Two things... make sure your REST service is up and running before you try to run the app. If the service was also part of your solution, then VS is starting the built-in IIS server and launching the web service for you when you are debugging. It won't be that polite when you run the app independent of VS. As a test, you should be able to enter the service endpoint URL into a browser and get back a WSDL or xml response or something. (other than a 404 or 500 :-) )

Second, download and install Fiddler. It proxys your web calls so you can 'insert' it between your app and the web service. You can see the calls on the "wire" to figure out what is going on. It should be standard equipment for anyone working with any type of web service.
Nitin S 22-Nov-12 23:12pm    
Hi,
Thanks for your reply.

The web service is working fine and it always returns the results.
e.g. http://api.feedzilla.com/v1/categories

I think before the call to the service completes, .NET is checking for the status of the http request thats why I am getting the error.

<pre>HttpResponseMessage response = client.GetAsync(MethodURL).Result; // Blocking call!</pre>
AmitGajjar 23-Nov-12 0:05am    
Add try... catch and message the exception.

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