我想采用 JSON,将其放入 StringContent 并将其传递给我的 API。从那里,我想将 JSON 反序列化回一个类并从那里使用它。目前,我在尝试反序列化时遇到错误,据我所知,到达 API 时 logInfo 为空。当我尝试写出参数时,它在事件日志中为空。请参阅下面的消费者和 API 代码。
public void Log(LogInfo logInfo)
{
using (var client = CreateHttpClient())
{
var jsonData = JsonConvert.SerializeObject(logInfo);
HttpContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync("Logger/Log", content).Result;
if (!response.IsSuccessStatusCode)
{
throw new InvalidOperationException($"{response} was not successful.");
}
}
}
[HttpPost]
public HttpResponseMessage Log([FromBody] string logInfo)
{
//var logData = JsonConvert.DeserializeObject<LogInfo>(logInfo);
EventLog.WriteEntry("TestApp", logInfo);
EventLog.WriteEntry("TestApp", Request.Content.ReadAsStringAsync().Result);
//Log(logData);
return Request.CreateResponse(HttpStatusCode.OK);
}