1

I'm trying to develop WebJob using SDK 3.0.x, and testing it locally. I've followed the sample in github without any success.

When running it locally everything is going ok, it also see the ProcessQueueMessage function but it doesn't pick the messages from the queue.

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

Here is the screen shot for the output

enter image description here

Appreciate your help

Screen shot for queue messages 'newrequests'

enter image description here

Fady Huwaidy
  • 41
  • 1
  • 7

1 Answers1

0

From your snapshot, your webjob runs well on local. It didn't pick message because you don't add message in the newrequests queue.

The function only be triggered after you add the message. Or I will get the same result just like yours.

enter image description here

About the tutorial , your could refer to the official doc:Get started with the Azure WebJobs SDK. And make sure you set the right storage account. The below is my appsettings.json. Make sure the "Copy to output directory" property of the appSettings.json file is set to either Copy if newer or Copy always. Or it will run into exception:Storage account 'Storage' is not configured.

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

Hope this could help you, if you still have other questions, please let me know.

George Chen
  • 13,703
  • 2
  • 11
  • 26
  • Thank you George for your answer, actually I tried to add to queue using Server Explorer but it didn't pick the messages. – Fady Huwaidy Mar 06 '19 at 22:47
  • @Fady Huwaidy, I test queue with Server Explorer, it worked, so please make sure you configure the right storage account connection string and the queue name in the Function trigger is same as the queue name in storage. Or it will show same results just like your snapshots. – George Chen Mar 07 '19 at 01:54
  • I edited my post by embedding screen shot for queue messages. About the storage connection I tried configuring wrong connection the console crashed so the storage connection is ok. – Fady Huwaidy Mar 07 '19 at 15:22
  • The issue was strange as when creating queue in VS Server Explorer, it shows that queues was created with the entered names, but when using Microsoft Azure Storage Explorer I found that the queues was created with random names. – Fady Huwaidy Mar 11 '19 at 19:07
  • @Fady Huwaidy, I test with server explorer and storage explorer, it works fine. So did you use the newest explorer, and could you share the queue status in explorer? – George Chen Mar 12 '19 at 01:40