1

我正在尝试使用 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");
    }
}

这是输出的屏幕截图

在此处输入图像描述

感谢你的帮助

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

在此处输入图像描述

4

1 回答 1

0

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

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

在此处输入图像描述

关于教程,可以参考官方文档:Azure WebJobs SDK 入门。并确保您设置了正确的存储帐户。下面是我的appsettings.json。确保文件的“复制到输出目录”属性appSettings.json设置为Copy if newerCopy always。否则会遇到异常:未配置存储帐户“存储”。

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

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

于 2019-03-04T05:37:47.167 回答