【发布时间】: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;
}
}
}
P.S 由于我删除了一些识别信息,因此此代码中的命名存在一些不一致之处。请忽略。
编辑:我解决了这个问题,但我仍然不确定如何克服它。正如预期的那样,问题是我发布的代码没有覆盖 Azure 上的代码。我删除了我的应用程序并重新发布,它开始使用以下代码。我会将此作为答案,但是我不确定如何克服这个挑战,而不是在每次需要进行更改时都删除我的应用程序,这显然是不推荐的。
【问题讨论】:
-
最好的解决方法是使用像wireshark或fiddler这样的嗅探器。将请求中的 html 标头与工作和不工作的应用程序进行比较。
-
如何直接从 VS 发布代码?如果是,您是否已设置删除发布配置文件中的现有文件?
-
是的。我直接从 VS17 发布。但是,我没有在我的发布配置文件中看到删除现有文件的选项。我在哪里可以找到该选项?
标签: c# xml azure azure-functions