【问题标题】:Azure WebJob won't run in VS 2015?Azure WebJob 不会在 VS 2015 中运行?
【发布时间】:2017-06-30 17:48:50
【问题描述】:

我是 Azure WebJobs 开发的新手,我正在尝试在 Visual Studio 中测试我的 WebJob。我的作业连续运行,但不执行 runWebJob() 方法?我在 host.CallAsync 行中指定的。这是我的代码:

public class Program {
    static void Main()
    {
        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }
        var host = new JobHost(config);

        host.CallAsync(typeof(Program).GetMethod("runWebJob"));
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();

    }

    [NoAutomaticTrigger]
    public static async Task runWebJob()
    {
        Console.WriteLine("RUNNING METHOD"); 
    }
}

【问题讨论】:

  • 当您尝试运行此应用程序时,控制台输出是什么?
  • 完全没有输出。它到达 host.CallAsync 没有问题,但什么也没做......
  • 当它运行 Microsoft.Azure.WebJobs.JobHost.CallAsync Id = 8,状态 = WaitingForActivation,方法 =“{null}”,结果 =“{尚未计算} "
  • 尝试将 .Wait() 添加到 CallAsync() 方法的末尾。它无论如何都应该运行,但它是一个异步调用,您需要明确说明您期望它何时完成。
  • @MCooper 你的函数不是异步的,它会同步运行,因为它缺少 await 关键字。

标签: c# .net azure azure-webjobs


【解决方案1】:

我用你提到的代码进行测试。我们需要设置class Program public。那么它应该可以工作了。

由于你的异步函数没有等待,我添加了等待进行测试。

public class Program
{
    static void Main(string[] args)
    {

        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }
        var host = new JobHost();

        host.CallAsync(typeof(Program).GetMethod("runWebJob"));
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();

    }
    [NoAutomaticTriggerAttribute]
    public static async Task runWebJob()
    {
        await Task.Delay(1000); //just for test
        Console.WriteLine("RUNNING METHOD");
    }

我自己测试一下。

注意:如果你把host.CallAsync(typeof(Program).GetMethod("runWebJob"))改成host.Call(typeof(Program).GetMethod("runWebJob")),然后把函数改成同步函数,你会看到详细的错误。

更新:

默认情况下,WebJobs SDK 会查找名为 AzureWebJobsStorage 和 AzureWebJobsDashboard 的连接字符串

如果您没有在app.config文件中添加以下连接字符串,请尝试添加。更多详情请参考document

 <connectionStrings>
        <add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=[accountname];AccountKey=[accesskey]"/>
        <add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=[accountname];AccountKey=[accesskey]"/>
</connectionStrings>

【讨论】:

  • 这对我不起作用...我的代码仍然无法运行?这可能是因为我的 App.config 文件有问题吗?我要做的就是运行它以查看它是否在 VS Studio 中有效。
  • 我已经更新了答案。如果您尚未添加 AzureWebJobsStorage 连接字符串,请将其添加到 app.config 文件中。如果它不起作用,您能否提供有关它的更多信息。
  • Sun MSFT 我的 App.config 文件中有以下内容:&lt;add name="AzureWebJobsDashboard" connectionString="UseDevelopmentStorage=true;"/&gt; &lt;add name="AzureWebJobsStorage" connectionString="UseDevelopmentStorage=true;"/&gt;
  • 如果您没有启动存储模拟器,请尝试启动它。或者您可以尝试使用 Azure 存储帐户连接字符串吗?
  • 你有关于这个线程的任何更新吗?如果有用,请将其标记为答案,以帮助更多有相同问题的社区。​​span>
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多