微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

装有Nuget的.net Project上的Firebase Admin SDK总是给我一个错误

如何解决装有Nuget的.net Project上的Firebase Admin SDK总是给我一个错误

我正在尝试使用nuget在.Net应用程序上安装Firebase Admin。一切看起来都很好:

  • 参考指向正确的位置
  • packages文件夹中装有二进制文件
  • 配置文件包含正确的引用并具有正确的版本
  • 所有依赖项都包括在内
  • 项目建立

但是,每次我尝试使用包含FirebaseAdmin类的方法时,它都会引发System.IO.FileNotFoundException。我尝试了许多解决方案:

  • 重新安装所有软件包
  • 清洁解决方案,然后重新安装
  • 删除软件包文件夹并还原软件包
  • 在干净的环境中安装并手动引用DLL

什么都没有。还有其他人遇到此错误吗?如果是这样,您如何解决

P.S。我对Nuget有点绿色。我一直在从事使用它的项目的研究。我的理解是,它应该是一个精简而简单的软件包管理器。到目前为止,我还没有安装新的软件包。我的经验不过是-到目前为止,知道一个所谓的好软件包管理者的来龙去脉一直是我的优先事项。欢迎任何帮助,甚至是相对明显的指点。

编辑1:我知道Firebase需要初始化,但是我一直在尝试将初始化放入模型的方法而不是项目启动中。

namespace Sis.Onesis.Business.Models.Notification
{
    [DataContract]
    public class NotificationFramework
    {

    #region Methods
    public static void SendNotification(int userId,int siteId,int yearId,string enumeration,int object_ID,string payload)
    {

        try
        {
            // Running the stored procedure
            V10Data.V10DataContextProvider modelContainer = new V10Data.V10DataContextProvider();
            List<sqlParameter> parameterList = new List<sqlParameter>();
            parameterList.Add(new sqlParameter { ParameterName = "@Enumeration",Value = enumeration,sqlDbType = sqlDbType.VarChar,Direction = ParameterDirection.Input });
            parameterList.Add(new sqlParameter { ParameterName = "@Payload",Value = payload,sqlDbType = sqlDbType.Xml,Direction = ParameterDirection.Input });
            parameterList.Add(new sqlParameter { ParameterName = "@Object_ID",Value = object_ID,sqlDbType = sqlDbType.Int,Direction = ParameterDirection.Input });
            parameterList.Add(new sqlParameter { ParameterName = "@User_ID",Value = userId,Direction = ParameterDirection.Input });
            parameterList.Add(new sqlParameter { ParameterName = "@Site_ID",Value = siteId,Direction = ParameterDirection.Input });
            parameterList.Add(new sqlParameter { ParameterName = "@Year_ID",Value = yearId,Direction = ParameterDirection.Input });
            DataSet dataSet = modelContainer.ExecuteGetProcedure("spNTF_ProcessNotification",parameterList);

            // Retrieving the notifications
            List<NotificationLogDTO> notificationLogs = new List<NotificationLogDTO>();
            foreach (DaTarow row in dataSet.Tables[0].Rows)
            {
                NotificationLogDTO notificationLog = new NotificationLogDTO();
                notificationLog.ID = row.Field<int>("ID");
                notificationLog.NotificationType_ID = row.Field<int>("NotificationType_ID");
                notificationLog.NotificationEvent_ID = row.Field<int>("NotificationEvent_ID");
                notificationLog.User_ID = row.Field<int>("User_ID");
                notificationLog.Object_ID = row.Field<int>("Object_ID");
                notificationLog.Student_ID = row.Field<int?>("Student_ID");
                notificationLog.Message = row.Field<string>("Message");
                notificationLog.Subject = row.Field<string>("Subject");
                notificationLog.AddedOn = row.Field<DateTime>("AddedOn");
                notificationLog.AddedBy = row.Field<int>("AddedBy"); 
                notificationLog.EmailAddress = row.Field<string>("EmailAddress");
                notificationLogs.Add(notificationLog);
            }

            // Sending the notifications
            using (CrudEngine crudEngine = new CrudEngine())
            {
                string defaultEmailAddress = "noreply@tylertech.com";
                var version = Sis.Onesis.Server.Configuration.AppConfiguration.RetrieveResourceVersion();
                if (version != null)
                {
                    if (version.Value == "V9")
                    {
                        defaultEmailAddress = crudEngine.Read<V9DomainModel.tbldistrict>()
                            .Where(o => o.blnExternal == false)
                            .Select(o => o.strEmailDefaultEAddress)
                            .SingleOrDefault();
                    } else
                    {
                        defaultEmailAddress = crudEngine.Read<V10DomainModel.SystemSetting>()
                            .Select(o => o.NotificationEmail)
                            .SingleOrDefault();
                    }
                }
                Dictionary<int,List<NotificationLogDTO>> userMobileNotifications = new Dictionary<int,List<NotificationLogDTO>>();
                foreach (NotificationLogDTO notification in notificationLogs)
                {
                    switch (notification.NotificationType_ID)
                    {
                        // Email
                        case 1:
                            SendEmailParameters parameters = new SendEmailParameters();
                            parameters.EmailFrom = defaultEmailAddress;
                            parameters.EmailTo = notification.EmailAddress;
                            parameters.Body = notification.Message;
                            parameters.Subject = notification.Subject;
                            sendEmailDTO.SendEmail(parameters,-1,userId,"Classroom");
                            break;

                        // Mobile Notification
                        case 3:
                            if (!userMobileNotifications.Keys.Contains(notification.User_ID))
                            {
                                userMobileNotifications.Add(notification.User_ID,new List<NotificationLogDTO>());
                            }
                            userMobileNotifications[notification.User_ID].Add(notification);
                            break;
                    }
                }

                // Sending mobile notifications
                FirebaseApp firebaseApp = FirebaseApp.DefaultInstance;
                if (firebaseApp == null)
                {
                    DataTable credentials = modelContainer.ExecutesqlQuery("SELECT TOP 1 * FROM NTF_FirebaseCredentials");
                    if (credentials.Rows.Count > 0)
                    {
                        string firebaseJSON = credentials.Rows[0].Field<string>("FirebaseJSON");
                        FirebaseApp.Create(new AppOptions()
                        {
                            Credential = GoogleCredential.FromJson(firebaseJSON),});
                    }
                }
                FirebaseMessaging firebaseMessaging = FirebaseMessaging.DefaultInstance;
                if (firebaseMessaging == null)
                {
                    firebaseMessaging = FirebaseMessaging.GetMessaging(firebaseApp);
                }
                List<NotificationUserDevice> userDevices = NotificationUserDevice.GetUserDevices(userId,userMobileNotifications.Keys.ToList());
                List<Message> messages = new List<Message>();
                foreach (NotificationUserDevice userDevice in userDevices)
                {
                    List<NotificationLogDTO> notifications = userMobileNotifications[userDevice.User_ID];
                    foreach (NotificationLogDTO notification in notifications)
                    {
                        Message message = new Message();
                        message.Token = userDevice.Devicetoken;
                        FirebaseAdmin.Messaging.Notification mobileNotification = new FirebaseAdmin.Messaging.Notification();
                        mobileNotification.Title = notification.Subject;
                        mobileNotification.Body = notification.Message;
                        message.Notification = mobileNotification;
                    }
                }
                firebaseMessaging.SendAllAsync(messages);
            }
        }
        catch (Exception ex)
        {
            string errMsg = "An error occurred -";
            LogServices.Error(ex,"userId:{0}:customMessage:{1}:",errMsg);
            throw ex;
        }
    }
}

解决方法

您对NuGet的理解对于绝大多数软件包都是准确的,但是FirebaseAdmin需要一些其他设置。

此处详细描述了整个过程:https://firebase.google.com/docs/admin/setup#initialize-sdk

我将在这里指出最重要的内容(例如TL; DR)。首先,您将需要一个json文件形式的firebase私钥(在“初始化SDK”下的链接中简要描述了这些步骤)。之后,请确保在调用与Firebase相关的功能之前运行这段代码。

FirebaseApp.Create(new AppOptions()
{
    Credential = GoogleCredential.FromFile("path/to/what/you/downlowded.json"),});

我希望这能解决您的问题。

,

我发现了问题所在。我们的解决方案之一内置于IIS引用的单独项目中。该项目需要对DLL的手动引用,因此我手动更新了该项目的引用,并从那里开始工作。如果您正在处理具有多个解决方案的分层项目,那么这也可能适用于您。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?