【问题标题】:In Xamarin Forms, SetEnvironmentVariable在 Xamarin 表单中,SetEnvironmentVariable
【发布时间】: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


【解决方案1】:

如果您希望直接从您的移动应用程序中使用数据库,我建议您改用 Cloud Firestore (https://cloud.google.com/firestore/)。它具有明确的安全功能,允许直接从客户端访问数据库。

我认为您尝试使用上面的 sn-p 执行的操作是在应用中加载您的服务帐户凭据,这将使您的任何用户都可以完全访问您的数据库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多