c# - Grpc.Auth:无法从程序集“Grpc.Core.Api”加载类型“Grpc.Core.CallCredentials”
问题描述
语境
我正在尝试使用 Google 的 Cloud Natural Language API。我有我的服务帐户密钥 JSON 文件,并且正在尝试编写一个简单的 .NET Core 应用程序(更具体地说是使用 .NET Core 的 Azure 函数),它将接收一些文本并使用来自自然语言 API 的情绪分析函数并返回几个值。
我的实现基于 Google 的文档,特别是标题下的代码部分:
以下是我的申请:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Google.Cloud.Language.V1;
using Google.Apis.Auth.OAuth2;
using Grpc.Auth;
namespace Project.Function
{
public static class GoogleNLAPI
{
[FunctionName("GoogleNLAPI")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Admin, "get", "post", Route = null)] HttpRequest req,
ILogger log,
ExecutionContext context)
{
string content = req.Query["content"];
var path = Path.Combine(context.FunctionAppDirectory, "{FILE-NAME}");
var credential = GoogleCredential.FromFile(path)
.CreateScoped(LanguageServiceClient.DefaultScopes);
var channel = new Grpc.Core.Channel(
LanguageServiceClient.DefaultEndpoint.ToString(),
credential.ToChannelCredentials()
);
var languageClient = LanguageServiceClient.Create(channel);
var response = languageClient.AnalyzeSentiment(new Document()
{
Content = content,
Type = Document.Types.Type.PlainText
});
var sentiment = response.DocumentSentiment;
return new OkObjectResult($"Score: {sentiment.Score}\nMagnitude: {sentiment.Magnitude}");
}
}
}
和 .csproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
<RootNamespace>google_nl_api</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.28"/>
<PackageReference Include="Google.Cloud.Language.V1" Version="1.2.0"/>
<PackageReference Include="Google.Apis.Auth" Version="1.40.2"/>
<PackageReference Include="Grpc.Auth" Version="1.21.0"/>
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="{FILE-NAME}">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
问题
当我运行它时,我收到以下错误:
Exception while executing function: GoogleNLAPI. Grpc.Auth: Could not load type 'Grpc.Core.CallCredentials' from assembly 'Grpc.Core.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d754f35622e28bad'.
我正在使用 Grpc.Auth 因为没有它我无法使用.ToChannelCredentials()
,这似乎是发生错误的方法。
解决方案
更具体地说是使用 .NET Core 的 Azure 函数
不幸的是,这就是问题所在。
至少在emulator中,存在一个问题,即 emulator 包含比 1.21.0 更旧版本的 Grpc.Core。通常只有在使用新功能时才会出现问题,但在 1.19.0 (IIRC) 左右,Grpc.Core 被拆分为 Grpc.Core 和 Grpc.Core.Api,类型转发处理兼容性问题。这很好,直到您使用期望存在拆分的代码,但发现已加载旧版本的 Grpc.Core。
此问题已报告给 Microsoft,但我还没有看到任何解决方法。请注意,您甚至不需要任何直接引用Google.Apis.Auth
或Grpc.Auth
- 请参阅我在该问题的最终评论中发布的重现。
推荐阅读
- html - 透明覆盖 div 层脱离父容器?
- arrays - 使用 CFArrayCreate 创建 CFArrayRef 时元素的混乱
- javascript - 无法使用下一个 js 导出路径图?
- mysql - 使用 SQL SELECT 从现有列数据创建新列
- git - 如果参数包含波浪号,System.Diagnostics.ProcessStartInfo 将挂起
- java - Java中的CSVWriter编码
- office365 - Microsoft graph people\me 返回空的用户列表
- angular - 使用 @OutPut 在子父 .ts 组件之间共享对象
- java - java.io.NotSerializableException: sun.print.Win32PrintService
- python-3.x - 如何使用两个 for 循环将列表变量复制到数据框中的位置变量中?