Grpc.Auth:无法从程序集"Grpc.Core.Api"加载类型"Grpc.Core.CallCredentials" [英] Grpc.Auth: Could not load type 'Grpc.Core.CallCredentials' from assembly 'Grpc.Core.Api'

查看:133
本文介绍了Grpc.Auth:无法从程序集"Grpc.Core.Api"加载类型"Grpc.Core.CallCredentials"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google的Cloud Natural Language API.我有我的服务帐户密钥JSON文件,并尝试编写一个简单的.NET Core应用程序(更具体地说是使用.NET Core的Azure函数),该应用程序将使用一些文本并使用Natural Language API中的情感分析功能并返回几个价值观.

I'm trying to use Google's Cloud Natural Language API. I have my service account key JSON file, and am trying to write a simple .NET Core application (more specifically an Azure Function using .NET Core) that will take in some text and use the sentiment analysis function from the Natural Language API and return a couple values.

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

I'm basing my implementation off Google's documentation, specifically the code section under the heading:

将路径传递到代码中的服务帐户密钥

以下是我的应用程序:

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文件:

And the .csproj file:

<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>

问题

运行此命令时,出现以下错误:

Issue

When I run this, I get the following error:

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

I'm using Grpc.Auth because without it I am unable to use .ToChannelCredentials(), which seems to be the method where the error is occurring.

推荐答案

更具体地说,是使用.NET Core的Azure函数

more specifically an Azure Function using .NET Core

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

That's the problem, unfortunately.

至少在 emulator 中,存在一个问题,即该仿真器包含的Grpc.Core版本低于1.21.0.通常,只有在使用新功能的情况下,这才是问题,但是在1.19.0(IIRC)左右,Grpc.Core被分为Grpc.Core和Grpc.Core.Api,而类型转发则解决了兼容性问题.没问题,除非您使用期望要存在的拆分代码,但发现原来的Grpc.Core版本已被加载.

At least in the emulator, there's a problem where the emulator contains an older version of Grpc.Core than 1.21.0. Normally that would only be a problem if new features were being used, but around 1.19.0 (IIRC), Grpc.Core was split into Grpc.Core and Grpc.Core.Api, with type forwarding handling the compatibility issue. That's fine until you use code that expects the split to be present, but finds that the old version of Grpc.Core has been loaded instead.

问题已报告给Microsoft ,但我没有尚未看到任何解决方法.请注意,您甚至都不需要直接引用Google.Apis.AuthGrpc.Auth-请参阅我在该问题的最终评论中发布的repro.

This issue has been reported to Microsoft but I haven't seen any workarounds yet. Note that you don't even need any direct reference to Google.Apis.Auth or Grpc.Auth - see the repro I posted in the final comment on that issue.

这篇关于Grpc.Auth:无法从程序集"Grpc.Core.Api"加载类型"Grpc.Core.CallCredentials"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆