NullReferenceException in Release mode of a Xamarin app

In a simple POST request for my REST API, I serialize objects to JSON like this:

var userDto = new { user = new { login = Username, password = Password } };
var jsonPayload = JsonConvert.SerializeObject(userDto, Formatting.Indented);

      

in debug mode everything works fine, but in Release mode the jsonPayload

variable is null and in my try / catch I get NullReferenceException

. Why is the value of this variable different in Debug / Release and how can I solve this problem?

This is an android app and I have allowed internet permission.

+3


source to share


3 answers


Most likely your problem is with the Xamarin Linker , removing from unused assemblies from your code. The default link setting is different for release and debug versions, which is probably the reason you are seeing this difference.



Try setting the Link option (you can find it in the project settings for your Android project under Android Settings) for SDK builds only or None and see if that works.

+3


source


I had to backtrack using anonymous object because I don't know why JSON.NET

the Release

mode is causing this error. After redefining it as it should, everything works fine. Does anyone know why?;)



UserSignInModel uDTO = new UserSignInModel()
            {
                user = new UserSignInDTO()
                {
                    login = Username,
                    password = Password,
                }
            };

var jsonPayload = JsonConvert.SerializeObject(uDTO, Formatting.Indented);

      

+1


source


I got the same error, but I didn't find a real solution for it. After narrowing down when this issue started popping up, I found out that it happened after updating the json.net package.

This is the difference, Before:

<package id="Newtonsoft.Json" version="6.0.3" targetFramework="MonoAndroid403" />

      

After:

<package id="Newtonsoft.Json" version="6.0.6" targetFramework="MonoAndroid50" />

      

I just canceled the package as a workaround right now. I would like to hear some understanding and solution to this problem!

+1


source







All Articles