gpt4 book ai didi

c# - 在 VS Code 中使用 DAPR 运行带有服务总线触发器的 Azure Function 时出错

转载 作者:行者123 更新时间:2023-12-03 06:07:27 27 4
gpt4 key购买 nike

我正在尝试使用服务总线触发器运行 Azure Function(C#),并使用 DAPR 将消息发布到 VS Code 中的另一个队列。但当我尝试运行代码时,出现以下错误:“未找到作业功能。尝试将您的作业类和方法公开。如果您使用绑定(bind)扩展(例如 Azure 存储、ServiceBus、计时器等),请确保您已在启动代码中调用了扩展的注册方法(例如 builder.AddAzureStorage()、builder.AddServiceBus()、builder.AddTimers() 等)。”

下面是我的 Azure 函数代码

using System;
using System.Collections.Generic;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.WindowsAzure.Storage.Blob;
using Dapr.Client;
namespace Company.ERM
{

public class erman
{
[FunctionName("erman")]
public void Run([ServiceBusTrigger("erman", Connection = "CON_SERVICEBUS")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
using var client = new DaprClientBuilder().Build();

client.PublishEventAsync("orderpubsub", "orders", "test");
}
}
}

我正在使用的 Component 文件夹中的 yaml 文件如下所示

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: orderpubsub
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""

下面是我在 VS 代码中使用 DAPR 运行该函数时运行的命令:

dapr run --app-id checkout --resources-path ./component/  -- func start --no-build --verbose

如果我遗漏了什么,请提出建议。

最佳答案

@fildor,感谢您的见解。我在 Azure 门户中创建了一个服务总线命名空间,并通过导航 Entities>Queues>+Add 添加了队列。

enter image description here

我在 VS Code 中使用服务总线队列触发器创建了一个 Azure Function 应用。dotnet add package Dapr.AspNetCore 软件包已安装。

函数代码:

using System;
using Microsoft.Azure.WebJobs;
using Dapr.Client;
using Microsoft.Azure.WebJobs.Host;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace MyFunctionApp
{
public class daprfunc
{
[FunctionName("daprfunc")]
public async Task RunAsync([ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
using var client = new DaprClientBuilder().Build();
await client.PublishEventAsync("pubsub", "topic", "Hello, Dapr!");
}
}
}

local.settings.json:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"ServiceBusConnection":"Your-servicebus-connectionstring"
}
}

function.json:

{
"generatedBy": "Microsoft.NET.Sdk.Functions.Generator-4.1.1",
"configurationSource": "attributes",
"bindings": [
{
"type": "serviceBusTrigger",
"connection": "ServiceBusConnection",
"queueName": "myqueue",
"direction": "in",
"isSessionsEnabled": false,
"autoComplete": true,
"name": "myQueueItem"
}
],
"disabled": false,
"scriptFile": "../bin/MyFunctionApp.dll",
"entryPoint": "MyFunctionApp.daprfunc.RunAsync"
}

pubsub.yaml:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""

使用上面的代码成功触发了我的函数。检查以下内容:

enter image description here

输出:

enter image description here

关于c# - 在 VS Code 中使用 DAPR 运行带有服务总线触发器的 Azure Function 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77176912/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com