visual-studio - Azure 函数在 function.json 的本地中缺少绑定(bind)

标签 visual-studio azure azure-functions azure-function-app

Visual Studio 2017 v15.9.25。

Azure 函数 v2。

这是我的运行函数:

public static async Task Run(
        [TimerTrigger("0 */2 * * *")]TimerInfo myTimer,
        ILogger log,
        [Blob("BlobContainer/blob.json", FileAccess.ReadWrite, Connection = "AzureWebJobsStorage")] CloudBlockBlob stateStore,
        ExecutionContext context)
    {

当我在 VS 中本地构建函数时,function.json 没有与 Blob 存储的绑定(bind)。

因此,当我尝试在本地运行它时,出现以下错误:

 The 'AzureFunctionApp' function is in error: 
Microsoft.Azure.WebJobs.Host: Error indexing method 'AzureFunctionApp'. 
Microsoft.Azure.WebJobs.Host: Can't bind Blob to type 'Microsoft.Azure.Storage.Blob.CloudBlockBlob'.

这是host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      }
    }
  }
}

以下是依赖项:

Dependencies

我尝试过以下方法:

  1. 修复 Visual Studio。
  2. 在发布设置中添加远程 Azure 功能。
  3. 通过 func host json 运行以及在 VS17 中调试。

我阅读了其他 6 个类似问题,但发现该解决方案没有帮助。

请帮我解决这个问题。

最佳答案

对于这个问题,我经历了一些类似的问题。其中大多数是在 VS2017 中使用 WindowsAzure.Storage 包创建的。根据你提供的截图,我知道你没有安装这个包,但是我建议你将你的Visual Studio从2017年更新到2019年,因为VS2019更适合我们开发azure功能(也许在“VS2017 和 VS2019 之间的存储模拟器”)。

将 Visual Studio 更新到 2019 后,请创建一个新的函数应用,其中不包含任何其他包。我注意到您的屏幕截图中还有一些其他软件包,例如“azure keyvault”。然后将上面的代码复制到您的新函数中并一一安装软件包。

下面提供我的功能详细信息供您引用。我在VS2019中创建了它并且运行良好。

函数代码和安装的包:

enter image description here

本地.settings.json

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.2" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.31" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

顺便说一句,我的 function.json 也不包含有关 blob 存储的绑定(bind)。所以我认为这与你的问题无关。

函数.json

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.31",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "timerTrigger",
      "schedule": "*/30 * * * * *",
      "useMonitor": true,
      "runOnStartup": false,
      "name": "myTimer"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/FunctionAppV2.dll",
  "entryPoint": "FunctionAppV2.Function1.Run"
}

================================更新========= ===============

我重现了你的问题,可能是使用了错误的包造成的。

虽然您安装的软件包的屏幕截图中没有软件包WindowsAzure.Storage。但这个包不需要安装,直接在函数中使用即可。

在我的函数代码中,如果我使用 Microsoft.WindowsAzure.Storage.Blob 如下所示的屏幕截图,它将显示与您相同的错误消息。因此,请将 Microsoft.Azure.Storage.Blob 用于 CloudBlockBlob

enter image description here

关于visual-studio - Azure 函数在 function.json 的本地中缺少绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63284448/

相关文章:

c++ - 使用Visual Studios C++ list 文件

c# - Visual Studio : How to easily convert a method and its callers to async

python - Azure Functions Python Blob 图像

c++ - 5_4-2016q3\bin\arm-none-eabi-g++ 未找到 - Windows 10

azure - Azure 应用服务计划内的 Web 应用是否作为 IIS 中的虚拟 Web 服务器实现?是否使用网络花园?

azure-functions - Azure 函数 : Publish fails with message "Web Deploy cannot modify the file on the Destination because it is locked by an external process."

c# - 如何使用 Azure Function HTTP 绑定(bind)执行 REST API 可选字符串 URI 参数?

.net - 升级 VS.NET 解决方案和目标 .NET 框架的手动步骤?

visual-studio - Visual Studio 本地工作区 + 启用 Nuget 包还原 (disableSourceControlIntegration)

azure - 如何使用 powershell 为具有共享 key 的 Azure 文件共享构建授权 header