【问题标题】:.NET7 Azure Functions: the host has not yet started.NET7 Azure Functions:主机尚未启动
【发布时间】:2023-02-17 17:26:24
【问题描述】:

我想在我的笔记本电脑上调试一个带有 Azure Functions 的项目。语言是C#。很少有功能使用与服务总线的连接。该项目已部署在 Azure 上并且正在运行。

在我的新笔记本电脑中,我安装了 Visual Studio 2022 社区(64 位)- 预览版 17.5.0 预览版 6.0。

然后,从 Git 存储库中拉取项目,当我运行该项目时出现此错误

启动操作“02312afe-22ad-4fdf-bb10-f4852471c73e”期间发生主机错误。

Microsoft.Azure.WebJobs.Script:未找到语言为 [dotnet-isolated] 的函数。

Visual Studio 向我展示了这些行:

public Task StopAsync()
{
    ThrowIfDisposed();

    Interlocked.CompareExchange(ref _state, StateStoppingOrStopped, StateStarted);

    if (_state != StateStoppingOrStopped)
    {
        throw new InvalidOperationException("The host has not yet started.");
    }

他们来自JobHost.cs(由微软撰写)

按照这个post,我尝试安装Azure Functions Core Tools,但我得到了同样的错误。

该项目具有以下属性

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <ApplicationInsightsResourceId>/subscriptions/</ApplicationInsightsResourceId>
    <UserSecretsId>d9d1dbff-5ee9-4590-ab74-4fbd7c563096</UserSecretsId>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.10.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.8.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ApplicationInsights" Version="1.0.0-preview4" />
    <PackageReference Include="PSC.Extensions" Version="6.0.28" />
    <PackageReference Include="WB.Domain" Version="1.2.44" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\WB.Api.Client\WB.Api.Client\WB.Api.Client.csproj" />
    <ProjectReference Include="..\..\WB.Connections.Reverso\WB.Connections.Reverso\WB.Connections.Reverso.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

我尝试将 local.settings.json 中的值 FUNCTIONS_WORKER_RUNTIMEdotnet-isolated 更改为 powershell 但遇到了同样的问题。

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "powershell",
        "SBConnectionString": "Endpoint=myconection"
    }
}

因此,然后,我尝试使用带有 NET 7 的 Visual Studio 创建一个新的 Azure Functions。在向导中,我添加了服务总线连接字符串和队列。错误是相似的

蓝铜矿

如果我使用 PowerShell 运行Azurite,我会得到一个错误

当我在 Visual Studio 中打开项目时,我可以看到

【问题讨论】:

  • 您的 Azure 存储模拟器启动了吗?

标签: c# azure azure-functions


【解决方案1】:

在问题描述中,您提供了 .NET 6 Azure Functions .csproj 代码:

如果您要迁移到 .NET 6/7 独立版本,则必须在 local.settings.json 文件中将 FUNCTIONS_WORKER_RUNTIME 设置为 dotnet-isolated 并按照用户在 SO #69104798 中给出的步骤进行操作 @德温布朗.

以下是带有 Http 和服务总线触发器的 Azure Functions .NET 7 隔离版本的 .csproj 代码:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.12" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="4.2.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0-preview2" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

Microsoft.Azure.WebJobs.Script:未找到语言为 [dotnet-isolated] 的函数。

如果 Functions 项目不在 PowerShell 语言/运行时中,请勿将 powershell 值用于 FUNCTIONS_WORKER_RUNTIME


我观察到您在使用服务总线连接字符串时遇到错误,例如 'Endpoint=sb://wbreverso.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;[Hidden Credential]' is missing or empty.

当服务总线连接字符串名称从 local.settings.json 到功能代码不相同时,将出现此错误。

您已在 local.settings.json 文件的变量 SBConnectionString 中定义了服务总线连接字符串,并且服务总线触发器函数声明中应使用相同的名称。

public static void Run([ServiceBusTrigger("myqueue", Connection = "SBConnectionString")]

这是为了在本地 IDE(Visual Studio、VS Code)中运行服务总线触发器 Azure Functions。

当您在 Azure Cloud 中运行相同的 Function 项目时,您必须在应用程序设置配置部分中定义该服务总线连接字符串。

有关 Azure Functions Service Bus Trigger Connection Bindings 的更多信息,请参阅MS Doc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    相关资源
    最近更新 更多