Xamarin Form: API receives always null data when posting from Xamarin Form after Archiving Android Application


Post Method in Xamarin Forms is shown below,

public async Task VerifyMobile(OtpDto otpDto)
    _httpClient.BaseAddress = new Uri("https://***.azurewebsites.net");

    var jsonData = JsonConvert.SerializeObject(otpDto);

    var content = new StringContent(jsonData, Encoding.UTF8, "application/json");

    HttpResponseMessage response = await _httpClient.PostAsync("api/Auth/VerifyMobile", content);

And API Method is like,

public async Task<IActionResult> VerifyMobile([FromBody]OtpDto otpDto)

And the OtpModel is same in both Xamarin Forms and API as shown below,

public class OtpDto
    public int Id { get; set; }
    public string PhoneNumber { get; set; }
    public int? OneTimePassword { get; set; }
    public DateTime? OtpGeneratedDateTime { get; set; }
    public string AppHashKey { get; set; }

The problem is, when posting the data from local, API is receiving the data but when I tried by Archiving (Converting to .apk File) the Android Application, am receiving null in the API.

Note: Receiving Null in API I got to know from logging and the logging message is as shown below,

Hosting environment: Production
Content root path: D:\home\site\wwwroot
Application started. Press Ctrl+C to shut down.
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (17ms) [Parameters=[@p0='?' (DbType = Int32), @p1='?' (DbType = DateTime), @p2='?' (Size = 50) (DbType = AnsiString)], CommandType='Text', CommandTimeout='30']
      INSERT INTO [Otp] ([OneTimePassword], [OtpGeneratedDateTime], [PhoneNumber])
      VALUES (@p0, @p1, @p2);
      SELECT [Id]
      FROM [Otp]
      WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
System.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'PhoneNumber', table 'UDMeShop.dbo.Otp'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Do I need to do something extra when am Archiving the Android Application? or Am I missing something?

asked on Stack Overflow Sep 5, 2019 by Chandan Y S • edited Sep 5, 2019 by Chandan Y S

1 Answer


The problem is you are trying to insert a null value into a column in your SQL server that is not nullable. There are a couple things we could do to prevent this. The first would be having model validation. So you can add an attribute like this:

public string PhoneNumber { get; set; }

Within you controller you can test that the model is valid by doing this:

if (!ModelState.IsValid)
   // Handle invalid model here.

Regarding your concern of the requests, often a situation like this would not be because of the difference between running it as an APK or through visual studio. However, you may consider building your APK without Linking enabled under project options. This would rule out the linker being an issue. See: https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker. Another issue, which I don't see based on your code is if you had a different URL configured in your release build - I don't think this would be the case in your situation just pointing it out. And my last tip would be compare the actual data received and sent and look closely at the request/response. I would be willing to bet this will give you the information you need to solve this issue. You can use a tool called post man or other methods to run various tests as well. Make sure to debug and inspect what the models look like going out and coming in.

Hope that helps.

answered on Stack Overflow Sep 5, 2019 by jazzmasterkc

User contributions licensed under CC BY-SA 3.0