【问题标题】:Azure Functions - The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)Azure Functions - 给定的程序集名称或代码库无效。 (来自 HRESULT 的异常:0x80131047)
【发布时间】:2020-01-05 19:16:36
【问题描述】:

问题

我正在尝试创建一个利用 .NET Core 2.2 的 Azure 函数,该函数通过 Google Sheets API 访问 Google Sheet,调用数据,并将其插入同样托管在 Azure 中的 SQL DB。

这是我的错误:

这是在遵循this 指南之后。

请注意,所有包都已恢复。

代码大致如下:

#r "D:\home\site\wwwroot\bin\Google.Apis.Sheets.v4"
#r "D:\home\site\wwwroot\bin\Google.Apis.Auth.OAuth2"
#r "D:\home\site\wwwroot\bin\Google.Apis.Sheets.v4"
#r "D:\home\site\wwwroot\bin\Google.Apis.Sheets.v4.Data"
#r "D:\home\site\wwwroot\bin\Google.Apis.Services"
#r "D:\home\site\wwwroot\bin\System.Data.SqlClient"

using System;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Data.SqlClient;


public static string DBConn { get; set; }
public static ILogger Log {get; set;}
public static void Run(TimerInfo myTimer, ILogger log)
{   
    Log=log;  
    DBConn="MySQLSrvrConnectionString";
    try
    {
        string spreadsheetId = "MyGoogleSheetId";
        SheetsService service = GetSheetService();
        if(service!=null)
        {
            DoSomethingFunc(GetInsertCommand(GetSheetVals(service,spreadsheetId)));
        }
    }
    catch (Exception ex)
    {
        Log.LogInformation($"Error ({DateTime.Now.ToLongDateString()}): {ex.Message}");
    }
    finally
    {
        Log.LogInformation($"Function Completed at: {DateTime.Now.ToLongDateString()}");
    }
}

如果需要,我可以提供更多。

我还添加了一个 function.proj 文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Google.Apis.Sheets.v4" Version="1.40.3.1679" />
    <PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
  </ItemGroup>

</Project>

研究 老实说,没有找到太多。

  • This 是一个类似的错误代码,但它似乎是一个命名问题 - 有一个撇号。我的应用名为“TopicsProvider”
  • This 谈论 Webparts XML 文件。这根本不是我项目的一部分。

更新 1

将代码还原为最简单的形式,我可以让它与 function.proj 中引用的 Google API 一起运行。此代码有效:

using System;
using System.Collections.Generic;
using System.IO;
using System.Data.SqlClient;
public static void Run(TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"HELLO");
}

使用这样的function.proj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Google.Apis.Sheets.v4" Version="1.40.3.1679" />
  </ItemGroup>

</Project>

失败的是在顶部添加using Google.Apis.Sheets.v4;。这给出了:

error CS0246: The type or namespace name 'Google' could not be found (are you missing a using directive or an assembly reference?)

在顶部添加#r "Google.Apis.Sheets.v4"D:\home\site\wwwroot\bin\Google.Apis.Sheets.v4"(请注意,在控制台中,没有以bin 文件夹开头...不确定原因),并不能解决问题,并更改错误:

2019-09-03T17:48:31.700 [Information] Script for function 'TimerTrigger1' changed. Reloading.
2019-09-03T17:48:31.976 [Error] run.csx(1,1): error CS0006: Metadata file 'Google.Apis.Sheets.v4' could not be found
2019-09-03T17:48:32.051 [Error] run.csx(7,7): error CS0246: The type or namespace name 'Google' could not be found (are you missing a using directive or an assembly reference?)
2019-09-03T17:48:32.075 [Information] Compilation failed.
2019-09-03T17:48:32.549 [Information] Executing 'Functions.TimerTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=bce27519-0236-4754-ac6c-66ae83808801)
2019-09-03T17:48:32.615 [Information] Package references have been updated.
2019-09-03T17:48:32.615 [Information] Restoring packages.
2019-09-03T17:48:32.647 [Information] Starting packages restore
2019-09-03T17:48:36.701 [Information] Restoring packages for D:\local\Temp\32a465b3-8a19-46c8-a533-799a91e1ec09\function.proj...
2019-09-03T17:48:38.434 [Information] Generating MSBuild file D:\local\Temp\32a465b3-8a19-46c8-a533-799a91e1ec09\obj\function.proj.nuget.g.props.
2019-09-03T17:48:38.435 [Information] Generating MSBuild file D:\local\Temp\32a465b3-8a19-46c8-a533-799a91e1ec09\obj\function.proj.nuget.g.targets.
2019-09-03T17:48:38.461 [Information] Restore completed in 2.56 sec for D:\local\Temp\32a465b3-8a19-46c8-a533-799a91e1ec09\function.proj.
2019-09-03T17:48:38.772 [Information] Packages restored.
2019-09-03T17:48:38.996 [Warning] You may be referencing NuGet packages incorrectly. Learn more: https://go.microsoft.com/fwlink/?linkid=2091419
2019-09-03T17:48:39.053 [Error] Function compilation error

Microsoft.CodeAnalysis.Scripting.CompilationErrorException : Script compilation failed.

   at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.CreateFunctionTarget(CancellationToken cancellationToken) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 314

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at async Microsoft.Azure.WebJobs.Script.Description.FunctionLoader`1.GetFunctionTargetAsync[T](Int32 attemptCount) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\FunctionLoader.cs : 55

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.GetFunctionTargetAsync(Boolean isInvocation) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 183
2019-09-03T17:48:39.122 [Error] run.csx(1,1): error CS0006: Metadata file 'Google.Apis.Sheets.v4' could not be found
2019-09-03T17:48:39.203 [Error] run.csx(7,7): error CS0246: The type or namespace name 'Google' could not be found (are you missing a using directive or an assembly reference?)
2019-09-03T17:48:39.256 [Error] Executed 'Functions.TimerTrigger1' (Failed, Id=bce27519-0236-4754-ac6c-66ae83808801)

脚本编译失败。

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

据我了解,#r 不是必需的,不是根据this 的答案。包应自行加载。我也不应该上传这些文件 - 它们是应该自动安装的 nuget 包。

更新 2

手动上传 DLL 不起作用。从“上传”中选择文件,有一段时间界面就像正在加载一样,但是什么也没发生:

我错过了什么?是否不允许使用其中一个包?是配置问题吗?

【问题讨论】:

    标签: c# azure .net-core google-api azure-functions


    【解决方案1】:

    更新:

    我重试你所做的,然后它似乎成功了,

    1.创建function.proj文件

    2.保存文件并运行:

     <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
        <AzureFunctionsVersion>v2</AzureFunctionsVersion>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Google.Apis.Sheets.v4" Version="1.40.3.1679" />
        <PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
      </ItemGroup>
    </Project>
    

    3.在.crx文件中添加代码:

    #r "Newtonsoft.Json"
    
    using System.Net;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Primitives;
    using Microsoft.Extensions.Logging;
    using Newtonsoft.Json;
    
    public static string DBConn = "123456";
    public static ILogger Log = null;
    
    public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
    {
        Log=log;  
        DBConn="MySQLSrvrConnectionString";
        try
        {
            string spreadsheetId = "MyGoogleSheetId";
            //SheetsService service = GetSheetService();
            //if(service!=null)
            //{
            //    DoSomethingFunc(GetInsertCommand(GetSheetVals(service,spreadsheetId)));
            //}
        }
        catch (Exception ex)
        {
            Log.LogInformation($"Error ({DateTime.Now.ToLongDateString()}): {ex.Message}");
        }
        finally
        {
            Log.LogInformation($"Function Completed at: {DateTime.Now.ToLongDateString()}");
        }
        string name = req.Query["name"];
    
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;
    
        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
    

    4.然后编译成功:

    PS:google .net 客户端库没有名为 get sheet service 的方法。

    原答案:

    这是我的代码:

    public static class Function1
    {
        public static string DBConn { get; set; }
        public static ILogger Log { get; set; }
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            Log = log;
            DBConn = "MySQLSrvrConnectionString";
            try
            {
                string spreadsheetId = "MyGoogleSheetId";
    
            }
            catch (Exception ex)
            {
                Log.LogInformation($"Error ({DateTime.Now.ToLongDateString()}): {ex.Message}");
            }
            finally
            {
                Log.LogInformation($"Function Completed at: {DateTime.Now.ToLongDateString()}");
            }
            return (ActionResult)new OkObjectResult($"");
        }
    }
    

    我删除了

    SheetsService service = GetSheetService();
    if(service!=null)
    {
         DoSomethingFunc(GetInsertCommand(GetSheetVals(service,spreadsheetId)));
    }
    

    因为我没有找到哪个 nuget 包有这些方法。

    这是我的 function.proj 文件:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
        <AzureFunctionsVersion>v2</AzureFunctionsVersion>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Google.Apis" Version="1.40.3" />
        <PackageReference Include="Google.Apis.Auth" Version="1.40.3" />
        <PackageReference Include="Google.Apis.Auth.Mvc" Version="1.40.3" />
        <PackageReference Include="Google.Apis.Sheets.v4" Version="1.40.3.1694" />
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.28" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>
    

    然后它工作正常。我不知道哪些包包含这些方法,因为我对 Google API 了解不多。希望我的回答能给你一些帮助。

    【讨论】:

    • 我缩减了代码进行测试。未安装的软件包出现问题 - 除了使用 System 以外的所有内容,使用 Google.Apis.Sheets.v4 会导致错误。
    • @user 你可以再试一次。在我这边,似乎没问题。
    猜你喜欢
    • 2017-07-23
    • 2013-07-21
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多