与Nuget一起安装的.net项目上的Firebase Admin SDK总是给我一个错误



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

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

但是,每次我尝试使用包含FirebaseAdmin类的方法时,它都会抛出System.IO.FileNotFoundException

  • 重新安装所有程序包
  • 清洁溶液,然后重新安装
  • 删除程序包文件夹并还原程序包
  • 在干净的环境中安装并手动引用DLL

什么都没有。其他人有这个错误吗?如果是,您是如何解决的?

附言:我对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, SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Input });
parameterList.Add(new SqlParameter { ParameterName = "@Site_ID", Value = siteId, SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Input });
parameterList.Add(new SqlParameter { ParameterName = "@Year_ID", Value = yearId, SqlDbType = SqlDbType.Int, 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, -1, -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}:", userId, 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,所以我手动更新了该项目的引用,并从那里开始工作。如果您正在处理一个具有多个解决方案的分层项目,这也可能适用于您。

相关内容

最新更新