none
WEB API RRS feed

  • Question

  • Hello

     I got a error FindAll which not returns a value how to solve it??

    Please help me 

    namespace CRUDWebAPI.Models

    {
        public class PlayerClient
        {
            private string BASE_URL = "http://localhost:56788/api";




            public IEnumerable<Player> FindAll()
            {
                try
                {
                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri(BASE_URL);
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = client.GetAsync("api/player").Result;
                    if (response.IsSuccessStatusCode)
                    {
                        return response.Content.ReadAsAsync<IEnumerable<Player>>().Result;
                        return null;
                    }
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    Monday, July 31, 2017 6:46 AM

All replies

  • Hello Sridhar10,

    I have created a example base on your code and everything works well. Maybe you should change some code with your server side.

    class Program
        {
            private static string BASE_URL = "http://localhost:50489/api/player";
            static void Main(string[] args)
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(BASE_URL);
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = client.GetAsync(new Uri(BASE_URL)).Result;
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine(response.Content.ReadAsStringAsync().Result);/*ReadAsAsync<IEnumerable<Player>>().Result;*/
                }
                Console.ReadLine();
            }
        }

    serverSide

    public class PlayerController : ApiController
        {
            // GET api/Player
            public HttpResponseMessage Get()
            {
                string json = "{\"result\":\"true\"}";
                return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
            }
        }

    The Result

    If the issue  still exists , you could share more information about your server code so that I can do further test.

    Best regards,
    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 31, 2017 12:11 PM
  • I got a error FindAll which not returns a value how to solve it??

    But you can't post the exception, if there is one?

    WebAPI issues can be discussed at the WebAPI forum section.

    http://forums.asp.net/

    Monday, July 31, 2017 1:30 PM
  • "which not returns a value how to solve it??"

    If you're referring to the fact that you get null back when something goes wrong then that is because you're returning null in your catch statement. Unless your code can actually handle the error then don't wrap it in a try-catch. Let the calling code decide how to handle the error.

    Also note that if there is a server-side error or bad request then Response contains the error information but it won't throw the exception by default. To have it throw exceptions you need to call EnsureSuccessStatusCode. Otherwise you're silently ignoring any errors that occur.

    public IEnumerable<Player> FindAll()
    {
       //You don't want to be recreating this each time, it is bad, refer to MSDN
       _client.BaseAddress = new Uri(BASE_URL);
       _client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
        using (var response = client.GetAsync("api/player").Result)
        {
            response.EsnureSuccessStatusCode();
    
            return response.Content.ReadAsAsync<List<Player>>().Result;
        };
    }
    
    private readonly HttpClient _client = new HttpClient();

    Making some assumptions here but IEnumerable<Player> won't work in reads if you're using certain JSON serializers like JSON.Net. You have to use List instead. This makes sense because the response is going to get cleaned up yesterday.

    HttpClient is designed to be created once and reused throughout the app. It is considered thread-safe but my experience is that this is only partially true and only for the same URL. Ideally create this object once and reuse for the life of the app. In the above code I made it per-instance of the parent type but changing that to static would be better provided you're not reusing this client for multiple base addresses. My rule is 1 client / base address and make it static.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, July 31, 2017 1:51 PM