Search code examples
jsonasp.net-mvcdotnet-httpclient

HttpClient.PostAsJsonAsync content empty


I'm trying to send a complex data type from one process to another using ASP.net MVC. For some reason the receiving end always receives blank (zero/default) data.

My sending side:

static void SendResult(ReportResultModel result)
{
    //result contains valid data at this point

    string portalRootPath = ConfigurationManager.AppSettings["webHost"];
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(portalRootPath);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage resp = client.PostAsJsonAsync("Reports/MetricEngineReport/MetricResultUpdate", result).Result;
    if (!resp.IsSuccessStatusCode) {
    //I've confirmed this isn't happening by putting a breakpoint in here.
    }
}

My receiving side, in a different class, running in a different process on my local machine:

public class MetricEngineReportController : Controller
{
    ...
    [HttpPost]
    public void MetricResultUpdate(ReportResultModel result)
    {
        //this does get called, but
        //all the guids in result are zero here :(
    }
    ...
}

My model is a bit complicated:

[Serializable]
public class ReportResultModel
{
    public ReportID reportID {get;set;}
    public List<MetricResultModel> Results { get; set; }
}

[Serializable]
public class MetricResultModel
{
    public Guid MetricGuid { get; set; }
    public int Value { get; set; }

    public MetricResultModel(MetricResultModel other)
    {
        MetricGuid = other.MetricGuid;
        Value = other.Value;
    }

    public MetricResultModel(Guid MetricGuid, int Value)
    {
        this.MetricGuid = MetricGuid;
        this.Value = Value;
    }

}

[Serializable]
public struct ReportID
{
    public Guid _topologyGuid;
    public Guid _matchGuid;
}

Any idea why the data's not arriving? Any help would be much appreciated...

P.S. For some reason I can't seem to catch the http POST message on fiddler, not sure why that is.


Solution

  • The problem was twofold:

    1. I needed to specify the type in my JSON post like this:

      HttpResponseMessage resp = client.PostAsJsonAsync<MetricResultModel>("Reports/MetricEngineReport/MetricResultUpdate", result.Results[0]).Result;
      
    2. The components of my model did not have default constructors, which is necessary for the JSON deserialization on the receiving end.