通过 Docker 和 Visual Studio 发布到 Azure 时,Azure 函数 HTTP 请求 404

Azure function HTTP Request 404 when published to Azure through Docker and Visual Studio

我正在尝试了解有关 Azure Functions 2.0 和 Docker 容器的更多信息以发布到我的 Azure 实例。我按照下面的教程进行操作,唯一的区别是我使用 visual studio 2019 将 docker 发布到 azure 中的容器注册表。

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-your-first-function-visual-studio

一切正常,我能够启动我的容器并访问该站点。但是,在示例中,您可以访问 /api/function1 并获得响应。这在我的本地主机上有效,但在实时站点上它 returns 一个 404。似乎 /api/function1 在发布后无法访问。

应用程序本身 returns 这是在访问 IP 本身时,所以我知道它正在运行。我是否需要在 Azure 中执行其他操作来公开我的 API?

我的容器日志只显示了这个。

Hosting environment: Production
Content root path: C:\
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

我从这里docker抓取了我的文件

https://github.com/Azure/azure-functions-docker/blob/master/host/2.0/nanoserver-1809/Dockerfile

# escape=`

# Installer image
FROM mcr.microsoft.com/windows/servercore:1809 AS installer-env

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Retrieve .NET Core SDK
ENV DOTNET_SDK_VERSION 2.2.402

RUN Invoke-WebRequest -OutFile dotnet.zip https://dotnetcli.blob.core.windows.net/dotnet/Sdk/$Env:DOTNET_SDK_VERSION/dotnet-sdk-$Env:DOTNET_SDK_VERSION-win-x64.zip; `
    $dotnet_sha512 = '0fa3bf476b560c8fc70749df37a41580f5b97334b7a1f19d66e32096d055043f4d7ad2828f994306e0a24c62a3030358bcc4579d2d8d439d90f36fecfb2666f6'; `
    if ((Get-FileHash dotnet.zip -Algorithm sha512).Hash -ne $dotnet_sha512) { `
        Write-Host 'CHECKSUM VERIFICATION FAILED!'; `
        exit 1; `
    }; `
    `
    Expand-Archive dotnet.zip -DestinationPath dotnet; `
    Remove-Item -Force dotnet.zip

ENV ASPNETCORE_URLS=http://+:80 `
    DOTNET_RUNNING_IN_CONTAINER=true `
    DOTNET_USE_POLLING_FILE_WATCHER=true `
    NUGET_XMLDOC_MODE=skip `
    PublishWithAspNetCoreTargetManifest=false `
    HOST_COMMIT=69f124faed40d20d9d8e5b8d51f305d249b21512 `
    BUILD_NUMBER=12858

RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; `
    Invoke-WebRequest -OutFile host.zip https://github.com/Azure/azure-functions-host/archive/$Env:HOST_COMMIT.zip; `
    Expand-Archive host.zip .; `
    cd azure-functions-host-$Env:HOST_COMMIT; `
    /dotnet/dotnet publish /p:BuildNumber=$Env:BUILD_NUMBER /p:CommitHash=$Env:HOST_COMMIT src\WebJobs.Script.WebHost\WebJobs.Script.WebHost.csproj --output C:\runtime


# Runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2.7-nanoserver-1809

COPY --from=installer-env ["C:\runtime", "C:\runtime"]

ENV AzureWebJobsScriptRoot=C:\approot `
    WEBSITE_HOSTNAME=localhost:80

CMD ["dotnet", "C:\runtime\Microsoft.Azure.WebJobs.Script.WebHost.dll"]

这是我的 azure 函数的 function1 代码

public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string productid = req.Query["productid"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            productid = productid ?? data?.product;

            Product newProduct = new Product()
            {
                ProductNumber = 0,
                ProductName = "Unknown",
                ProductCost = 0
            };
            if (Convert.ToInt32(productid) ==1)
            {
                newProduct = new Product()
                {
                    ProductCost = 100,
                    ProductName = "Lime Tree",
                    ProductNumber = 1
                };
            }
            else if(Convert.ToInt32(productid) == 2) 
            {
                newProduct = new Product()
                {
                    ProductCost = 500,
                    ProductName = "Lemon Tree",
                    ProductNumber = 2
                };
            }
            return productid != null
                ? (ActionResult)new JsonResult(newProduct)
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }

这是我的容器的照片 运行 我的图片。

我是新手,所以任何建议肯定会有帮助!

谢谢!

首先,我不知道您是否真的需要(或想要)运行 Windows 容器上的函数。如果你想在容器中 运行,我可能会选择 Linux。为此,这是一个示例 Dockerfile。它确实建立在 Microsoft 提供的基础映像之上。所以你不必从头开始构建它。

我确定 Windows 也有一个已经构建的基础映像。如果你需要它,只需在类似的回购中四处看看我猜。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

COPY . ./
RUN dotnet publish myfunction -c Release -o myfunction /out

FROM mcr.microsoft.com/azure-functions/dotnet:3.0 AS base
WORKDIR /app
EXPOSE 80

COPY --from=build-env /app/ao-backendfunctions/out .

ENV AzureWebJobsScriptRoot=/app
ENV AzureFunctionsJobHost__Logging__Console__IsEnabled=true

重要的部分是RUN dotnet publish myfunction -c Release -o myfunction /out。将 myfunction 替换为您实际函数的(文件夹)名称。

@silent 的回答是正确的 - Linux 容器是 Azure Functions 的必经之路。我的环境没有为 Linux 容器正确设置,但一旦我获得正确的环境,它就可以立即使用。

这是我为另一个使用 Linux 容器

的项目创建的最新 DockerFile

请参阅 https://aka.ms/containerfastmode 以了解 Visual Studio 如何使用此 Dockerfile 构建您的映像以加快调试速度。

FROM mcr.microsoft.com/azure-functions/dotnet:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /src
COPY ["FunctionTestAppLinux/FunctionTestAppLinux.csproj", "FunctionTestAppLinux/"]
RUN dotnet restore "FunctionTestAppLinux/FunctionTestAppLinux.csproj"
COPY . .
WORKDIR "/src/FunctionTestAppLinux"
RUN dotnet build "FunctionTestAppLinux.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "FunctionTestAppLinux.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV AzureWebJobsScriptRoot=/app