Can I create an Azure Webjob that exposes function

2019-04-10 03:20发布

问题:

I want to create an Azure Webjob to serve batch processing needs (specifically it will continuously iterate over a SQL Azure database table picking up certain records, performing some operations and then updating the table). I have no need for Azure storage.

In this scenario can I still expose my methods to the Azure function invocation dashboard? Or are methods that have the Azure storage attributes the only ones that get exposed?

As an example I might have a function:

ShowTotalNumRecordsProcessedToday()

that I would like to expose and be able to invoke from the dashboard. I've created some public test functions but they don't show in the dashboard.

Can I do this in my scenario?

回答1:

You can take advantage of the WebJobs SDK regardless of whether you use Azure Storage for data or not.

Here is an example of job that uses the SDK for logging but nothing else:

public static void Main
{
     using(JobHost host = new JobHost())
     {
         // Invoke the function from code
         host.Call(typeof(Program).GetMethod("DoSomething"));

         // The RunAndBlock here is optional. However,
         // if you want to be able to invoke the function below
         // from the dashboard, you need the host to be running
         host.RunAndBlock();
         // Alternative to RunAndBlock is Host.Start and you
         // have to create your own infinite loop that keeps the
         // process alive
    }
}

// In order for a function to be indexed and visible in the dashboard it has to 
// - be in a public class
// - be public and static
// - have at least one WebJobs SDK attribute
[NoAutomaticTrigger]
public static void DoSomething(TextWriter log)
{
    log.WriteLine("Doing something. Maybe some SQL stuff?");
}

However, you will need a storage account to connect the host and the dashboard.

You can also create your own "custom trigger" for SQL or anything else like this:

public static void Main
{
     using (JobHost host = new JobHost())
     {
         host.Start();

         while (!TerminationCondition)
         {
             if (SomeConditionRequiredForTheTrigger)
             {
                 host.Call(typeof(Program).GetMethod("DoSomething"));
             }
             Thread.Sleep(500);
         }

         host.Stop();
    }
}

// In order for a function to be indexed and visible in the dashboard it has to 
// - be in a public class
// - be public and static
// - have at least one WebJobs SDK attribute
[NoAutomaticTrigger]
public static void DoSomething(TextWriter log)
{
    log.WriteLine("Doing something. Maybe some SQL stuff?");
}

PS: Wrote the code directly in the browser so there might some errors.



回答2:

Quick answer is:

Yes you can. You don't need to interact with Azure storage in Azure webjobs. I have implemented a processor that interacts with a SQL Azure database. You can deploy and run it from the dashboard like any other webjob.

Hope this helps

Update: Make sure you configure your connectionstring properly. Maybe that's the issue. As a side note, if you're deploying with a website you need to add the connectionstrings to the website configuration in Azure.