Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF Web API
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)
 
        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 20-Nov-12 7:16am
Edited 22-Nov-12 16:50pm
v2
Comments
Soroush Falahati at 22-Nov-12 7:59am
   
500 mean internal server error, So it must be a problem from server side.
Jason Gleim at 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 at 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 at 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
0 Sergey Alexandrovich Kryukov 365
1 CPallini 335
2 BillWoodruff 324
3 George Jonsson 319
4 Gihan Liyanage 225
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2014
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