azure - QueueTrigger 未选取消息 - Azure WebJobs SDK 3.0

标签 azure azure-webjobssdk webjob azure-webjobs-triggered queuetrigger

我正在尝试使用 SDK 3.0.x 开发 WebJob,并在本地进行测试。我按照 github 中的示例进行操作,但没有成功。

在本地运行时,一切正常,它还会看到 ProcessQueueMessage 函数,但不会从队列中选取消息。

程序.cs

static void Main(string[] args)
    {
        var builder = new HostBuilder();
        //builder.UseEnvironment(EnvironmentName.Development);
        builder.ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddAzureStorage();

        });
        builder.ConfigureAppConfiguration((context, config) =>
        {
            config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
        });

        builder.ConfigureLogging((context, b) =>
        {
            b.AddConsole();

            // If the key exists in settings, use it to enable Application Insights.
            string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
            if (!string.IsNullOrEmpty(instrumentationKey))
            {
                b.AddApplicationInsights(o => o.InstrumentationKey = instrumentationKey);
            }
        });

        builder.ConfigureServices((context, services) =>
        {
            //services.AddSingleton<IJobActivator, MyJobActivator>();
            services.AddScoped<Functions, Functions>();
            services.AddSingleton<IHostService, HostService>();
        })
        .UseConsoleLifetime();

        var host = builder.Build();
        using (host)
        {
            host.Run();
        }
    }

函数.cs

public class Functions
{
    private readonly IHostService _hostService;

    public Functions(IHostService hostService)
    {
        _hostService = hostService;
    }
    // This function will get triggered/executed when a new message is written 
    // on an Azure Queue called queue.
    public void ProcessQueueMessage([QueueTrigger("newrequests")] string dd,
        //DateTimeOffset expirationTime,
        //DateTimeOffset insertionTime,
        //DateTimeOffset nextVisibleTime,
        //string queueTrigger,
        //string id,
        //string popReceipt,
        //int dequeueCount,
        ILogger logger)
    {
        var newRequestItem = new RequestQueueItem();
        logger.LogTrace($"New queue item received...");
        //logger.LogInformation($"    QueueRef = {id} - DequeueCount = {dequeueCount} - Message Content [Id = {newRequestItem.Id}, RequestDate = {newRequestItem.RequestDate}, Mobile = {newRequestItem.Mobile}, ProviderCode = {newRequestItem.ProviderCode}, ItemIDClass = {newRequestItem.MappingIDClass}]");
        // TODO: Read the DatabaseConnectionString from App.config
        logger.LogTrace($"    Getting DB ConnectionString...");
        var connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;
        // TODO: Initiation of provider service instance
        logger.LogTrace($"    Init IalbayanmtnclientserviceClient service instance...");
        var bayanService = new AlbayanMtnWCFService.IalbayanmtnclientserviceClient();
        // TODO: sending request to provider service endpoint and wait for response
        logger.LogTrace($"    Sending request to Service Endpoint...");
        var response= bayanService.requestpaymenttransactionAsync("agentcode", "agentpassword", "accountno", int.Parse(newRequestItem.TransactionType), newRequestItem.MappingIDClass, newRequestItem.Mobile, (int)newRequestItem.Id).Result;

        logger.LogTrace($"Done processing queue item");
    }
}

这是输出的屏幕截图

enter image description here

感谢您的帮助

队列消息“newrequests”的屏幕截图

enter image description here

最佳答案

从您的快照来看,您的网络作业在本地运行良好。它没有选择消息,因为您没有在 newrequests 队列中添加消息。

该功能只有在添加消息后才会触发。否则我会得到和你一样的结果。

enter image description here

关于教程,可以引用官方文档:Get started with the Azure WebJobs SDK 。并确保设置正确的存储帐户。下面是我的 appsettings.json。确保 appSettings.json 文件的“复制到输出目录”属性设置为如果较新则复制始终复制。否则会遇到异常:存储帐户“存储”未配置。

{
  "ConnectionStrings": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=key;..."
  }
}

希望这可以帮助您,如果您还有其他问题,请告诉我。

关于azure - QueueTrigger 未选取消息 - Azure WebJobs SDK 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54950494/

相关文章:

azure - 预定的网络作业

c# - Azure 服务总线每隔一段时间发送消息

Azure documentdb 防火墙阻止了来自 Web 作业的访问

azure - 没有 www 的域无法工作,但有 www 的域可以在 Azure 上工作

azure - Azure Databricks 是否在 Azure Data Lake Storage 中使用查询加速?

python - Azure网站使用python3和django

azure - 当您重新生成服务总线根管理器 sas key 时,连续 Web 作业也不异常(exception)

node.js - Azure 诊断日志大小限制

c# - Azure Webjobs ServiceBusTrigger - 达到 MaxDeliveryCount 时执行不同的功能

c# - 更新后 ServiceBusTrigger 不工作