3

First of all, this question is not a duplicate. I found this question and this answer. But it didn't solve my problem. I tried to accept permission. It seems accepted on api. But app stucks in loading. Because I have a problem to get a response from web api. I put breakpoints. But it does not hit any line after SendAsync or PutAsync and no exception/error. It tested on postman and returned 200. This api is using for web project also and it works fine.

This code was working before:

public async Task AcceptPermission(string Id)
{
    try
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
        var request = new HttpRequestMessage(HttpMethod.Put, BaseAddress + "leave/response/" + Id + "/Accepted");

        var response = await client.SendAsync(request);
        var jsonResp = response.Content.ReadAsStringAsync().Result;
     }
     catch (Exception ex)
     {
         ...
     }
 }

I tried these from taking a reference to the other questions:

  • Changed base address http to https
  • Removed .Result from response
  • Added ConfigureAwait(false)

I changed my code with this and also didn't work:

public async Task<string> AcceptPermission(string Id)
{
     try
     {
         string uri = BaseAddress + "leave/response/" + Id + "/Accepted";
         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
         HttpContent content = new StringContent(JsonConvert.SerializeObject(Id), Encoding.UTF8, "application/json");

         var response = await client.PutAsync(uri, content).ConfigureAwait(false);
         if (response.IsSuccessStatusCode)
         {
             ...
         }
         else
         {
             ...
         }
     }
     catch (Exception ex)
     {
         ...
     }
 }

I don't understand what did I wrong? Any help is very useful. Thanks for your help.

Hilal Sener
  • 103
  • 1
  • 11
  • 1
    You say it does not work, but do you see an exception / error message, in your output window when debugging this ? – Miiite Jan 22 '19 at 09:03
  • 1
    Exception/Stacktrack? What exactly is not working? Also avoid deadlocks by not mixing .Result and await, *in general* avoid using .Result ( change your .Result to `var jsonResp = await response.Content.ReadAsStringAsync();`) – SushiHangover Jan 22 '19 at 09:08
  • Are you sure the problem is the Xamarin implementation and not the server configurations? https://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed-iis-8 – Bruno Caceiro Jan 22 '19 at 09:23
  • @Miiite I don't see any exception/error. Just stuck in loading and it does not hit response.IsSuccessStatusCode. It flies away after PutAsync – Hilal Sener Jan 22 '19 at 10:34
  • @SushiHangover I tried before the same but with not await. I'll try and let you know – Hilal Sener Jan 22 '19 at 10:48
  • @SushiHangover Thanks for your help. But It didn't work – Hilal Sener Jan 22 '19 at 11:05
  • Saying something like 'HttpClient for Xamarin.Forms in .Net Standard still does not work stable' with no reproducible test case makes this question a candidate for closing, I voted for that. The most likely problem is that server API is not working you don't mention that you have tested that, but as you haven't provided details only you can test which again makes question a candidate for closing. – Ivan Ičin Jan 22 '19 at 11:10
  • @IvanIčin What do you understand from this sentence? "I tried to accept permission. It seems accepted on api. This code was working before..." Something is looking not stable. Based on the other question I said. I tried on postmand and also this api is using on web project. Api works for the other projects excluding Xamarin project. If you think some meaning is wrong, you can edit it. This helps more. Thanks for your interest. – Hilal Sener Jan 22 '19 at 11:37
  • @HilalSener exactly that part "This code was working before" points to the API problem. If you haven't done anything then the API has changed, otherwise you would get the same results. If you did change something then you can explain what, that would surely make it much better question. The question and answer you referred actually says that is sees no problems in HttpClient in .Net Standard, only in PCL... – Ivan Ičin Jan 22 '19 at 11:59

0 Answers0