Skip to content

Support Xamarin

fonlow edited this page Jun 2, 2018 · 3 revisions

The support for Android and iOS is done through Xamarin. As you can see from the ClientApi solution folder there are 3 respective projects for these platforms. So from the same C# codebase generated, you could have 4 sets of client APIs for 4 platforms.

Remarks:

When reviewing the C# codes generated as shown below:

        public async Task<long> CreatePersonAsync(DemoWebApi.DemoData.Client.Person p)
        {
            var requestUri = new Uri(this.baseUri, "api/Entities/createPerson");
            using (var requestWriter = new System.IO.StringWriter())
            {
            var requestSerializer = JsonSerializer.Create();
            requestSerializer.Serialize(requestWriter, p);
            var content = new StringContent(requestWriter.ToString(), System.Text.Encoding.UTF8, "application/json");
            var responseMessage = await client.PostAsync(requestUri, content);
            responseMessage.EnsureSuccessStatusCode();
            var stream = await responseMessage.Content.ReadAsStreamAsync();
            using (JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(stream)))
            {
            return System.Int64.Parse(jsonReader.ReadAsString());
            }
            }
        }

you might be wondering why the codes couldn't be simpler like what shown below:

public async Task<long> CreatePersonAsync(DemoWebApi.DemoData.Client.Person p)
{
	var requestUri = new Uri(this.baseUri, "api/Entities");
	var responseMessage = await client.PostAsJsonAsync(requestUri, p);
	responseMessage.EnsureSuccessStatusCode();
	var stream = await responseMessage.Content.ReadAsStreamAsync();
	using (JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(stream)))
	{
	var serializer = new JsonSerializer();
	return System.Int64.Parse(jsonReader.ReadAsString());
	}
}

Actually the earlier versions before v1.5 of WebApiClientGen were using the second form. However, to support Tuple and Xamarin, the first form had become used.

Reasons:

  1. PostAsJsonAsync is actually using JsonMediaTypeFormatter which is not handling Tuple well.
  2. JsonMediaTypeFormatter is implemented in assembly System.Net.Http.Formatting which is unavailable to Xmarin.
Clone this wiki locally