如何在本地测试Azure Webjobs SDK项目?

Sam*_*ica 10 azure-webjobs azure-webjobssdk

我希望能够在实际将Azure WebJobs SDK项目发布到Azure之前在本地测试它.

如果我创建一个全新的Azure Web Jobs项目,我会得到一些如下所示的代码:

Program.cs中:

// To learn more about Microsoft Azure WebJobs SDK, please see http://go.microsoft.com/fwlink/?LinkID=320976
class Program
{
    // Please set the following connection strings in app.config for this WebJob to run:
    // AzureWebJobsDashboard and AzureWebJobsStorage
    static void Main()
    {
        var host = new JobHost();
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
}
Run Code Online (Sandbox Code Playgroud)

Functions.cs:

public class Functions
{
    // This function will get triggered/executed when a new message is written 
    // on an Azure Queue called queue.
    public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
    {
        log.WriteLine(message);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想绕过测试QueueTrigger函数是否正常工作,但我甚至无法做到这一点,因为host.RunAndBlock();我得到以下异常:

mscorlib.dll中发生了未处理的"System.InvalidOperationException"类型异常

其他信息:Microsoft Azure WebJobs SDK仪表板连接字符串缺失或为空.可以通过以下方式设置Microsoft Azure存储帐户连接字符串:

  1. 按以下格式在.config文件的connectionStrings部分中设置名为"AzureWebJobsDashboard"的连接字符串,或者

  2. 设置名为"AzureWebJobsDashboard"的环境变量,或

  3. 设置JobHostConfiguration的相应属性.

我运行了存储模拟器,并设置了Azure AzureWebJobsDashboard连接字符串,如下所示:

<add name="AzureWebJobsDashboard" connectionString="UseDevelopmentStorage=true" />
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,我得到了一个不同的错误

mscorlib.dll中发生了未处理的"System.InvalidOperationException"类型异常

其他信息:无法验证Microsoft Azure WebJobs SDK仪表板帐户.不支持Microsoft Azure存储模拟器,请使用Microsoft Azure中托管的Microsoft Azure存储帐户.


有没有办法测试我在本地使用WebJobs SDK?

小智 7

WebJobs 2.0现在可以使用开发存储(我正在使用v2.0.0-beta2).

请注意,一般的延迟和特别是Blob触发器目前远远好于生产中的延迟.小心设计.


lop*_*oni 5

如果要在本地测试 WebJobs SDK,则需要在 Azure 中设置存储帐户。您无法针对 Azure 模拟器对其进行测试。这就是该错误告诉您的内容。

无法验证 Microsoft Azure WebJobs SDK 仪表板帐户。不支持 Microsoft Azure 存储模拟器,请使用 Microsoft Azure 中托管的 Microsoft Azure 存储帐户。

因此,要回答您的问题,您可以使用门户在 Azure 中创建一个存储帐户,然后在控制台应用程序的app.config中设置连接字符串。然后,只需将消息放入队列并在本地运行控制台应用程序,它就会拾取该消息(假设您显然正在尝试与队列交互)。

确保将[QueueTrigger("queue")]“队列”替换为要轮询的队列的名称。

希望这可以帮助

  • 我们有一个用于本地模拟器支持的跟踪项目,但目前不支持。请参阅此处的跟踪项目:https://github.com/Azure/azure-webjobs-sdk/issues/53。请注意,不需要 AzureWebJobsDashboard 连接字符串(仅当您想要在仪表板中查看日志时)。需要 AzureWebJobsStorage 连接字符串。 (3认同)