Click here to Skip to main content
13,048,584 members (93,047 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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)

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;
        throw new Exception(string.Format("Data access faild,{0} ({1}) method:{2}", (int)response.StatusCode, response.ReasonPhrase, MethodURL));

Thanks & Regards,
Posted 20-Nov-12 7:16am
Updated 22-Nov-12 16:50pm
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 Sawant 22-Nov-12 23:12pm
Thanks for your reply.

The web service is working fine and it always returns the results.

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100