【发布时间】:2016-04-20 11:53:00
【问题描述】:
我一直在尝试构建一个 Windows ConsoleApp 以作为 WebService 运行来来回同步一些数据我已经构建了一个用于同步的项目,当我通过我的 wpf 项目运行它时,它似乎可以工作,但事实并非如此。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using QAQC_DataCommon.Models;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
Gettasks();
}
public static async void Gettasks()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost/QAQC_SyncWebService/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
var response = await client.GetAsync("Tasks/?username=XXXXXX&LastUpdated=1/1/15");
if (response.IsSuccessStatusCode)
{
List<QaqcRow> ls = await response.Content.ReadAsAsync<List<QaqcRow>>();
foreach (QaqcRow qaqcRow in ls)
{
Debug.WriteLine(qaqcRow.GetValue("BusinessUnit"));
}
}
}
catch (Exception)
{
throw;
}
}
}
}
}
刚退出时,它会到达 Var response = Await 行。没有异常或任何警告,如果我正在调试它就会停止。
我的输出是:
The thread 0x1414 has exited with code 259 (0x103).
The thread 0x16e4 has exited with code 259 (0x103).
The program '[9656] TestApp.vshost.exe' has exited with code 0 (0x0).
我的网络服务中的控制器如下:
public IEnumerable<QaqcRow> Index(string username, string lastUpdated)
{
return GetFilteredList(username, lastUpdated).OrderBy(x => x.GetValue("FormId"));
}
我可以通过链接手动转到网络服务并获取数据,但是当我使用 httpclient 时它就死了。
【问题讨论】:
标签: c# web-services asp.net-web-api httpclient