【发布时间】:2019-02-28 15:24:38
【问题描述】:
在Xamarin Forms 和.Net Standard 代码共享应用程序中,我想为Google Datastore 设置环境变量。这样我就可以通过手机app和Google Datastore交流了。
以下代码在console 应用程序中运行良好,但在Xamarin Forms 尝试创建数据存储数据库对象时抛出错误。
从位置 /DB.json 读取凭据文件时出错:找不到文件“/DB.json” 请检查环境变量 GOOGLE_APPLICATION_CREDENTIALS 的值
我把 DB.json 放在解决方案的根目录上。
try
{
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"DB.json");
var dir = Environment.CurrentDirectory;
// Your Google Cloud Platform project ID.
string projectId = "xamarin-project";
//We are storing movies. So this is a Movie kind.
string kind = "Country";
//Create the datastore db
var db = DatastoreDb.Create(projectId);
// City entity
Entity cityEntities = new Entity
{
Key = db.CreateKeyFactory(kind).CreateKey($"US"),
["CountryCode"] = "US",
["Name"] = "United States"
};
//Lets send the city to the datastore
using (var transction = db.BeginTransaction())
{
transction.Upsert(cityEntities);
transction.Commit();
}
}
catch (Exception ex)
{
await DisplayAlert("Error", ex.Message, "OK");
Console.WriteLine(ex.Message);
}
使用Dependency 读取文件也不起作用。我尝试了.Android 项目。
【问题讨论】:
-
我不是 100% 确定,但我认为 iOS 和 Android 上没有环境变量之类的东西。至少不是您可以通过您的应用轻松访问或影响的应用
-
好的。感谢重播。
-
您是要让您的移动应用程序的任何用户直接访问 Cloud Datastore,还是只有数量有限的数据库管理员会使用您的应用程序?
-
@JimMorrison 我认为应用程序将由客户在内部使用,但我现在不确定。我对谷歌云平台很陌生。如果我允许任何用户访问怎么办?我怎样才能以安全的方式做到这一点?
标签: c# xamarin xamarin.forms google-cloud-datastore