如何修复“'FirebaseAdmin.FirebaseApp'的类型初始值设定项引发异常”

我有一个wcf服务,试图将其纯粹用于推送消息服务而连接到Firebase。我需要将XML从经典的ASP服务器(我知道它很旧,但是我无法控制)发送到我的wcf Web服务,该服务将处理所有Firebase消息传递。

我在wcf服务中安装了firebase admin sdk,并在localhost上的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上运行,但不能在远程服务器上运行。

谢谢您的帮助。

qq872167458 回答:如何修复“'FirebaseAdmin.FirebaseApp'的类型初始值设定项引发异常”

我知道这是一篇过时的文章,但是我遇到了同样的问题,自己来寻找答案,却一无所获。

要修复,我的Web.config中缺少依赖项!

    <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>

希望它对某人有帮助:)

本文链接:https://www.f2er.com/3122425.html

大家都在问