I am trying to write a small test application using WebApi. It basically works for me, I can get data from my web service and display it on Android.
I added a button and used the same code that I used in the Winforms test client:
async void buttonSave_Clicked(object sender, EventArgs e) { HttpClient client = new HttpClient (); Customer data = new Customer () { Surname = editSurname.Text, GivenName = editGivenName.Text}; var result = await client.PutAsJsonAsync("http://10.0.0.4/WebApplication1/api/Customers/2", data); if (result.IsSuccessStatusCode ) { labelStatus.Text = "Saved"; } }
This works fine in a Windows Forms test application, but in a Xamarin application I get an exception on client.PutAsJsonAsync:
E / mono-rt (7519): [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Failed to load type 'System.Net.Http.ObjectContent`1 [T]' from the assembly 'System.Net.Http. Formatting, Version = 5.1.0.0, Culture = Neutral, PublicKeyToken = 31bf3856ad364e35 '.
Any suggestions? Using Xamarin 3.
edit:
It works if I format the contents manually:
string sData = Newtonsoft.Json.JsonConvert.SerializeObject(data); HttpContent content = new System.Net.Http.StringContent(sData, System.Text.Encoding.UTF8, "application/json") ; var result = await client.PutAsync("http://10.0.0.4/WebApplication1/api/Customers/2",content);
I have all the correct links, as far as I can see. I used the nuget package of the WebApi client.
Literature:
Newtonsoft.Json System.Net.Http System.Net.Http.Extensions System.Net.Http.Formatting System.Net.Http.Primitives
Molloch
source share