如何从.NET控制台使用Web服务

时间:2016-12-12 13:42:56

标签: c# database web-services asp.net-web-api mapping

我成功地从数据库表中读取了我的记录,并且我将信息放在类型为ClaimComment的List中(用于映射的类),因为我需要将信息放在新的数据库表中。

using (var dbContext = new OldDbContextEntities())
{
    var claimComments = dbContext.R_ClaimHistory.ToList();
    var newDbContextClaimCommentsToSend = new List<ClaimComment>();
    foreach (var claimComment in claimComments)
    {
        var claimCommentToMigrate = new ClaimComment()
        {
            ClaimId = (int)claimComment.IdClaim,
            Comment = claimComment.HistClaimDescription,
            DateCreated = claimComment.DateCreated,
            UserCreated = (int)claimComment.IdUserCreated
        };
        newDbContextClaimCommentsToSend.Add(claimCommentToMigrate);
    }
    // continues below...

我需要使用Web Api将信息放入database2表中。 我是这样开始的:

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:54957/api/EntityMigration");
        var response = client.PostAsync("api/claimComment", newDbContextClaimCommentsToSend);
    }

但我收到错误:

  

错误CS1503参数2:无法转换   &#39; System.Collections.Generic.List&#39;   到&#39; System.Net.Http.HttpContent&#39;

1 个答案:

答案 0 :(得分:0)

HttpClient documentation非常清楚第二个参数应该是一个HttpContent实例...考虑看一下ObjectContent类型。