【问题标题】:Azure Functions not be overwritten by new content published from VS?Azure Functions 不会被 VS 发布的新内容覆盖?
【发布时间】:2019-01-24 14:58:20
【问题描述】:

我有一个带有 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 的响应不正确 本地测试时返回的预期响应

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

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

【问题讨论】:

  • 最好的解决方法是使用像wireshark或fiddler这样的嗅探器。将请求中的 html 标头与工作和不工作的应用程序进行比较。
  • 如何直接从 VS 发布代码?如果是,您是否已设置删除发布配置文件中的现有文件?
  • 是的。我直接从 VS17 发布。但是,我没有在我的发布配置文件中看到删除现有文件的选项。我在哪里可以找到该选项?

标签: c# xml azure azure-functions


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢。这似乎是解决方案。
【解决方案2】:

您可以尝试返回ContentResult 吗?例如

StringBuilder xmlBuilder = new StringBuilder();

// ... build xml....

return new ContentResult
{
    Content = xmlBuilder.ToString(),
    ContentType = @"application/xml",
    StatusCode = StatusCodes.Status200OK
};

【讨论】:

  • 我过去曾尝试返回 ContentResult;它没有效果。不过,我解决了我的问题。检查我的编辑。
【解决方案3】:

在返回中添加如下媒体类型,

return new OkObjectResult(xmlDoc) { ContentTypes = new Microsoft.AspNetCore.Mvc.Formatters.MediaTypeCollection { @"application/xml" } };

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 2019-03-20
    • 2023-01-28
    • 2020-06-24
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多