C# HttpClient PostAsync turns 204 into 404
asp.net-web-api, c#, httpclient, silverlight-5.0
Solution
Finally figured this out. Seems you get a choice of using either browser or client HTTP handling in Silverlight. When using browser HTTP handling a lot of stuff is unsupported - including various response codes and headers. Adding these lines before calling HttpClient fixed it:
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
Problem
Given this WebApi service: ``` [ActionName("KillPerson")] [HttpPost] public void KillPerson([FromBody] long id) { // Kill } ``` And this HttpClient PostAsync call: ``` var httpClient = new HttpClient { BaseAddress = new Uri(ClientConfiguration.ApiUrl) }; httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, Formatting = Formatting.Indented, ReferenceLoopHandling = ReferenceLoopHandling.Serialize }; var serializedParameter = JsonConvert.SerializeObject(parameter, serializerSettings); var httpContent = new StringContent(serializedParameter, Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(serviceUrl, httpContent).ConfigureAwait(false); response.EnsureSuccessStatusCode(); ``` I would expect response.EnsureSuccessStatusCode(); to succede but it throws a 404 instead. The funny thing is that fiddler tells med that the webapi service is returning 204 as expected and when I debug it the KillPerson runs without issue. Update: I Have determined that this only happens when the client code is within a PCL or Silverlight 5 project. The exact same code will give the expected 204 if I duplicate it in a Windows forms application. If i point the Windows Forms app to client code contained in a PCL it gives me the 404 Again. Update2: This resolves the issue (though it bothers me no end that I should need to do it): ``` [ActionName("KillPerson")] [HttpPost] public HttpResponseMessage KillPerson([FromBody] long id) { return this.Request.CreateResponse(HttpStatusCode.OK); } ``` This reintroduces the 404 (fiddler still says 204 and non-silverlight client runs fine) ``` [ActionName("KillPerson")] [HttpPost] public HttpResponseMessage KillPerson([FromBody] long id) { return this.Request.CreateResponse(HttpStatusCode.NoContent); } ``` Update 3 (resolved): Finally figured this out. Seems you get a choice of using either browser or client HTTP handling in Silverlight. When using browser HTTP handling a lot of stuff is unsupported - including various response codes and headers. Adding these lines before calling HttpClient fixed it: ``` WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp); WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); ```