Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FirebaseAdmin FirebaseApp检查Firebase实例是否存在_C#_Firebase_Firebase Authentication - Fatal编程技术网

C# FirebaseAdmin FirebaseApp检查Firebase实例是否存在

C# FirebaseAdmin FirebaseApp检查Firebase实例是否存在,c#,firebase,firebase-authentication,C#,Firebase,Firebase Authentication,我有一个C#项目,订阅一个主题的多个注册。由于项目的性质以及您无法查看有多少人已订阅某个主题的事实,我需要对服务器进行以下异步调用: 订阅注册 TopicManagementResponse response = await FirebaseMessaging.DefaultInstance.SubscribeToTopicAsync(registrationTokens, topic); TopicManagementResponse response = await FirebaseMes

我有一个C#项目,订阅一个主题的多个注册。由于项目的性质以及您无法查看有多少人已订阅某个主题的事实,我需要对服务器进行以下异步调用:

订阅注册

TopicManagementResponse response = await FirebaseMessaging.DefaultInstance.SubscribeToTopicAsync(registrationTokens, topic);
TopicManagementResponse response = await FirebaseMessaging.DefaultInstance.UnsubscribeFromTopicAsync(registrationTokens, topic);
向主题发送消息

string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
取消注册

TopicManagementResponse response = await FirebaseMessaging.DefaultInstance.SubscribeToTopicAsync(registrationTokens, topic);
TopicManagementResponse response = await FirebaseMessaging.DefaultInstance.UnsubscribeFromTopicAsync(registrationTokens, topic);
因为有三个调用需要使用凭据创建FirebaseApp实例:

FirebaseApp.Create(new AppOptions()
{
    Credential = GoogleCredential.FromFile(path),
});
但是,由于async POST返回“WaitingForActivation”响应(但它确实正确地执行了它应该执行的操作),我无法删除该实例以继续执行下一个函数,因为它会抛出错误,因为它无法重新创建另一个FirebaseApp实例-如果我给它一个名称,它将失败,因此我无法使用GetInstance(字符串名称)

我是否遗漏了什么,或者是否有其他方法可以做到这一点

以下是订阅函数的示例:

internal static async Task SubscribeToTopic(string path, string topic, string regID5, string regID)
{
    FirebaseApp app = FirebaseApp.Create(new AppOptions()
    {
        Credential = GoogleCredential.FromFile(path),
    });

    var registrationTokens = new List<string>()
    {
       regID5, regID
    };

    // Subscribe the devices corresponding to the registration tokens to the
    // topic

    try
    {
        TopicManagementResponse response = await FirebaseMessaging.DefaultInstance.SubscribeToTopicAsync(registrationTokens, topic);

        using (StreamWriter sw = System.IO.File.AppendText(HttpContext.Current.Server.MapPath("/tokens.txt")))
        {
            sw.WriteLine($"{response.SuccessCount} tokens were subscribed successfully");
        }

    }
    catch (Exception ex)
    {
        string myerror = ex.Message;
    }

}
内部静态异步任务订阅主题(字符串路径、字符串主题、字符串regID5、字符串regID)
{
FirebaseApp=FirebaseApp.Create(new AppOptions()
{
凭证=谷歌凭证.FromFile(路径),
});
var registrationTokens=新列表()
{
regID5,regID
};
//将与注册令牌对应的设备订阅到
//话题
尝试
{
TopicManagementResponse=等待FirebaseMessaging.DefaultInstance.SubscribeToPicasync(注册令牌,主题);
使用(StreamWriter sw=System.IO.File.AppendText(HttpContext.Current.Server.MapPath(“/tokens.txt”))
{
sw.WriteLine($“{response.SuccessCount}令牌已成功订阅”);
}
}
捕获(例外情况除外)
{
字符串myerror=ex.Message;
}
}

有什么想法吗?

您每次都在创建firebase实例。因此,您需要在global.asax文件中的application start中创建firebase实例