简体   繁体   中英

is there any way to insert input to continuously running webjob?

I have a simple webjob hosted on azure as a continuously running job.

I need to pass token value as a input , how can I achieve this using continuous web job?

static void Main(string[] args)
        {
             try
            {
            Console.WriteLine("Goto Login url" + kite.GetLoginURL());
            Console.WriteLine("Enter request token: ");
            string requestToken = Console.ReadLine();

在此处输入图片说明

I don't think it's possible using webjobs (continuos job) unless you have some web server running where you can fire requests against (eg sinatra, aspnet core, etc). You need to use Azure Functions with HTTP Trigger. Then you can pass the token in the querystring or in the body of the request and do what you need to do.

Webjob is background service, so you can not take user input using Console.ReadLine() anyway. Since WebJob does not have HTTP trigger like Azure Function, I see the only alternate way to pass message/input to a WebJob is to make it event-driven like using a queue trigger and do processing on receiving the queue message. Refer this quick-start guide for details.

Below uses .net core 3.1 with web job sdk v3. For equivalent code with older sdk, referthis .

Queue triggered job:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace WebJobsSDKSample
{
    public class Functions
    {
        public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger logger)
        {
            // do stuffs with message
            logger.LogInformation(message);
        }
    }
}

Program.cs

using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;

namespace WebJobsSDKSample
{
    public class Program
    {
        static async Task Main()
        {
          var builder = new HostBuilder();
          builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
            });
          var host = builder.Build();
          using (host)
          {
            await host.RunAsync();
          }
       }
    }
}

You can also explore some old samples here .But these are using v1 SDK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM