1

I am exploring Firebase Realtime Database (RTDB) with C#, and am trying out the Firesharp framework on a .NET console app. I have followed most online tutorials on getting started, but was not able to post any data to my RTDB. I am thrown this error at AddData().Wait():

System.AggregateException has been thrown

"One or more errors occurred. (An error occured while execute request. Path : 0/ , Method : PUT)"

My implementation is like so:

class Program
{
    static IFirebaseConfig config = new FirebaseConfig
    {
        AuthSecret="MY_AUTH_SECRET",
        BasePath="https://MY_APP.firebaseio.com/"
    };

    static IFirebaseClient client;

    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        client = new FireSharp.FirebaseClient(config);

        if (client != null)
        {
            Console.WriteLine("Connection established!");
            AddData().Wait(); //Crashed here with above error
        }
    }

    public static async Task AddData() 
    {
        var data = new Data
        {
            id = "Hello",
            text = "World"
        };

        SetResponse response = await client.SetAsync("0/", data);
        Data result = response.ResultAs<Data>();
    }
}

I am fairly new to C#. Where have I gone wrong?

I'm using VS on Mac, and hence not writing a form app to test instead.

Edit

Full error log:

System.AggregateException: "One or more errors occurred. (An error occured while execute request. Path : 0 , Method : PUT)" ---> System.Exception {FireSharp.Exceptions.FirebaseException}: "An error occured while execute request. Path : 0 , Method : PUT" ---> System.Exception {System.IO.FileNotFoundException}: "Could not load file or assembly 'System.Security.Permissions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.\n"

at at Newtonsoft.Json.Serialization.JsonTypeReflector.get_DynamicCodeGeneration()\n at Newtonsoft.Json.Serialization.JsonTypeReflector.get_ReflectionDelegateFactory()\n at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator(Type createdType)\n at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)\n at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)\n at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)\n at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\n at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)\n at FireSharp.RequestManager.PrepareRequest(HttpMethod method, Uri uri, Object payload)\n at FireSharp.RequestManager.RequestAsync(HttpMethod method, String path, QueryBuilder queryBuilder, Object payload)

--- End of inner exception stack trace --- at FireSharp.RequestManager.RequestAsync(HttpMethod method, String path, QueryBuilder queryBuilder, Object payload)\n at FireSharp.FirebaseClient.SetAsync[T](String path, T data)\n at FiresharpTest.Program.AddData() in /Users/MYNAME/Desktop/C#Projects/FiresharpTest/FiresharpTest/Program.cs:40

--- End of inner exception stack trace --- at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)\n at System.Threading.Tasks.Task.Wait()\n at FiresharpTest.Program.Main(String[] args) in /Users/MYNAME/Desktop/C#Projects/FiresharpTest/FiresharpTest/Program.cs:28

Community
  • 1
  • 1
Koh
  • 2,687
  • 1
  • 22
  • 62
  • Try `await client.SetAsync("0", data);` instead – samthecodingman Apr 29 '19 at 14:06
  • Are you sure the client has permission to write to the database? By default nowadays no such permission is granted. – Frank van Puffelen Apr 29 '19 at 14:08
  • @FrankvanPuffelen How do i check on this? This line `Console.WriteLine("Connection established!");` prints successfully, so I presume its connected. I have also checked the security rules on firebase console, and are set to public. – Koh Apr 29 '19 at 14:45
  • @samthecodingman have tried that as well, but still throws the same error. – Koh Apr 29 '19 at 14:46
  • Hmm...public read/write should be good enough. I'm not sure what's going wrong in that case, since the error message is rather vague. Is there any way you can see if there's a nested exception? – Frank van Puffelen Apr 29 '19 at 15:07
  • @FrankvanPuffelen I have added the full log inside my question. See edit – Koh Apr 29 '19 at 15:34
  • There seem to be lots of other error messages in there, about an assembly not loading, and an issue with parsing JSON. Did you search for those yet? – Frank van Puffelen Apr 29 '19 at 15:39
  • @FrankvanPuffelen I do not quite understand where does all these json serialization errors originate from, i am barely getting started. About the assembly loading, what file are we talking about? – Koh Apr 29 '19 at 15:45
  • this is the tutorial that i follow: https://www.youtube.com/watch?v=jZMwwZHJXJc&list=PLuv4McUMmyAbsNdfZ4rH_OyEM3P2MJS-F – Koh Apr 29 '19 at 15:46
  • did you solve the problem ? – DarioN1 May 08 '19 at 12:29
  • @DarioN1 yes I did, pls see answer below. – Koh May 09 '19 at 15:07
  • my problem was related to different version of newtonsoft dll... – DarioN1 May 09 '19 at 15:35

1 Answers1

0

Ok, so the problem to this issue was that I had two Nuget packages installed, Firesharp and Firesharp.Serialization.JsonNet.

I removed Firesharp and then made amendments to my AddData() function, and it all worked.

public static async Task AddData() 
{
    var data = new Data
    {
        id = "Hello",
        text = "World"
    };

    SetResponse response = await client.SetTaskAsync("0", data); //Change to SetTaskAsync here
    Data result = response.ResultAs<Data>();
}
Koh
  • 2,687
  • 1
  • 22
  • 62