首页 > 解决方案 > Azure Function 默认模板无法在本地运行

问题描述

我正在尝试创建 Azure Function 并在本地对其进行测试。我有 MSFT VS2017 + Azure 开发工具。我已经使用 TimerTrigger 创建了 Azure Function 项目。我没有更改默认模板中的任何内容。

功能是:

    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }

当我运行它时:

              %%%%%%
             %%%%%%
        @   %%%%%%    @
      @@   %%%%%%      @@
   @@@    %%%%%%%%%%%    @@@
 @@      %%%%%%%%%%        @@
   @@         %%%%       @@
     @@      %%%       @@
       @@    %%      @@
            %%
            %

Azure Functions Core Tools (2.7.1158 Commit hash: f2d2a2816e038165826c7409c6d10c0527e8955b)
Function Runtime Version: 2.0.12438.0
SKipping 'FUNCTIONS_CORETOOLS_ENVIRONMENT' because value is null
[5/15/2019 10:11:50 AM] Starting Rpc Initialization Service.
[5/15/2019 10:11:50 AM] Initializing RpcServer
[5/15/2019 10:11:50 AM] Building host: startup suppressed:False, configuration suppressed: False
[5/15/2019 10:11:52 AM] Initializing Host.
[5/15/2019 10:11:52 AM] Host initialization: ConsecutiveErrors=0, StartupCount=1
[5/15/2019 10:11:52 AM] LoggerFilterOptions
[5/15/2019 10:11:52 AM] {
[5/15/2019 10:11:52 AM]   "MinLevel": "None",
[5/15/2019 10:11:52 AM]   "Rules": [
[5/15/2019 10:11:52 AM]     {
[5/15/2019 10:11:52 AM]       "ProviderName": null,
[5/15/2019 10:11:52 AM]       "CategoryName": null,
[5/15/2019 10:11:52 AM]       "LogLevel": null,
[5/15/2019 10:11:52 AM]       "Filter": "<AddFilter>b__0"
[5/15/2019 10:11:52 AM]     },
[5/15/2019 10:11:52 AM]     {
[5/15/2019 10:11:52 AM]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[5/15/2019 10:11:52 AM]       "CategoryName": null,
[5/15/2019 10:11:52 AM]       "LogLevel": "None",
[5/15/2019 10:11:52 AM]       "Filter": null
[5/15/2019 10:11:52 AM]     },
[5/15/2019 10:11:52 AM]     {
[5/15/2019 10:11:52 AM]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[5/15/2019 10:11:52 AM]       "CategoryName": null,
[5/15/2019 10:11:52 AM]       "LogLevel": null,
[5/15/2019 10:11:52 AM]       "Filter": "<AddFilter>b__0"
[5/15/2019 10:11:52 AM]     }
[5/15/2019 10:11:52 AM]   ]
[5/15/2019 10:11:52 AM] }
[5/15/2019 10:11:52 AM] FunctionResultAggregatorOptions
[5/15/2019 10:11:52 AM] {
[5/15/2019 10:11:52 AM]   "BatchSize": 1000,
[5/15/2019 10:11:52 AM]   "FlushTimeout": "00:00:30",
[5/15/2019 10:11:52 AM]   "IsEnabled": true
[5/15/2019 10:11:52 AM] }
[5/15/2019 10:11:52 AM] SingletonOptions
[5/15/2019 10:11:52 AM] {
[5/15/2019 10:11:52 AM]   "LockPeriod": "00:00:15",
[5/15/2019 10:11:52 AM]   "ListenerLockPeriod": "00:00:15",
[5/15/2019 10:11:52 AM]   "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
[5/15/2019 10:11:52 AM]   "LockAcquisitionPollingInterval": "00:00:05",
[5/15/2019 10:11:52 AM]   "ListenerLockRecoveryPollingInterval": "00:01:00"
[5/15/2019 10:11:52 AM] }
[5/15/2019 10:11:52 AM] Starting JobHost
[5/15/2019 10:11:52 AM] Starting Host (HostId=, InstanceId=79b21f7e-8904-41d9-9339-df547871c510, Version=2.0.12438.0, ProcessId=10380, AppDomainId=1, InDebugMode=False, InDiagnosticMode=False, FunctionsExtensionVersion=)
[5/15/2019 10:11:52 AM] Loading functions metadata
[5/15/2019 10:11:52 AM] 1 functions loaded
[5/15/2019 10:11:52 AM] WorkerRuntime: dotnet. Will shutdown other standby channels

它在这里失败了:

namespace Microsoft.Azure.WebJobs.Script.Description
{
    /// <summary>
    /// Establishes an assembly load context for a extensions, functions and their dependencies.
    /// </summary>
    public partial class FunctionAssemblyLoadContext : AssemblyLoadContext
....
protected static string ResolveFunctionBaseProbingPath()
{
    string basePath = null;

    if (ScriptSettingsManager.Instance.IsAppServiceEnvironment)
    {
        string home = Environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHomePath);
        basePath = Path.Combine(home, "site", "wwwroot"); // <-- HOME IS NULL and it throws ArgumentNullException
    }
    else
    {
        basePath = Environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsScriptRoot) ?? AppContext.BaseDirectory;
    }

    return Path.Combine(basePath, "bin");
}

有人遇到过这个问题吗?我应该怎么做才能使默认的 Azure 函数模板运行?

标签: azure-functions

解决方案


最有可能的是,如果您刚刚尝试运行默认设置,则问题出在您的 local.settings.json 中。

如果您打开文件,您将拥有:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

那是对的吗?

如果是这样,您需要将存储连接字符串放在 AzureWebJobsStorage 中。它看起来像:

DefaultEndpointsProtocol=https;AccountName=<account_name>;AccountKey=<account_key>;EndpointSuffix=core.windows.net

然后脚本应该运行没有错误。


推荐阅读