从控制台创建 WebAPI 帖子以将$type包含在 json 数据中



我正在创建对象并将它们发布到webapi。 基本上我只是无法让的东西序列化以将$type信息包含在 json 中。 以下是我尝试编写的代码。 之后是我期望的json。

       var cds = new List<CreditDefaultSwaps>()
        {
            new CreditDefaultSwaps() { ModelNumber = "SP8A1ETA", BrokerSpread = 0},
            new CreditDefaultSwaps() { ModelNumber = "SP3A0TU1", BrokerSpread = 0},
            new CreditDefaultSwaps() { ModelNumber = "SP4A102V", BrokerSpread = 0}
        };
        var client = new HttpClient {BaseAddress = new Uri("http://localhost/BloombergWebAPI/api/")};
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        // set up request object
        var oContract = new WebApiDataServiceRequest
        {
            RequestType = ReferenceDataRequestServiceTypes.ReferenceDataRequest,
            SwapType = BloombergWebAPIMarshal.SwapType.CDS,
            SecurityList = cds
        };
        Tried something like this and the var content was formatted as I would expect
        however I couldn't post the data using postasjsonasync
        //var content = JsonConvert.SerializeObject(oContract, Formatting.Indented,
        //    new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });
           Console.ReadLine();
        var response = client.PostAsJsonAsync("bloombergapi/processbloombergrequest", oContract).Result;

以下是我尝试发布的 json。 我在上面的代码中缺少什么,我确定这是愚蠢的。

   {
      "$type": "BloombergWebAPIMarshal.WebApiDataServiceRequest, BloombergWebAPIMarshal",
      "RequestType": 3,
      "SwapType": 1,
      "SecurityList": [
        {
          "$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
          "ModelNumber": "SP8A1ETA",
          "BrokerSpread": 0
        },
        {
          "$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
          "ModelNumber": "SP3A0TU1",
          "BrokerSpread": 0
        },
        {
          "$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
          "ModelNumber": "SP4A102V",
          "BrokerSpread": 0
        }
      ]
    }

创建了另一个重载 使用此调用来生成正确的请求:

var response = client.PostAsJsonAsync("processbloombergrequest", oContract, TypeNameHandling.Objects).Result

这是新的重载

public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUri, T value, TypeNameHandling typeNameHandling)
{
    return client.PostAsJsonAsync<T>(requestUri, value, CancellationToken.None, typeNameHandling);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUri, T value, CancellationToken cancellationToken, TypeNameHandling typeNameHandling)
{
    var formatter = new JsonMediaTypeFormatter
    {
        SerializerSettings = new JsonSerializerSettings()
        {
            TypeNameHandling = typeNameHandling
        }
    };
    return client.PostAsync<T>(requestUri, value, formatter, cancellationToken);
}

最新更新