我一直在尝试构建一个Windows控制台应用程序,将其作为Web服务运行,以同步一些数据。我已经建立了一个用于同步的项目,当我通过我的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).
我的Web服务控制器如下所示:
public IEnumerable<QaqcRow> Index(string username, string lastUpdated)
{
return GetFilteredList(username, lastUpdated).OrderBy(x => x.GetValue("FormId"));
}
我可以手动通过链接访问webservice并获取数据,但是当我使用httpclient时它就会失败。
Gettasks().Wait()
。在main
方法中这样做是可以的。 - Yacoub Massad