2013-05-04 40 views
0

我想创建一个WPF应用程序连接到我的Web API服务器的正确方法是一些建议。但我是WPF的新手。更糟糕的是,即使在.net。需要要写自己的HttpClient接口

其实,我已经把事情的工作,遵循这一http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-wpf-application

更多的搜索后,我发现我完全在一个错误的方式(WinForm的方式)的工作。而我似乎应该使用MVVM风格最群落说。所以我决定重写我的所有代码。

我用棱镜来创建一个模块,并结合名和密码的ViewModel类。现在我可以从文本框中获取名称和密码。

首先,我没能创造每一个视图模型使用HttpClient的实例。下面的问题是链接

Hot to register already constructed instance in unity?

所以我DEICIDE写这样。首先创建界面。

public interface IData 
{ 
    IEnumerable<device> getDevicesByUser(user user); 
    IEnumerable<user> getUsers(); 
    currentUser getCurrentUserInfo(); 
} 

和实现接口

public class HttpService : IData 
{ 
    HttpClient client = new HttpClient(); 
    public HttpService() 
    { 
     client.BaseAddress = new Uri("https://localhost:3721"); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    } 

    public IEnumerable<device> getDevicesByUser(user user) 
    { 
     throw new NotImplementedException(); 
    } 

    public IEnumerable<user> getUsers() 
    { 
     throw new NotImplementedException(); 
    } 

    public currentUser getCurrentUserInfo() 
    { 
     throw new NotImplementedException(); 
    } 
} 

后来,我可以在统一注册实例,可以使用它在每一个ViewModel.But的情况是,我不能再往前走。我使用两种方法,但都不可行。我需要关于正确方法的建议。

最前一页的方式。我为每个接口方法编写异步方法。

public currentUser getCurrentUserInfo() 
    { 
     return getCurrentUserInfoFromServer(); 
    } 

private async void getCurrentUserInfoFromServer() 
    { 
     try 
     { 
      var response = await client.GetAsync("api/user"); 
      response.EnsureSuccessStatusCode(); 
      var currentuser = await response.Content.ReadAsAsync<currentUser>(); 
     } 
     catch (Newtonsoft.Json.JsonException jEx) 
     { 
      MessageBox.Show(jEx.Message); 
     } 
     catch (HttpRequestException ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 

     } 
    } 

但似乎异步不能返回除void和任务以外的类型。这似乎很奇怪。 所以我尝试将所有请求写入一个方法。

private async void getData(string requestUri) 
    { 
     try 
     { 
      var response = await client.GetAsync("api/user"+requestUri); 
      response.EnsureSuccessStatusCode(); 
      var ? = await response.Content.ReadAsAsync<?>(); 
     } 
     catch (Newtonsoft.Json.JsonException jEx) 
     { 
      MessageBox.Show(jEx.Message); 
     } 
     catch (HttpRequestException ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 

     } 
    } 

这个代码仍然有像第一个相同的问题。无法返回void和任务以外的类型,另一个问题是如何获取数据动态?

回答

0

异步方法应该总是返回TaskTask<T>,因为那是什么是异步的,意思是:一种方法,几乎​​立即返回,但它实际上并没有尚未完成。所以,要得到它的结果,你await返回Task。所以,你的方法会是这个样子:

private async Task<CurrentUser> GetCurrentUserInfoFromServerAsync() 
{ 
    try 
    { 
     var response = await client.GetAsync("api/user"); 
     response.EnsureSuccessStatusCode(); 
     var currentuser = await response.Content.ReadAsAsync<currentUser>(); 
     return currentUser; // sets the result of the returned Task 
    } 
    catch (Newtonsoft.Json.JsonException jEx) 
    { 
     MessageBox.Show(jEx.Message); 
    } 
    catch (HttpRequestException ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

这意味着调用GetCurrentUserInfoFromServerAsync()也需要async的方法,而方法是,等,一路到顶部。

而且,这是一个很好的做法,分开您的问题:你的HttpService不应该显示MessabeBox ES,而是只让例外传播出来的方法。你应该把UI处理到更高层。