1

I have been trying to find examples of CRUD Firebase database for C#. I have researched some websites and tried doing on my own, but not all are complete nor updated examples.

May I know if there is a complete example of such?

I have tried using HttpWebResponse to query from Firebase.

So this is my sample code

public void getUser()
        {
            var request = (HttpWebRequest)WebRequest.Create(apiUrl + "user.json?orderBy=\"email\"&equalTo=" + "\"" + userId + "\"");

            request.Method = "GET";
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var js = new JavaScriptSerializer();
                    var objText = reader.ReadToEnd();
                    var result = JsonConvert.DeserializeObject<List<User>>(objText);
                }
            }
}

My User class

public string email { get; set; }
public string firstName { get; set; }
public string @group { get; set; }
public string lastName { get; set; }

However, when I call getUser, it returns me null. But inside the objText, there is text in it.

E.g.

{"12345678":{"email":"user@hotmail.com","firstName":"Alan","group":"N","lastName":"Paul"}}
Arane
  • 323
  • 4
  • 19
  • May be you can share whatever you have tried and tell us what issue you are facing in that. Are you able yo write s simple console application to do CRUD operations on Firebase? – Chetan Jul 10 '18 at 09:46
  • @ChetanRanpariya Alright, I will update on the question now to show some things that I have done and some issues I have faced – Arane Jul 10 '18 at 09:50
  • @ChetanRanpariya I have updated already. Please have a look, thank you. – Arane Jul 10 '18 at 09:53
  • Did you try https://stackoverflow.com/questions/41676714/using-firebase-in-net? – Chetan Jul 10 '18 at 09:58
  • Did you debug the code? What do you get in `objText` variable? – Chetan Jul 10 '18 at 09:59
  • @ChetanRanpariya the example is the string I get from `objText`. – Arane Jul 10 '18 at 10:01
  • Got it..its the issue with json deserialization. The json you are getting is not of `List`. That's why you are getting this error. – Chetan Jul 10 '18 at 10:05
  • @ChetanRanpariya Why `List` ? I am doing for `User` class though. Anyway if that's the issue, how do I solve it? I have tried many ways of deserializing it but it doesn't work. – Arane Jul 10 '18 at 10:17

1 Answers1

3

You are trying to get the User data from the Firebase database and you are able to get the data successfully too.

The issue is with the JSON deserialization. The JSON you are getting from Firebase can not be deserialized to List<User>. Also it can not be deserialized to a single object of User.

The reason for this is the format of the JSON. As per the JSON the target class should have a property with name 12345678 and the type of the property should be User. And your User class not like that. I assume that the value "12345678" is the userId.

What you can do is to convert the JSON to a dictionary with string as Key and User as value holding type and then get the User object from the dictionary.

Consider following code. (I am writing only necessary code here)

var objText = reader.ReadToEnd();
var usersDictionary = JsonConvert.DeserializeObject<Dictionary<string, User>>(objText);
// I assume that the value `12345678` represents the UserId.
// If my assumption is right, following code gives you the user object from the dictionary.

var result = usersDictionary[userId];
//this line of code may throw an exception coz the dictionary does not have the userId in it 
// this could be due to Firebase did not return any data in "objText".

This way you can get the user object out or the response text from the Firebase.

Chetan
  • 6,711
  • 3
  • 22
  • 32