我正在尝试从azure函数中读取POST内容。出于开发原因,我决定从Azure门户复制确切的样本,我的代码如下:
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
using Microsoft.WindowsAzure.Storage.Table; // Namespace for Table storage types
using System.Configuration;
using Newtonsoft.Json;
using System.IO;
namespace UnioAzureFunctions.Controllers
{
[RoutePrefix("api/telemetria")]
public class TelemetriaController : ApiController
{
[AllowAnonymous]
[HttpPost]
public async Task<HttpResponseMessage> Run(HttpRequestMessage request, TraceWriter log)
{
try
{
// parse query parameter
string name = request.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
string data = await request.Content.ReadAsAsync<string>();
var telemetria = JsonConvert.DeserializeObject<Telemetria>(data);
// Set name to query string or body data
// name = name ?? data?.name;
//TODO: Usar CloudCOnfigurationManager ou ConfigurationManager normal *
//// Parse the connection string and return a reference to the storage account.
//CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
// CloudConfigurationManager.GetSetting("StorageConnectionString"));
#region conexão com cloud table storage
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.AppSettings["StorageConnectionString"]);
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
// Retrieve a reference to the table.
CloudTable table = tableClient.GetTableReference("telemetria");
// Create the table if it doesn't exist.
table.CreateIfNotExists();
// Create the TableOperation object that inserts the customer entity.
TableOperation insertOperation = TableOperation.Insert(telemetria);
// Execute the insert operation.
table.Execute(insertOperation);
#endregion
return name == null
? request.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: request.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
catch (Exception ex)
{
throw ex;
}
}
}
public class Telemetria : TableEntity
{
public Telemetria(string carteira, string crm, string app,
long idTenant, long idUsuario, string operadora, string action, DateTime dataHora)
{
Carteira = carteira;
Crm = crm;
App = app;
IdTenant = IdTenant;
IdUsuario = IdUsuario;
Operadora = operadora;
Action = action;
DataHora = dataHora;
}
public Telemetria()//não retirar - necessário para funcionar o tableEntity
{
}
public string Carteira { get; set; }
public string Crm { get; set; }
public string App { get; set; }
public long IdTenant { get; set; }
public long IdUsuario { get; set; }
public string Operadora { get; set; }
public string Action { get; set; }
public DateTime DataHora { get; set; }
}
}
正如您所看到的,我的目标是将此POST和Desserialize读取到名为“Telemetria”的实体,并将其保存到我的azure存储帐户的表中。
我不知道为什么我不能阅读这一行中的内容:
string data = await request.Content.ReadAsAsync<string>();
数据始终为空,我不知道为什么。
似乎因为这是一个天蓝色的函数,我无法模拟webapi的行为。
欢迎任何帮助。提前谢谢。
答案 0 :(得分:0)
出于开发原因,我决定从Azure门户中复制确切的样本
使用Visual Studio 2017 Update 3(v15.3),我们可以创建预编译的 Azure azure函数,我们可以在本地调试它,更多详细信息我们可以参考这个{{3 }}
正如AndrésNava - .NET提到的,请尝试使用string data = await request.Content.ReadAsStringAsync()
,它应该有用。