问题描述
我正在尝试使用nuget在.Net应用程序上安装Firebase Admin。一切看起来都很好:
但是,每次我尝试使用包含FirebaseAdmin类的方法时,它都会引发System.IO.FileNotFoundException。我尝试了许多解决方案:
什么都没有。还有其他人遇到此错误吗?如果是这样,您如何解决?
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的手动引用,因此我手动更新了该项目的引用,并从那里开始工作。如果您正在处理具有多个解决方案的分层项目,那么这也可能适用于您。