【发布时间】: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,但不适用于远程服务器。
感谢您的帮助。
【问题讨论】:
-
根据我的经验,您应该能够使用相同的密钥,但为使用实例创建不同的密钥是一种很好的做法