简体   繁体   English

有什么方法可以将输入插入到连续运行的 webjob 中吗?

[英]is there any way to insert input to continuously running webjob?

I have a simple webjob hosted on azure as a continuously running job.我在 azure 上托管了一个简单的 webjob 作为持续运行的作业。

I need to pass token value as a input , how can I achieve this using continuous web job?我需要将令牌值作为输入传递,如何使用连续的 Web 作业来实现这一点?

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).我不认为使用 webjobs(连续作业)是可能的,除非你有一些 web 服务器在运行,你可以在其中发出请求(例如 sinatra、aspnet 核心等)。 You need to use Azure Functions with HTTP Trigger.需要将 Azure Functions 与 HTTP 触发器配合使用。 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. Webjob 是后台服务,因此无论如何您都无法使用Console.ReadLine()获取用户输入。 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.由于 WebJob 没有像 Azure Function 这样的 HTTP 触发器,我认为将消息/输入传递给 WebJob 的唯一替代方法是使其像使用队列触发器一样由事件驱动,并在接收队列消息时进行处理。 Refer this quick-start guide for details.有关详细信息,请参阅此快速入门指南

Below uses .net core 3.1 with web job sdk v3.下面使用 .net core 3.1 和 web job sdk v3。 For equivalent code with older sdk, referthis .有关旧版 sdk 的等效代码,请参阅

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程序.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.您还可以在此处探索一些旧示例。但这些示例使用的是 v1 SDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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