【问题标题】:Having Issue while using Google Vision OCR API when hosted on IIS在 IIS 上托管时使用 Google Vision OCR API 时出现问题
【发布时间】:2026-01-01 13:55:01
【问题描述】:

将图像发送到 Google Api 函数时出现以下错误

        //Load the image file into memory
        var image = Google.Cloud.Vision.V1.Image.FromStream(uplFile.PostedFile.InputStream);


        // Instantiates a client
        var client = Google.Cloud.Vision.V1.ImageAnnotatorClient.Create();


        // Performs label detection on the image file
        var response = client.DetectDocumentText(image);

无法加载文件或程序集“Google.Apis.Auth, Version=1.21.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab' 或其之一 依赖关系。定位程序集的清单定义不 匹配程序集引用。 (HRESULT 异常:0x80131040)

StackTrace:在 Google.Api.Gax.Grpc.ChannelPool.CreateChannelCredentialsUncached 的 System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) 的 Google.Api.Gax.Grpc.ChannelPool.d__5.MoveNext() () 在 System.Threading.Tasks.Task`1.InnerInvoke() 在 System.Threading.Tasks.Task.Execute() --- 从先前抛出异常的位置结束堆栈跟踪--- 在 System.Runtime。 ExceptionServices.ExceptionDispatchInfo.Throw() 在 Google.Api.Gax.TaskExtensions.WaitWithUnwrappedExceptions(Task task) 在 Google.Api.Gax.Grpc.ChannelPool.GetChannel(ServiceEndpoint endpoint) 在 Google.Cloud.Vision.V1.ImageAnnotatorClient.Create( ServiceEndpoint 端点,ImageAnnotatorSettings 设置)在.....

package.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.SqlServer.Types" version="11.0.1" targetFramework="net40" />
<package id="Google.Api.CommonProtos" version="1.1.0" targetFramework="net45" />
<package id="Google.Api.Gax" version="2.2.1" targetFramework="net45" />
<package id="Google.Api.Gax.Grpc" version="2.2.1" targetFramework="net45" />
<package id="Google.Apis" version="1.30.0" targetFramework="net45" />
<package id="Google.Apis.Auth" version="1.30.0" targetFramework="net45" />
<package id="Google.Apis.Core" version="1.30.0" targetFramework="net45" />
<package id="Google.Cloud.Vision.V1" version="1.1.0" targetFramework="net45" />
<package id="Google.Protobuf" version="3.3.0" targetFramework="net45" />
<package id="Grpc.Auth" version="1.7.0" targetFramework="net45" />
<package id="Grpc.Core" version="1.7.1" targetFramework="net45" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net45" developmentDependency="true" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
<package id="System.Interactive.Async" version="3.1.1" targetFramework="net45" />
<package id="System.Net.Http" version="4.3.1" targetFramework="net45" />
</packages>

web.config

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
    <assemblyIdentity name="Google.Apis.Auth" publicKeyToken="4b01fa6e34db77ab" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.34.0.0" newVersion="1.34.0.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="Google.Apis.Auth.PlatformServices" publicKeyToken="4b01fa6e34db77ab" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.34.0.0" newVersion="1.34.0.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="Google.Protobuf" publicKeyToken="a7d26565bac4d604" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.6.0.0" newVersion="3.6.0.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="Google.Api.CommonProtos" publicKeyToken="3ec5ea7f18953e47" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="Google.Api.Gax" publicKeyToken="3ec5ea7f18953e47" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.4.0.0" newVersion="2.4.0.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="Google.Api.Gax.Grpc" publicKeyToken="3ec5ea7f18953e47" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.4.0.0" newVersion="2.4.0.0" />
    </dependentAssembly>
    </assemblyBinding>
</runtime>

【问题讨论】:

  • -相同的代码在本地机器上工作
  • -.Net framework 使用的是 4.5
  • 请检查部署在IIS上的代码中是否存在Google.Apis.Auth.dll
  • Yes..Google.Apis.Auth.dll 存在于版本 1.30.0 @IpsitGaur
  • 但是您的代码要求版本为 1.21.0,如错误所示,本地环境中是否提供相同的版本?

标签: c# google-api google-cloud-platform ocr google-vision


【解决方案1】:

首先,我会检查您的项目文件在哪里寻找 DLL。您可以通过在 Visual Studio 中展开依赖项、右键单击 Google.Apis.Auth 依赖项并选择属性来找到它。我的猜测是它指向了您机器上其他地方没有发布的另一个 DLL 副本。如果是这种情况,通过 NuGet 重新安装应该可以解决问题(它将更新项目文件中的依赖项引用)。我会通过 NuGet 卸载包,确保它已从依赖项列表中删除,然后重新安装。

其次,尝试在 Google.Apis.Auth 依赖项的 &lt;dependentAssembly&gt; 标记内的 web.config 文件中添加 &lt;bindingRedirect oldVersion="0.0.0.0-1.30.0" newVersion="1.30.0" /&gt;。这告诉应用程序您正在使用比项目实际寻找的新版本来满足依赖关系。该部分应如下所示。请参阅herebindingRedirect 的文档。

<dependentAssembly>
    <assemblyIdentity name="Google.Apis.Auth" publicKeyToken="xxx" cultuer="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.30.0" newVersion="1.30.0" />
</dependentAssembly>

【讨论】:

最近更新 更多