C#Firesharp设置异步空对象引用错误

时间:2018-10-02 08:03:38

标签: c# asp.net firebase firebase-realtime-database

我正在创建一个ASP.NET Web表单,用于跨多个与Web兼容的设备查看实时数据。我选择的实时数据库是Firebase,并且正在使用Firesharp SDK与数据进行交互。但是,当我尝试将数据插入数据库时​​,出现以下错误:System.NullReferenceException:

'Object reference not set to an instance of an object.'
FireData result = response.ResultAs<FireData>(); //on this line. 

在实现与数据库https://libraries.io/nuget/FireSharp的连接时,我遵循以下文档,但是我不确定在这种情况下为什么我会在复制了上述文档中的代码的情况下收到空对象引用错误。

这是我的课程结构:

public class FirebaseConnection
    {

        IFirebaseConfig config;
        IFirebaseClient client;
        FirebaseResponse firebaseResponse;
        FireData data;

        public FirebaseConnection(string secret, string path)
        {
            config = new FirebaseConfig {
                AuthSecret = secret,
                BasePath = path
            };

            client = new FireSharp.FirebaseClient(config);
        }


        public bool ConnectionStatus()
        {
            return client != null;
        }

        public IFirebaseConfig Config()
        {
            return this.config;
        }

        public IFirebaseClient Client()
        {
            return this.client; 
        }

        public async void WriteStream(FireData insertData)
        {
            SetResponse response = await client.SetTaskAsync("bluezonedemo/", insertData);
            FireData result = response.ResultAs<FireData>();
            Debug.WriteLine("Method has been called."); 
        }

        public async void ReadStream()
        {
            firebaseResponse = await client.GetTaskAsync("Info");
            data = firebaseResponse.ResultAs<FireData>();
        }

        public FireData RetrievedData()
        {
            return data; 
        }


    }

并在Default.aspx.cs中调用方法WriteStream:

FirebaseConnection firebaseConnection =
                new FirebaseConnection("mysecret",
                "https://mydb.firebaseio.com/");

public async Task UpdateFireDB()
        {
            Random rnd = new Random();
            firebaseConnection.WriteStream(new FireData(0, rnd.Next(1, 50)));
        }

        protected void Page_Load(object sender, EventArgs e)
        {


            RegisterAsyncTask(new PageAsyncTask(UpdateFireDB)); 

        }

最后,这是用于处理数据的类:

 public class FireData
    {
        public int Id { get; set; }
        public double Value { get; set; }

        public FireData(int identity, double inputValue)
        {
            Id = identity;
            Value = inputValue; 
        }

        public FireData()
        {

        }
    }

有人可以告诉我我做错了什么吗?

非常感谢,

本。

0 个答案:

没有答案