[英]azure functions, Multiple triggers in same .net project
I am new to azure web functions and I can't seem to find any documentation about multiple triggers in the same project. 我是天蓝色的Web函数的新手,我似乎找不到关于同一项目中多个触发器的任何文档。 I have created my solution and already created a nice TimerTrigger , which works fine. 我已经创建了解决方案,并且已经创建了一个不错的TimerTrigger ,它可以正常工作。
This trigger downloads files from an ftp directory and then uploads the files to our azure storage account. 此触发器从ftp目录下载文件,然后将文件上传到我们的azure存储帐户。 The code looks like this: 代码如下:
[DependencyInjectionConfig(typeof(DependencyInjectionConfig))]
public class FtpTrigger
{
[FunctionName("EOrderTimerTrigger")]
public static async Task Run(
[TimerTrigger("*/15 * * * * *")]TimerInfo myTimer,
TraceWriter log,
[Inject]IConfig config,
[Inject]SmartFileClient smartFileClient,
[Inject]SettingsHandler settingsHandler,
[Inject]StorageHandler storageHandler)
{
if (myTimer.IsPastDue)
log.Info("Timer is running late!");
log.Info($"Listing directories from { config.FtpUrl }");
var accounts = await smartFileClient.ListDirectories(config.FtpPath);
if (!accounts.Any())
{
log.Warning($"There are no files waiting to be processed for any account");
return;
}
foreach (var account in accounts)
{
try
{
log.Info($"Retrieving settings for { account }");
var url = $"{config.ApiBaseUrl}/{config.ApiSettingsPath}";
var settings = await settingsHandler.GetAsync(url, account);
log.Info($"Find all order files for { account }");
var fileNames = await smartFileClient.ListFiles($"{config.FtpPath}/{account}", settings.OrderFileSuffix.Replace(".", ""));
if (!fileNames.Any())
{
log.Warning($"No files to process for { account }");
continue;
}
log.Info($"Get a list of files awaiting to be processed for { account }");
var awaiting = await storageHandler.ListAsync(config.StorageProcessingContainer, account);
foreach(var fileName in fileNames)
{
log.Info($"Finding any files awaiting to be processed in the storage account for { account }");
var friendlyName = Regex.Replace(fileName, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled); ;
var match = awaiting.Any(m => m.Equals(friendlyName));
if (match)
{
log.Warning($"File ({fileName}) already awaiting to be processed for { account }");
continue;
}
log.Info($"Download { fileName } from the ftp directory for { account }");
var bytes = await smartFileClient.DownloadFile($"{config.FtpPath}/{account}", fileName);
log.Info($"Upload { fileName } to the Azure Storage account for { account }");
await storageHandler.UploadAsync(friendlyName, bytes, config.StorageProcessingContainer, account);
log.Info($"Delete { fileName } from the ftp directory for { account }");
if (!await smartFileClient.DeleteFile($"{config.FtpPath}/{account}", fileName))
log.Error($"Failed to delete { fileName } from the ftp directory for { account }");
}
} catch (Exception ex)
{
log.Error($"{ ex.Message }");
}
}
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
Now I want to add a second trigger. 现在,我想添加第二个触发器。 This trigger is going to be a BlobTrigger . 该触发器将是BlobTrigger 。 I added it to my project and ran it and it never fired, even when files were created. 我将其添加到我的项目中并运行它,即使创建了文件,它也从未触发过。 So I realised I must be doing something wrong. 所以我意识到我一定做错了。
Can someone tell me how to have multiple triggers in a project? 有人可以告诉我如何在一个项目中使用多个触发器吗? And if it can't be done; 如果无法完成, what is the alternative? 有什么选择?
Actually, you could add multiple trigger in a same azure function project. 实际上,您可以在同一个Azure功能项目中添加多个触发器。
I added it to my project and ran it and it never fired, even when files were created. 我将其添加到我的项目中并运行它,即使创建了文件,它也从未触发过。
You could follow this article . 您可以阅读本文 。
[FunctionName("Function2")]
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "blobconnect")]Stream myBlob, string name, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}
Note : The string {name} in the blob trigger path samples-workitems/{name} creates a binding expression that you can use in function code to access the file name of the triggering blob. 注意 :Blob触发路径samples-workitems / {name}中的字符串{name}创建一个绑定表达式,您可以在函数代码中使用该绑定表达式来访问触发Blob的文件名。 For more information, see Blob name patterns later in this article. 有关更多信息,请参见本文后面的Blob名称模式。
You only need to modify samples-workitems
to your container name
. 您只需要将samples-workitems
修改为您的container name
。 And set the blob connection in local.hosting.json
file. 并在local.hosting.json
文件中设置Blob连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.