c# - Grpc.授权 : Could not load type 'Grpc.Core.CallCredentials' from assembly 'Grpc.Core.Api'

标签 c# authentication .net-core google-cloud-platform grpc

上下文

我正在尝试使用 Google 的 Cloud Natural Language API。我有我的服务帐户 key JSON 文件,并且正在尝试编写一个简单的 .NET Core 应用程序(更具体地说是一个使用 .NET Core 的 Azure 函数),它将接收一些文本并使用来自自然语言 API 的情绪分析功能并返回几个值。

我的实现基于 Google 的文档,特别是标题下的代码部分:

Passing the path to the service account key in code

以下是我的申请:

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(),这似乎是发生错误的方法。

最佳答案

more specifically an Azure Function using .NET Core

不幸的是,这就是问题所在。

至少在模拟器 中,存在一个问题,即模拟器包含比 1.21.0 更旧的 Grpc.Core 版本。通常只有在使用新功能时才会出现问题,但在 1.19.0 (IIRC) 左右,Grpc.Core 被拆分为 Grpc.Core 和 Grpc.Core.Api,类型转发处理兼容性问题。这很好,直到您使用的代码期望出现拆分,但发现加载的是旧版本的 Grpc.Core。

issue has been reported to Microsoft但我还没有看到任何解决方法。请注意,您甚至不需要对 Google.Apis.AuthGrpc.Auth 的任何直接引用 - 请参阅我在该问题的最终评论中发布的重现。

关于c# - Grpc.授权 : Could not load type 'Grpc.Core.CallCredentials' from assembly 'Grpc.Core.Api' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56708920/

相关文章:

c# - 使用集成 Windows 身份验证从 MVC 应用程序调用 Web API

c# - 无法使用C#连接zkteco指纹考勤机

c# - 在 iPhone 5 上测试时加载图像出错

javascript - 公共(public)第一方客户端的正确 OAuth2 流程

c# - 如何配置部署在 IIS 和远程客户端上的 WCF 服务以从远程客户端 PC 进行身份验证?

c# - JSON 补丁验证 .Net Core

c# - 如何从 Angular Reactive Form 接收文件上传?

c# - 如果您不打算从自适应呈现中获益,那么使用 HtmlTextWriter 有什么好处吗?

c# - 使用 c# 连接 Mongodb 并出现统一错误

node.js - 根据数据库字段区分登录响应: Express + Node + MongoDB