我有一个Xamarin应用程序,我想要连接到我的FiRestore数据库. 我用的是‘Plugi.GoogleFiRestore’,但它太旧了,我可以用这个包来发布我的应用程序,所以我下载了‘Google.Cloud.Firebase’和‘Google.Cloud.Firebase.V1’包.它们工作得很好.我在我的移动应用程序和Windows应用程序中使用它们. 对于我的Windows应用程序,它们工作正常,但我的Xamarin移动应用程序有问题.

我无法连接应用程序和数据库,因为系统找不到json文件. 这是错误:

System.InvalidOperationException: Error reading credential file from location app-firebase-adminsdk.json: Could not find file "/app-firebase-adminsdk.json"
Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS. ---> System.IO.FileNotFoundException: Could not find file "/app-firebase-adminsdk.json"
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001aa] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:239 
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:149 
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:86 
  at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.GetStream (System.String filePath) [0x00000] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:380 
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialFromFileAsync (System.String credentialPath, System.Threading.CancellationToken cancellationToken) [0x0002f] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:162 
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.<CreateDefaultCredentialAsync>g__GetAdcFromEnvironmentVariableAsync|11_0 () [0x0005c] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:108 
   --- End of inner exception stack trace ---
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.<CreateDefaultCredentialAsync>g__GetAdcFromEnvironmentVariableAsync|11_0 () [0x000b0] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:114 
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialAsync () [0x00037] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:85 
  at Google.Api.Gax.Grpc.DefaultChannelCredentialsCache.<.ctor>b__5_1 () [0x00027] in /_/Google.Api.Gax.Grpc/DefaultChannelCredentialsCache.cs:51 
  at Google.Api.Gax.TaskExtensions.WaitWithUnwrappedExceptions (System.Threading.Tasks.Task task) [0x00009] in /_/Google.Api.Gax/TaskExtensions.cs:51 
  at Google.Api.Gax.TaskExtensions.ResultWithUnwrappedExceptions[T] (System.Threading.Tasks.Task`1[TResult] task) [0x00000] in /_/Google.Api.Gax/TaskExtensions.cs:30 
  at Google.Api.Gax.Grpc.DefaultChannelCredentialsCache.GetCredentials () [0x00000] in /_/Google.Api.Gax.Grpc/DefaultChannelCredentialsCache.cs:67 
  at Google.Api.Gax.Grpc.ChannelPool.GetChannel (Google.Api.Gax.Grpc.GrpcAdapter grpcAdapter, System.String endpoint, Google.Api.Gax.Grpc.GrpcChannelOptions channelOptions) [0x00018] in /_/Google.Api.Gax.Grpc/ChannelPool.cs:74 
  at Google.Api.Gax.Grpc.ClientBuilderBase`1[TClient].CreateCallInvoker () [0x00034] in /_/Google.Api.Gax.Grpc/ClientBuilderBase.cs:366 
  at Google.Cloud.Firestore.V1.FirestoreClientBuilder.BuildImpl () [0x00006] in /_/apis/Google.Cloud.Firestore.V1/Google.Cloud.Firestore.V1/FirestoreClient.g.cs:414 
  at Google.Cloud.Firestore.V1.FirestoreClientBuilder.Build () [0x00001] in /_/apis/Google.Cloud.Firestore.V1/Google.Cloud.Firestore.V1/FirestoreClient.g.cs:400 
  at Google.Cloud.Firestore.FirestoreDbBuilder.Build () [0x00056] in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/FirestoreDbBuilder.cs:105 
  at Google.Cloud.Firestore.FirestoreDb.Create (System.String projectId, Google.Cloud.Firestore.V1.FirestoreClient client) [0x00000] in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/FirestoreDb.cs:104 

这是我的代码:

string path = AppDomain.CurrentDomain.BaseDirectory + @"app-firebase-adminsdk.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
FirestoreDb databaseFS = FirestoreDb.Create("appmobile");

我也试过了

string path = Path.Combine(FileSystem.AppDataDirectory, @"app-firebase-adminsdk.json")

而不是

string path = AppDomain.CurrentDomain.BaseDirectory + @"app-firebase-adminsdk.json";

但它不起作用..

.json文件安装在我的应用程序项目中,包含:

  • 生成操作:嵌入资源
  • 复制到输出目录:不复制

在Xamarin.Android和Xamarin.iOS中为:

  • 生成操作:内容
  • 复制到输出目录:始终复制

你有什么 idea 吗?

推荐答案

您可能为.json文件设置了错误的Build Action.

对于Android系统:

  • 将任何文件添加到Android项目的Assets文件夹中,并标记 作为androidAsset使用它的构建操作 OpenAppPackageFileAsync.

对于iOS:

  • 将任何文件添加到iOS项目的Resources文件夹中并标记 作为与其一起使用的绑定资源的生成操作 OpenAppPackageFileAsync.

Csharp相关问答推荐

如何将ref T*重新解释为ref nint?

C# Json重新初始化动态类型

如何从顶部提取发票号作为单词发票后的第一个匹配

我无法在Ubuntu下编译使用microsoft.extension.configurationbuilder jsonapi和mono mcs的c#应用程序

如何模拟耐久任务客户端在统一测试和获取错误在调度NewsListationInstanceAsync模拟设置

.NET 6控制台应用程序,RabbitMQ消费不工作时,它的程序文件中的S

查找表中的模式

ASP.NET Core AutoMapper:如何解决错误 CS0121调用在以下方法或属性之间不明确

.NET SDK包中的官方C#编译器在哪里?

单元测试:模拟返回空

Blazor Fluent UI DialogService,<;FluentDialogProvider/>;错误

为什么Docker中没有转发该端口?

在使用UserManager时,如何包含与其他实体的关系?

MudBlazor Textfield已禁用,但其验证工作正常

Blazor:搜索框在第一次搜索时不搜索

RCL在毛伊岛应用程序和Blazor服务器应用程序.Net 8.0中使用页面

当`JToken?`为空时?

ASP.NET Core 8 Web API:如何添加版本控制?

使用Try-Catch-Finally为API端点处理代码--有什么缺点?

如何在C#中用Serilog记录类路径、方法名和行编号