0

I am looking for an example for a simple webjob:

the task would be to process the response from a web link and save it to blob on a regular time interval.

first of all the ms documentation is confusing me as far as time triggers are concerned:

https://learn.microsoft.com/en-us/azure/app-service/webjobs-create#ncrontab-expressions

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=csharp#example

and also how exactly should I proceed on building the WebJob, should I use an azure webjob template (.net 4.x.x), or .net core console app ??

https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to

https://github.com/Azure/azure-webjobs-sdk-samples/tree/master/BasicSamples

https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started

https://learn.microsoft.com/en-us/azure/app-service/webjobs-create

all this resource and no simple example for a time scheduled task that would get a web response, also the confusion on building the webjob VS, wth?? I want to build a c# app in VS and deploy to azure as webjob via azure devops.

wasted 3 days on this since im not a .net developer...

1 Answers1

1

Webjobs have changed and grown over the years including contributions from Azure Functions, which is also built on top of the Webjobs SDK. I can see how this can get confusing, but the short answer is that all of the different methods are still valid, but some are newer than others. Of the two timer trigger styles, the second is more current.

I generally recommend Functions instead of Webjobs for something like this since at this point as it will save you some boiler-plate code, but it is entirely up to you. As I mentioned, the foundations are very similar. You can deploy Functions apps to any App Service plan, including the Consumption plan- this is specific to Functions that is pay-by-usage instead of a monthly fee like you would need for WebJobs.

As far as .NET Framework vs. .NET Core, you can use it will depend on what runtime you used to set up your App Service. If you have a choice, I would recommend using Core since that will be the only version moving forward. If you elect to use Functions, you will definitely want to use Core.

As far as the Console App question, all WebJobs are essentially console apps. From a code perspective, they are a console app that implements the Webjobs SDK. You could run them outside of Azure if you wanted to. Functions apps are different. The Function's host is what actually runs behind the scenes and you are creating a class library that the host consumes.

Visual Studio vs. Visual Studio Code is very much a personal preference. I prefer VS for Webjobs and work with both VS and VS Code for Functions apps depending on which language I am working in.

The most basic version of a Webjob in .NET Core that pulls data from a webpage on a schedule and outputs it to blob storage would look something like this. A Function app would use exactly the same GetWebsiteData() method plus a [FunctionName("GetWebsiteData")] at the beginning, but you wouldn't need the Main method as that part is handled by the host process.

    public class Program
    {
        static async Task Main(string[] args)
        {
            var builder = new HostBuilder();
            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.AddAzureStorage();
                b.AddTimers();
            });

            builder.ConfigureAppConfiguration((context, configurationBuilder) =>
            {
                configurationBuilder
                    .AddJsonFile($"appsettings.json", optional: true);
            });

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

        public async static void GetWebsiteData(
            [TimerTrigger("0 */1 * * * *")] TimerInfo timerInfo,
            [Blob("data/websiteData", FileAccess.Write)] Stream outputBlob, 
            ILogger logger)
        {
            using(var client = new HttpClient())
            {
                var url = "https://microsoft.com";
                var result = await client.GetAsync(url);
                //you may need to do some additional work here to get the output format you want
                outputBlob = await result.Content.ReadAsStreamAsync();
            }
        }
    }
PerfectlyPanda
  • 3,271
  • 1
  • 6
  • 17
  • 1
    thanks for this clarification, and for the best practices advices, also the code example you posted does exactly what I need. You helped me a lot, not only with code but with thorough explanation. Thanks again, and have a nice day. – z.milosev Nov 17 '20 at 08:11