1

我需要配置使用Azure队列和Web作业的SharePoint Online团队客房。 我创建了一个控制台应用程序,并公布为连续Web作业使用以下设置:Azure Web作业 - 来自队列的并行消息处理无法正常工作

  config.Queues.BatchSize = 1; 

      config.Queues.MaxDequeueCount = 4; 

      config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(15); 

      JobHost host = new JobHost(); 

      host.RunAndBlock(); 

触发功能如下:

public static void TriggerFunction([QueueTrigger("messagequeue")]CloudQueueMessage message) 
    { 
     ProcessQueueMsg(message.AsString); 
    } 

内ProcessQueueMsg功能即时deserialising接收JSON消息在一个类中运行以下操作:

  • 我在现有网站集中创建子网站;
  • 使用Pnp供应引擎,我在供应内容的子 网站(列表,上传文件,权限,快速午餐等)。

如果在队列中我只有一条消息要处理,那么一切正常。

但是,当我在延迟几秒的队列中发送两条消息时,在处理第一条消息时,下一条消息覆盖类属性,并且第一条消息结束。

试图在单独的线程中运行每条消息,但触发器函数在我的函数内部处理消息之前标记为成功。这样我就无法控制潜在的异常/消息出队。

试图也给线程数限制为1和信号灯使用,但有相同的行为:

private const int NrOfThreads = 1; 
private static readonly SemaphoreSlim semaphore_ = new SemaphoreSlim(NrOfThreads, NrOfThreads); 

//Inside TriggerFunction 

      try 
      { 
       semaphore_.Wait(); 

       new Thread(ThreadProc).Start(); 
      } 
      catch (Exception e) 
      { 
       Console.Error.WriteLine(e); 
      } 

public static void ThreadProc() 
     { 
      try 
      { 
       DoWork(); 
      } 
      catch (Exception e) 
      { 
       Console.Error.WriteLine(">>> Error: {0}", e); 
      } 
      finally 
      { 
       // release a slot for another thread 
       semaphore_.Release(); 
      } 
     } 

     public static void DoWork() 
     { 
      Console.WriteLine("This is a web job invocation: Process Id: {0}, Thread Id: {1}.", System.Diagnostics.Process.GetCurrentProcess().Id, Thread.CurrentThread.ManagedThreadId); 
      ProcessQueueMsg(); 
      Console.WriteLine(">> Thread Done. Processing next message."); 
     } 

有没有一种方法可以让我以运行并行的消息我的处理功能,提供我的网站没有干扰?

如果您需要更多详细信息,请让我知道。

预先感谢您!

回答

3

你没有将配置对象传递给JobHost的构造 - 这就是为什么你的配置设置没有效果。将您的代码更改为:

JobHost host = new JobHost(config); 
host.RunAndBlock();