Azure Webjobs和Servicebus触发器-在升级nuget包后无法找到功能
本文关键字:功能 nuget Servicebus Webjobs 触发器 Azure | 更新日期: 2023-09-27 18:15:47
我不确定是否有什么改变,但是我们的Windows Azure webjob JobHost不再看到functions .cs文件中包含的函数。我们最近刚刚在这个解决方案上升级了Azure Nuget包(不确定是否相关)。
Program.cs
using M5Worker.Classes;
using Microsoft.Azure.WebJobs;
using Microsoft.ServiceBus.Messaging;
using System;
using TuesPechkin;
namespace M5Worker
{
// To learn more about Microsoft Azure WebJobs SDK, please see http://go.microsoft.com/fwlink/?LinkID=320976
public class Program
{
// This is needed for pechkin do not remove!
public static readonly IConverter converter =
new ThreadSafeConverter(
new PdfToolset(
new Win64EmbeddedDeployment(
new TempFolderDeployment())));
//published with devmode false
public static bool DevMode = false;
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
public static void Main()
{
libUFE.clsLogging cLog = new libUFE.clsLogging("M5");
try
{
cLog.LogWrite(System.Reflection.MethodBase.GetCurrentMethod().Name, 0, "", "", "Information", "M5Worker has Started", 0, "");
JobHostConfiguration jhc = new JobHostConfiguration();
if (DevMode == false)
{
jhc.ServiceBusConnectionString = Properties.Settings.Default.strSBConnProd;
}
else
{
jhc.ServiceBusConnectionString = Properties.Settings.Default.strSBConnDev;
}
jhc.DashboardConnectionString = "##################";
jhc.StorageConnectionString = "##################";
jhc.Queues.MaxPollingInterval = TimeSpan.FromSeconds(30);
jhc.NameResolver = new QueueNameResolver();
JobHost jh = new JobHost(jhc);
jh.RunAndBlock();
cLog.LogWrite(System.Reflection.MethodBase.GetCurrentMethod().Name, 0, "", "", "Information", "M5Worker has Finished", 0, "");
}
catch (Exception ex)
{
cLog.LogWrite(System.Reflection.MethodBase.GetCurrentMethod().Name, 0, "", "", "Error", ex.Message, 0, "");
cLog.LogWrite(System.Reflection.MethodBase.GetCurrentMethod().Name, 0, "", "", "Information", "M5Worker has Finished", 0, "");
}
}
public class QueueNameResolver : INameResolver
{
public string Resolve(string name)
{
if (DevMode == true)
return "m5queuedev";
else
return "m5queue";
}
}
Function.cs
using Microsoft.ApplicationServer.Caching;
using Microsoft.Azure.WebJobs;
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json;
using System;
using M5Worker.DB;
using System.Threading;
using System.Data;
using M5Worker.Classes;
namespace M5Worker
{
public class Functions
{
public static libUFE.clsLogging cLog = new libUFE.clsLogging("M5");
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public static void M5Worker([ServiceBusTrigger("%queuename%")] BrokeredMessage message)
{
if (message != null)
{
try
{
if (message.Properties.Count > 0 && message.Properties.ContainsKey("Function"))
{
//message.Complete();
switch (message.Properties["Function"].ToString().ToUpper())
{
case "CREATEELECTRONICFORECLOSURE":
FunctionHandler.ProcessMessage(message, clsElectronicForeclosure.processItem, true);
break;
case "PROCESSCUSTOMER":
FunctionHandler.ProcessMessage(message, clsProcessCustomer.ProcessCustomerObject, true);
break;
}
}
else
{
throw new Exception("Message does not contain function");
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
cLog.LogWrite(System.Reflection.MethodBase.GetCurrentMethod().Name, 0, "", "", "Error", ex.Message, 0, "");
// Indicate a problem, unlock message in queue
message.DeadLetter();
}
message.Dispose();
}
}
}
}
结果https://i.stack.imgur.com/0ocZ5.jpg
我一直在寻找,并找到了与确保函数类是公共静态相关的答案,但仅此而已。
您现在必须在启动代码"config.UseServiceBus()"中添加额外的行来注册ServiceBus支持。示例如下:http://github.com/Azure/azure-webjobs-sdk-samples/blob/master/BasicSamples/ServiceBus/Program.cs#L26
我们这样做是为了将ServiceBus从核心WebHobs SDK库中解耦,并且我们还将其移到新的可扩展性模型中。
请将这行添加到你的启动代码中,你应该会很好。