如何在Azure函数中修复“ HostInitializationException”

时间:2019-06-05 08:17:10

标签: python azure azure-functions

首先,我按照说明进行操作以使功能应用程序运行。 https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-python

成功创建应用程序后,我已将一个小的HTTP触发功能部署到Azure以进行测试。

我的函数是用python编写的。我正在使用Linux OS推送到Azure。 一切看起来都很好。

我使用以下语句进行发布: func azure functionapp发布myApp --publish-local-settings

在成功部署到Azure之后,我尝试访问“ https://myAppName.azurewebsites.net”,这首先给了我一个错误代码为502的站点。 几分钟后,它更改了状态,我得到了Azure Functions的欢迎页面。

如果我尝试直接通过以下方式访问该功能:https://myAppName.azurewebsites.net/api/functionName

我得到502。即使等待30分钟后,该功能仍无法正常运行。.

如果您有相关的有用信息,请告诉我。

在“ Application Insights”中查看会显示更多信息:

09:55:40 | Trace@(none)
Hosting stopping
09:55:40 | Exception | HostInitializationException@(none)
Did not find functions with language [python].
09:55:40 | Trace@(none)
A host error has occurred
09:55:40 | Trace@(none)
Creating function descriptors.
09:55:40 | Trace@(none)
Adding Function descriptor provider for language python.
09:55:40 | Trace@(none)
1 proxies loaded
import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )


{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}
{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}
{
  "version": "2.0"
}

1 个答案:

答案 0 :(得分:0)

出现此错误的可能原因是因为local.settings.json文件,以及使用--publish-local-settings覆盖Azure Function App中的设置而将其发布到Azure的原因。

使用下面的文件,可以在本地托管它,也可以将其发布到Azure。

local.settings.json

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

host.json

{
    "version":  "2.0"
}

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

init .py

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

在VS Code中运行以下步骤

func init MyFunctionProj2 (selected Python)
cd .\MyFunctionProj2\
func new (selected HTTP trigger)
func host start     

#Pick your region of choice

az functionapp create --resource-group rgname --os-type Linux --consumption-plan-location westeurope  --runtime python --name funcappname --storage-account storageaccountname

func azure functionapp publish funcappname

在本地托管时,可在以下URL上使用

enter image description here

按照上面列出的步骤发布到Azure Function时

enter image description here

您可以在Function中从Azure门户获取函数URL

enter image description here

检查Function App设置是否具有正确的连接字符串以进行存储。

Additional documentation reference for local settings file.

希望这会有所帮助。