Xamarin PutAsJsonAsync exception

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 
+7
source share
3 answers

You need to install the following Nuget packages for all projects that reference your HttpClient project.

  • Microsoft.Bcl.Build
  • Microsoft.Bcl

These are Visual Studio warnings when I built my project, and after I did this, it will work!

+1
source share

Serialize your data into a JSON string as follows:

  string json = JsonConvert.SerializeObject(item); var content = new StringContent(json, Encoding.UTF8, "application/json"); HttpResponseMessage response = null; response = await client.PostAsync(url, content); 
+1
source share

The overload list for the System.Net.Http.PutAsJsonAsync method on MSDN shows only overloads containing three or four parameters. In your example, I see two that you pass. It seems that you can leave the HttpClient parameter on invocation as an instance from HttpClient based on code samples from MSDN.

However, with Xamarin it was built as a MonoDevelop fork. MonoDevelop uses the Mono Framework, which is a .NET Framework port. Because of this difference, I'm interested if the error should be open to Mono, MonoDevelop or Xamarin.

Back to the list of overload methods, although I recommend using one of these overloads.

  • PutAsJsonAsync (HttpClient, String, T)
  • PutAsJsonAsync (HttpClient, Uri, T)
  • PutAsJsonAsync (HttpClient, String, T, CancellationToken)
  • PutAsJsonAsync (HttpClient, Uri, T, CancellationToken)

I recommend trying to pass the HttpClient to the HttpClient method without calling PutAsJsonAsync from the HttpClient instance.

By the way, this is not a guaranteed solution, as it was too much information for comment. Hope this helps.

0
source share

All Articles