首页 > 解决方案 > Azure Functions 不会被 VS 发布的新内容覆盖?

问题描述

我有一个带有 HTTP 触发器的 Azure 函数应用程序,它接收自动 HTTP 消息,将消息记录到 Blob 存储,然后返回一个简单的 XML SOAP 信封响应,以确认收到 HTTP 消息。这是代码。注释掉的代码是我试图让它工作但也没有成功的其他方式。

当我在本地测试此代码时,它可以正常工作并返回 XML 响应。但是,当我将它发布到 Azure 时,它​​只会在响应正文中返回“200”。在此函数的先前迭代中,我在正文中返回了“200”字符串,所以我想知道我是否只是未能正确发布到 Azure。我检查了 Azure 活动日志并查看了与我的发布尝试相对应的应用程序更新。

我正在运行 .Net 4.6.1 和 .Net SDK 1.0.11

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;

namespace MyFunctionsApp
{
    public static class MyNotifications
    {
        [FunctionName("MyHttpTrigger")]
        public static async Task<HttpResponseMessage> MyHttpTrigger(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,
            [Blob("my-notifications", Connection = "StorageConnectionString")] CloudBlobContainer container,
            TraceWriter log)
        {
            log.Info("The MyHttpTrigger function was triggered.");
            var blobName = $"{DateTime.UtcNow.ToString("o")}-{CreateGuid()}";

            var blockBlobReference = container.GetBlockBlobReference(blobName);
            using (Stream stream = await req.Content.ReadAsStreamAsync())
            {   
                await blockBlobReference.UploadFromStreamAsync(stream);
            }

            // Tried using a StringBuilder to assemble my XML response in case there was an error with my formatting (double quotes etc...).
            StringBuilder xmlBuilder = new StringBuilder();
            xmlBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            xmlBuilder.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            xmlBuilder.Append("<soapenv:Body>");
            xmlBuilder.Append("<ReceiveNotificationResponse xmlns=\"http://apps.myapp.net/services/subscribers\" />");
            xmlBuilder.Append("</soapenv:Body>");
            xmlBuilder.Append("</soapenv:Envelope>");


            // Tried writing the XML response inline.
            //var xmlResponse = @"<?xml version=""1.0"" encoding=""utf-8""?>
            //            <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
            //            <soapenv:Body>
            //                <ReceiveNotificationResponse xmlns=""http://apps.MyAppName.net/services/subscribers"" />
            //            </soapenv:Body>
            //            </soapenv:Envelope>
            //        ";
            var response = new HttpResponseMessage
            {
                //StatusCode = HttpStatusCode.OK,
                Content = new StringContent(xmlBuilder.ToString(), Encoding.UTF8, "text/xml")

                // Tried reading the XML response from a .xml file
                // Content = new StringContent(File.ReadAllText("../../../../MyFunctionsApp/XmlResponseMessage.xml")),
            };

            // Set additional headers
            //response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
            //response.Content.Headers.ContentType.CharSet = "utf-8";
            //response.Content.Headers.Add("Content-Type", "text/xml");
            return response;
        }

        private static Guid CreateGuid()
        {
            Guid guid = Guid.NewGuid();
            return guid;
        }
    }
}

Azure 的响应不正确不正确的反应 本地测试时返回的预期响应预期响应

PS 由于我删除了一些识别信息,因此此代码中的命名存在一些不一致之处。请忽略这一点。

编辑:我解决了这个问题,但我仍然不确定如何克服它。正如预期的那样,问题是我发布的代码没有覆盖 Azure 上的代码。我删除了我的应用程序并重新发布,它开始使用以下代码。我会将此作为答案,但是我不确定如何克服这一挑战,而无需在每次需要进行更改时都删除我的应用程序,这显然不推荐。

标签: c#xmlazureazure-functions

解决方案


由于您已经确定问题是发布后文件似乎没有被覆盖,请尝试在发布配置文件中设置删除现有文件。

在发布面板上,单击Manage Profile settings...,然后选中Remove additional files at destination

在此处输入图像描述

请注意,这是一个潜在的解决方案,因为我没有遇到类似的问题,即使没有Remove additional files at destination,您提供的示例项目也可以按照我的预期进行更新(即从 200 到 xml 内容)。

顺便说一句,更新Microsoft.NET.Sdk.Functions到最新版本(现在是 1.0.24),以防我们因 SDK 过时而遇到问题。


推荐阅读