Azure 和 VSTO 插件 - 无法加载文件或程序集“System.Diagnostics.DiagnosticSource,版本 = 6.0.0.0,

问题描述 投票:0回答:1

我真的被这个问题难住了。我试图从 VSTO Excel 加载项中调用 Azure 表单识别器,上传要分析的本地文档,但遇到了各种错误。

这是当前的错误输出,比我在标题中输入的内容要多一些:https://pastebin.com/niS9n0V3

我尝试在常规的旧控制台应用程序中使用相同的 Azure.AI.FormRecognizer 库,并且工作正常。我已经以管理员身份运行 Excel 和 VS。我还能够对 REST API 进行简单调用,只是为了查看 Excel 是否阻止了互联网连接或其他东西,这没有问题。

这是当前代码:

using System;
using System.IO;


using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;
using System.Threading.Tasks;

namespace ExcelAddIn6
{
    public partial class ThisAddIn
    {

        static string key2 = "key";
        static string endpoint2 = "endpoint";

        static string imagepath = "path to file";

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //Authenticate
            AzureKeyCredential credential = new AzureKeyCredential(key2);
            DocumentAnalysisClient client = new DocumentAnalysisClient(new Uri(endpoint2), credential);

            //Send file on local disk to OCR
            LaunchOCR(client);

        }


        public void LaunchOCR(DocumentAnalysisClient c)
        {
            try
            {
                Task.Run(() =>
                {
                    AnalyzeDocumentOperation operation = c.AnalyzeDocument(WaitUntil.Completed, "prebuilt-invoice", (Stream)File.Open(imagepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                    AnalyzeResult result = operation.Value;
                }).Wait();
            }
            catch (AggregateException ae)
            {
                // Handle the AggregateException
                ae.Handle((x) =>
                {
                    if (x is RequestFailedException rfe)
                    {
                        System.Windows.Forms.MessageBox.Show($"Error analyzing document: {rfe.Message}");
                        return true;
                    }
                    // Handle other exceptions
                    System.Windows.Forms.MessageBox.Show($"An error occurred: {x.Message}");
                    return true;
                });
            }
        }


        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}

我对所有这些支持的 .config 文件不是很了解,所以这是我的 packages.config,以防万一产生任何影响:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Azure.AI.FormRecognizer" version="4.1.0" targetFramework="net48" />
  <package id="Azure.Core" version="1.34.0" targetFramework="net48" />
  <package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.1" targetFramework="net48" />
  <package id="System.Buffers" version="4.5.1" targetFramework="net48" />
  <package id="System.Diagnostics.DiagnosticSource" version="6.0.1" targetFramework="net48" />
  <package id="System.IO" version="4.3.0" targetFramework="net48" />
  <package id="System.Memory" version="4.5.4" targetFramework="net48" />
  <package id="System.Memory.Data" version="1.0.2" targetFramework="net48" />
  <package id="System.Net.Http" version="4.3.4" targetFramework="net48" />
  <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
  <package id="System.Runtime" version="4.3.0" targetFramework="net48" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net48" />
  <package id="System.Text.Encodings.Web" version="4.7.2" targetFramework="net48" />
  <package id="System.Text.Json" version="4.7.2" targetFramework="net48" />
  <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
  <package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>
c# .net excel azure vsto
1个回答
0
投票

就像

Azure.AI.FormRecognizer
包依赖项需要特定版本的
System.Diagnostics.DiagnosticSource
(版本 6.0.0.0),但在运行时正在加载不同的版本。

  • 错误消息表明正在加载的程序集是版本
    6.0.0.0
    ,但
    packages.config
    中引用的版本是
    4.5.1
    。这种不匹配导致了
    FileLoadException

这里您需要向

app.config
文件添加程序集绑定重定向,以重定向运行时以使用正确的版本。

<dependentAssembly>
  <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

enter image description here

上面的代码没有任何改变,它工作得很好。这样,它就可以从本地选择文件/doc/png并通过Azure Form Recognizer进行分析。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.