c# - 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;
}
}
}
PS 由于我删除了一些识别信息,因此此代码中的命名存在一些不一致之处。请忽略这一点。
编辑:我解决了这个问题,但我仍然不确定如何克服它。正如预期的那样,问题是我发布的代码没有覆盖 Azure 上的代码。我删除了我的应用程序并重新发布,它开始使用以下代码。我会将此作为答案,但是我不确定如何克服这一挑战,而无需在每次需要进行更改时都删除我的应用程序,这显然不推荐。
解决方案
由于您已经确定问题是发布后文件似乎没有被覆盖,请尝试在发布配置文件中设置删除现有文件。
在发布面板上,单击Manage Profile settings...
,然后选中Remove additional files at destination
。
请注意,这是一个潜在的解决方案,因为我没有遇到类似的问题,即使没有Remove additional files at destination
,您提供的示例项目也可以按照我的预期进行更新(即从 200 到 xml 内容)。
顺便说一句,更新Microsoft.NET.Sdk.Functions
到最新版本(现在是 1.0.24),以防我们因 SDK 过时而遇到问题。
推荐阅读
- java - Jboss聚类
- c# - UDPClient 不接收数据
- python - Python:Peer无法在P2P实现网络中监听连接
- jenkins - 如何从外部系统调用 SOAP UI 请求?
- python - 将逗号分隔的序列从 DataFrame 读取到 Python 中的字符串
- javascript - 杀死 Linux/Ubuntu 上的所有 Node.js 进程
- c# - 如何将 PropertyGrid 的 CategoryAttribute 设置为列表的 SelectedItem
- javascript - Vue.js/Vuex:在创建生命周期钩子上直接调用 getter 与访问状态值
- java - 使用两个组合框设置在数据库中搜索并在 jtable 中显示的条件
- python - 如何处理目录中的多标签数据集以在 PyTorch 中添加图像字幕