问题描述
我正在.net core 3.1中创建一个webjob。在这个项目中,我有一个被计时器激活的函数,该函数应该读取队列Q1中的消息数,如果为空,则将消息放入Q2并触发对API的调用。
为了检查API中有多少条消息,我需要访问appsettings.json中的AzureWebJobsstorage
,然后访问设置中的url。
Program.cs
class Program
{
static async Task Main()
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
});
builder.ConfigureLogging((context,b) =>
{
b.AddConsole();
});
builder.ConfigureAppConfiguration((context,b) =>
{
b.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json",optional: false,reloadOnChange: true)
.AddEnvironmentvariables();
});
builder.ConfigureServices((context,services) =>
{
var mySettings = new MySettings
{
AzureWebJobsstorage = context.Configuration.GetValue<string>("AzureWebJobsstorage"),AzureWebJobsDashboard = context.Configuration.GetValue<string>("AzureWebJobsDashboard"),url = context.Configuration.GetValue<string>("url"),};
services.AddSingleton(mySettings);
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
}
Fuctions.cs
public class Functions
{
public static void UpdateChannels([QueueTrigger("Q1")] string message,ILogger logger)
{
logger.Log@R_467_4045@ion(message);
}
public static void WhatIsTheretoUpdate([QueueTrigger("Q2")] string message,ILogger logger)
{
logger.Log@R_467_4045@ion(message);
}
public static void CronJob([TimerTrigger("0 * * * * *")] TimerInfo timer,[Queue("Q2")] out string message,ILogger logger,MySettings mySettings)
{
message = null;
// Get the connection string from app settings
string connectionString = mySettings.AzureWebJobsstorage;
logger.Log@R_467_4045@ion("Connection String: " + connectionString);
// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString,"Q1");
if (queueClient.Exists())
{
QueueProperties properties = queueClient.GetProperties();
// Retrieve the cached approximate message count.
int cachedMessagesCount = properties.ApproximateMessagesCount;
// display number of messages.
logger.Log@R_467_4045@ion($"Number of messages in queue: {cachedMessagesCount}");
if (cachedMessagesCount == 0)
message = "Hello World!" + System.DateTime.Now.ToString(); //here I would call the REST API as well
}
logger.Log@R_467_4045@ion("Cron job fired!");
}
}
appsettings.json
{
"AzureWebJobsstorage": "constr","AzureWebJobsDashboard": "constr","url": "url"
}
我的设置
public class MySettings
{
public string AzureWebJobsstorage { get; set; }
public string AzureWebJobsDashboard { get; set; }
public string url { get; set; }
}
但是,当我运行它时,出现以下错误:
错误索引方法'Functions.CronJob' Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexingException:错误索引方法“ Functions.CronJob” ---> system.invalidOperationException:无法绑定参数“ mySettings”以键入MySettings。确保绑定支持参数“类型”。如果您正在使用绑定扩展(例如Azure存储,ServiceBus,Timer等),请确保已在启动代码中调用了扩展的注册方法(例如builder.AddAzureStorage(),builder.AddServiceBus( ),builder.AddTimers()等)。
除了上面代码中显示的内容之外,我还尝试使用ConfigurationManager
和Environment.GetEnvironmentvariable
,当我尝试读取值时,这两种方法都给了我空值。例如ConfigurationManager.AppSettings.GetValues("AzureWebJobsstorage")
。
我还尝试将IConfiguration
注册为服务services.AddSingleton(context.Configuration);
并将其注入参数中(而不是MySettings
),但这也给了我同样的绑定错误。
我真的很茫然,我在SO档案库中进行搜索以寻求解决方案,我想我尝试了所看到的给人们带来积极成果的一切,但是不幸的是,我没有其他海报那样幸运。
非常感谢任何指导。
已编辑以添加我的包裹
以防万一,我在使用以下
Azure.Storage.Queues (12.4.0)
Microsoft.Azure.WebJobs.Extensions (3.0.6)
Microsoft.Azure.WebJobs.Extensions.Storage (4.0.2)
Microsoft.Extensions.Logging.Console (3.1.7)
解决方法
使用DI时,建议您使用非静态方法和构造函数注入。
这是Functions.cs:
public class Functions
{
private readonly MySettings mySettings;
public Functions(MySettings _mySettings)
{
mySettings = _mySettings;
}
public void ProcessQueueMessage([TimerTrigger("0 */1 * * * *")] TimerInfo timer,[Queue("queue")] out string message,ILogger logger)
{
message = null;
string connectionString = mySettings.AzureWebJobsStorage;
logger.LogInformation("Connection String: " + connectionString);
}
}
其他.cs文件中的代码未更改。
这是测试结果: