【问题标题】:How to Fix "the type initializer for 'FirebaseAdmin.FirebaseApp' threw an exception"如何修复“'FirebaseAdmin.FirebaseApp' 的类型初始化程序引发异常”
【发布时间】:2019-11-11 17:26:29
【问题描述】:

我有一个 wcf 服务,我试图连接到 Firebase,纯粹是为了推送消息服务。我需要将 xml 从经典的 asp 服务器(我知道它很旧,但我无法控制它)发送到我的 wcf web 服务,它将处理所有的 firebase 消息传递。

我在 wcf 服务中安装了 firebase admin sdk,并在我的本地主机上的 iis 中运行 wcf 服务时成功使用它向指定设备发送通知,但是当我发布到产品服务器时,我得到了上述信息FirebaseAdmin.FirebaseApp.Create() 方法调用的异常。

这是我在 wcf 服务中的代码:

public List<string> OnSafetyMessagePosted(List<user> users)
        {
            List<string> strings = new List<string>();

            if (users != null && users.Count > 0)
            {
                // Authenticate with Firebase using Google json auth file
                try
                {
                    if (FirebaseApp.DefaultInstance == null)
                    {
                        FirebaseApp.Create(new AppOptions()
                        {
                            Credential = GoogleCredential.FromFile($@" 
                                  {AppDomain.CurrentDomain.BaseDirectory}" +
                                  "\\project-####-firebase-adminsdk-####-######.json""),
                        });
                    }
                }
                catch (ArgumentException ex)
                {
                    throw new FaultException($"{AppDomain.CurrentDomain.BaseDirectory}" +
                    "\\project-####-firebase-adminsdk-####-######.json" + $"\n{ex.Message}");
                }
                catch (Exception ex)
                {
                    throw new FaultException(ex.Message + ex.StackTrace);
                }



                // This registration token comes from the client FCM SDKs.
                var registrationToken = "cXM76XuqHlY:.....";

                // See documentation on defining a message payload.
                var notification = new Notification()
                {
                    Title = "Hello From Firebase!",
                    Body = "Sample Notification"
                };


                var message = new Message()
                {
                    Notification = notification,
                    Data = new Dictionary<string, string>()
                    {
                        { "userID", users[0].userID.ToString() }
                    },
                    Token = registrationToken,
                };

                // Send a message to the device corresponding to the provided
                // registration token.
                string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;

                strings.Add(message.ToString());
            }

            return strings;
        }

我从邮递员那里得到的结果是 400 错误,堆栈跟踪如下:

The type initializer for
            'FirebaseAdmin.FirebaseApp' threw an exception. at FirebaseAdmin.FirebaseApp.Create(AppOptions options,
            String name)
            at FirebaseAdmin.FirebaseApp.Create(AppOptions options)
            at SeqCompanionSrv.SeqCompSrv.OnSafetyMessagePosted(List`1 users) in C:\source\repos\Sequence Companion
            Service\SeqCompanionSrv\SeqCompSrv.svc.cs:line 387'

我的怀疑是,由于我已经在我的本地主机上创建了一个 FirebaseApp 实例,因此相同的密钥不适用于服务器。我尝试为我的服务帐户生成一个新的密钥对并使用它,但我得到了相同的结果:它适用于 localhost,但不适用于远程服务器。

感谢您的帮助。

【问题讨论】:

  • 根据我的经验,您应该能够使用相同的密钥,但为使用实例创建不同的密钥是一种很好的做法

标签: c# firebase wcf


【解决方案1】:

我知道这是一篇旧帖子,但我也遇到了同样的问题,自己来寻找答案,但一无所获。

要修复,我的 Web.config 中缺少 Dependents!

    <dependentAssembly>
        <assemblyIdentity name="Google.Apis" publicKeyToken="4b01fa6e34db77ab" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.40.0.0" newVersion="1.40.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="Google.Apis.Auth" publicKeyToken="4b01fa6e34db77ab" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.40.0.0" newVersion="1.40.0.0" />
    </dependentAssembly>

希望它可以帮助某人:)

【讨论】:

  • 或者在我的情况下,它是 Web.config 中的错误版本,因为我刚刚更新了 Firebase SDK,它附带了对各种 Google.Apis.* 库的更新
【解决方案2】:

这可能不适合这个问题,但我来到这里时抛出了同样的异常。

我发现更新服务器时,文件丢失了……

System.Collections.Immutable.dll

它是 FirebaseAdmin 的依赖项之一

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2013-05-15
    • 2013-11-08
    • 2019-07-08
    • 2013-10-06
    相关资源
    最近更新 更多