【问题标题】:Azure Functions - can't be invoked from Azure WebJobs SDKAzure Functions - 无法从 Azure WebJobs SDK 调用
【发布时间】:2018-09-21 00:54:30
【问题描述】:

所以我一直在尝试创建一个简单的天蓝色函数,这将是一个 http 触发器“CreateUser”。
我做了另一个 http 触发器来简化问题,它看起来相当简单:

using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace TutoTableAzureTemplate
{
    public static class TestTrigger
    {
        [FunctionName("TestTrigger")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route =  null)]HttpRequestMessage req, TraceWriter log)
        {
            return req.CreateResponse(HttpStatusCode.OK, "This request arrived succcesfully");
        }
    }
}

这在模拟器上运行,给我带来以下错误:

Error indexing method 'TestTrigger.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'log' to type TraceWriter. Make sure the parameter Type is supported by the binding.

(我的模拟器版本是5.3)
我试图删除参数TraceWriter log,并且函数“运行”正常......直到我使用邮递员向它发送一个http请求,这带来了一个关于WebJobs的错误:

"System.InvalidOperationException : 'TestTrigger' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes? ... "

我想知道该属性是否是导致上一个问题的TraceWriter log,是否有办法将其带回此处...

哦,顺便说一句,我进入了某种地狱般的版本冲突,出于某种原因,我不得不使用 .NET Standard 2.0 而不是我之前使用的 .NET 461,按照教程的建议。
这是我的 .csproj :

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>    
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.13" />
    <PackageReference Include="Microsoft.Azure.Storage.Common" Version="9.0.0.1-preview" />
    <PackageReference Include="Microsoft.Azure.CosmosDB.Table" Version="1.1.1" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

"Microsoft.Azure.CosmosDB.Table" 显然在 .NET Standard 2.0 中不可用,并且 .NET 461 版本在此处恢复,但“这只是一个警告”......而"Microsoft.Azure.Storage.Common" 仅在预览中。

这可能与某个地方的某个版本有关,但我在所有使用不同东西的教程中迷失了自己,而且由于我对 Azure 还很陌生,我不知道发生了什么......

【问题讨论】:

  • 能否添加运行时版本:运行func 命令并复制Azure Functions Core Tools (xyz)Function Runtime Version: xyz 等行。
  • 这有帮助吗?我在运行 .netstandard 2.0 时遇到了完全相同的问题:stackoverflow.com/questions/49672011/…
  • 这就是我得到的:Azure Functions Core Tools (1.0.10)Function Runtime Version: 1.0.11612.0... 这里我只在模拟器上运行,但是当我将它发布到Azure 的云

标签: c# azure azure-functions azure-compute-emulator


【解决方案1】:

出于某种原因,我不得不按照教程建议使用 .NET Standard 2.0 而不是我之前使用的 .NET 461。

似乎当您创建 azure function initial 时,您的函数是 .NET 461,出于某种原因,您将其更改为 .NET Standard 2.0。

但是,当您的函数是 .NET Standard 2.0 时,您的运行时版本应设置为 beta

所以在你的.csproj中添加AzureFunctionsVersion,因为默认的.NET 461 runtime是1,当你改成.NET core时,需要把runtime改成“beta ”手动。

您可以参考以下代码:

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>

【讨论】:

  • 是的,它实际上是有道理的,并且它修复了这个绑定错误......我也停止使用 CosmosDB 包,使用已经是 Sdk 函数一部分的那个,WindowsStorage.Table,这是受支持的在标准中。
  • 感谢您的回答,请问如何将运行时版本更改为测试版?
  • 默认情况下,在 Azure 门户中创建的函数应用设置为版本 2.x。您只能在创建函数应用之后、添加任何函数之前更改运行时版本。这是article。并转到您的函数应用>函数应用设置>运行时版本,您将找到它。
【解决方案2】:

我在 VS 2019 上创建新的 Azure Function V2 时遇到了这个问题,并且我包含了函数应用程序引用的另一个项目。删除另一个项目为我修复了它。

【讨论】:

  • 有什么解决办法吗?我需要引用多个项目。谢谢
  • 尝试将您的函数升级到 v3 或将您依赖的其他项目打包到 nuget。
  • @jay 我解决这个问题的另一种方法是通过依赖注入。我能够使用它引用另一个项目。
【解决方案3】:

现在是 2020 年,我在 vs 2019 中创建了我的 Azure Functions v1 并得到了同样的错误。但是,我通过如下编辑 .csproj 文件找到了解决方法:

    <AzureFunctionsVersion>v2</AzureFunctionsVersion>

【讨论】:

    【解决方案4】:

    我刚刚在运行.Net 6.0v4 函数中遇到了相同的'TestTrigger' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?"-error。

    就我而言,这就像一条糟糕的路线一样简单,如下所示:

    public async Task<Models.BinaryFile> GetAsync([HttpTrigger(AuthorizationLevel.Function, "get",
     Route = BaseUrl")] HttpRequest req, Guid fileId, ILogger log)
        {
            // [logic to get file here...]
            return file;
        }
    

    应该是这样的:

    public async Task<Models.BinaryFile> GetAsync([HttpTrigger(AuthorizationLevel.Function, "get",
     Route = BaseUrl + "/{fileId}")] HttpRequest req, Guid fileId, ILogger log)
        {
            // [logic to get file here...]
            return file;
        }
    

    注意这部分:Route = BaseUrl + "/{fileId}"

    超级容易修复,但令人讨厌的部分是它没有遇到任何断点,因此很难精确定位。 这就是我发布此内容的原因,以帮助人们在故障排除期间检查此类基本内容:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多