无法从Azure函数读取POST内容

问题描述:

我正尝试从azure函数读取POST内容。对于发展的原因,我决定将复制Azure的门户网站的确切样品和我的代码如下:无法从Azure函数读取POST内容

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; } 
    } 
} 

正如你所看到的,我的目标是要阅读这篇文章,Desserialize我的实体,称为“Telemetria”并保存这到一个表格到我的天蓝色的存储帐户。 我不知道为什么我不能读这行内容: string data = await request.Content.ReadAsAsync<string>();数据总是空的,我只是不知道为什么。 看来,因为这是一个天蓝色的函数,我不能模拟webapi的行为。

任何帮助,欢迎。提前致谢。

+0

由于您将内容反序列化为'string',为什么不直接使用Content的'ReadAsStringAsync()'方法呢? 'string data = await request.Content.ReadAsStringAsync()' –

对于发展的原因,我决定确切的样品从Azure的门户网站复制

随着Visual Studio 2017更新3(v15.3),我们可以创建预编译天青天蓝功能,我们可以对它进行调试本地更详细的信息,我们可以参考这个blog

由于安德烈斯纳瓦 - NET提到,请尝试使用string data = await request.Content.ReadAsStringAsync(),它应该工作。