QueueTrigger 未选择消息 - Azure WebJobs SDK 3.0

QueueTrigger is not picking messages- Azure WebJobs SDK 3.0

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

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

Program.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();
        }
    }

Functions.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");
    }
}

这是输出的屏幕截图

感谢您的帮助

队列消息的屏幕截图'newrequests'

enter image description here

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

该功能仅在您添加消息后触发。或者我会得到和你一样的结果。

教程可以参考官方文档:Get started with the Azure WebJobs SDK。并确保设置正确的存储帐户。下面是我的appsettings.json。确保 appSettings.json 文件的 "Copy to output directory" 属性 设置为 Copy if newerCopy always.或者它会运行进入exception:Storage帐户'Storage'未配置。

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

希望对您有所帮助,如果您还有其他问题,请告诉我。