Unable to find a constructor to use for type Realms.Sync.User

I am writing code to maintain users in Realm, create, delete, etc. I successfully retrieve a user record using the following code;

string body = JsonConvert.SerializeObject(new { email = userName, password = $"{pin}" });
StringContent requestContent = new StringContent(body, Encoding.UTF8, "application/json");

using (HttpClient client = new HttpClient() { BaseAddress = new Uri($"{_baseUrl}") })
	client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
	client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _authResponse.AccessToken);

	HttpResponseMessage response = await client.PostAsync($"groups/{_projectId}/apps/{_appId}/users", requestContent).ConfigureAwait(true);

	if (response.IsSuccessStatusCode)
		string content = await response.Content.ReadAsStringAsync();
		user = JsonConvert.DeserializeObject<Realms.Sync.User>(content);

Line 15 throws the following exception;

Unable to find a constructor to use for type Realms.Sync.User. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.

Realms.Sync.User is not a type that is (de)serializable. Can you clarify your use case a little bit better? Perhaps there’s a different way to achieve your goal, but I’m having a hard time wrapping my head around it.

One scenario; I’m trying to get a list of all the users from Realm and return them as objects for use in my code.

You should probably create a custom class to hold the user information in this case as you wouldn’t be able to use those fetched users as arguments to API that expect a Realms.Sync.User. They’re also unlikely to contain the same properties as the build-in User class.

Thanks Nikola,

Just as a matter of interest, if I wanted to retrieve the UserId for a user so that I could then delete them using the User API’s how would someone normally do this? Would I need to login as the user in code to retrieve the User using the SDK then call the delete user API?

What delete user API are you referring to? The .NET SDK doesn’t expose an API to delete users as that is considered admin functionality and can be accessed either via the cli or the http API.

Regarding obtaining the User’s Id - it really depends on what is the flow you have in mind. I.e. how do you decide which user to delete? Do you pick from a list of existing users? If so, then the users in the list should probably have an Id property. If it’s the user who requests data deletion themselves, then they should probably do it while authenticated, meaning that at this point they know what their Id is.

Thanks for explaining that. I wasn’t saving the user IDs in my initial code so I will now include it.