當談到MVVM和Web相關技術時,我是一個新手。我們目前正在使用Visual Studios 2013中的最新MVVMLight框架開發MVVM客戶端應用程序。該應用程序使用Microsoft的Webapi(最新版本)通過HTTP請求將數據傳入我們的模型。我們的代碼會執行HTTP請求,並且我們已經確認服務器正在獲取請求,收集請求的數據並將其作爲JSON返回。然而,客戶從來沒有看到迴應,它只是繼續等待迴應。它幾乎就像我們正在看到一個似乎與線程一樣的問題。請求是在一個線程中進行的,但是正在收到另一個線程的響應。下面是我們的代碼(其中HTTP請求時):使用Webapi發出HTTP請求的MVVM Light
公共類的DataService:IDataService {
#region Fields
private HttpClient _client;
private LfActivityDataReturnObject _activityReturnObj;
private AdroServices _adroServices;
private bool _selectedProcssingOptionPosted = false;
private bool _activityDataSuccessfullyRetrieved = false;
private decimal _incomingLFEntryId;
#endregion
#region Constructor
public DataService()
{
try
{
//Get command line arguments
string[] arguments = Environment.GetCommandLineArgs();
for (int i = 1; i < arguments.Length; i++)
{
switch (i)
{
case 1:
{
if (!Decimal.TryParse(arguments[i], out _incomingLFEntryId))
{
_incomingLFEntryId = -1;
}
break;
}
}
}
if (_incomingLFEntryId <= 0)
{
throw new ArgumentException(String.Format("Invalid Activity Shortcut Entry ID: {0}", _incomingLFEntryId));
}
}
catch (Exception e)
{
throw e;
}
}
#endregion
#region Methods
public void GetGeneralInformationModel(Action<GeneralInformationModel, Exception> callback)
{
Exception locException = null;
GeneralInformationModel locGeneralInformationModel = null;
if (_adroServices == null)
{
try
{
//Start the HTTP request
GetActivityDataAsync().Wait();
}
catch (Exception e)
{
locException = e;
}
// change? should be for http success but adro failure
if (_activityDataSuccessfullyRetrieved)
{
_adroServices = new AdroServices(_activityReturnObj);
locGeneralInformationModel = new GeneralInformationModel(_adroServices);
}
else
{
Exception e2 = new Exception("Error retrieving activity data in DataService");
locException = e2;
}
}
else
{
locGeneralInformationModel = new GeneralInformationModel(_adroServices);
}
var item = locGeneralInformationModel;
callback(item, locException);
}
//Get data from the repository via the service layer.
private async Task GetActivityDataAsync()
{
try
{
using (this._client = new HttpClient())
{
_client.BaseAddress = new Uri("http://localhost:52512//");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Line below is where the app just runs getting no response
HttpResponseMessage caseDataResponse = await _client.GetAsync(string.Format("api/LfUrs/{0}", _incomingLFEntryId));
if (caseDataResponse.IsSuccessStatusCode)
{
_activityReturnObj = await caseDataResponse.Content.ReadAsAsync<LfActivityDataReturnObject>();
if (_activityReturnObj.ReturnCode == 0)
{
_activityDataSuccessfullyRetrieved = true;
}
else
{
_activityDataSuccessfullyRetrieved = false;
}
}
else
{
_activityDataSuccessfullyRetrieved = false;
}
}
}
catch (Exception ex)
{
_activityDataSuccessfullyRetrieved = false;
throw ex;
}
}
我們已經使用招,以獲得更多的信息,嘗試,但菲德勒似乎並不能夠揭示任何細節。另外我應該提到,我們模擬本地主機上的服務器,正如我上面所述,我們已經確認服務器獲取請求並返回數據。我的同事和我開始認爲這與MVVM Light Framework和IOC或可能的線程有關。當我們在不使用框架的MVVM解決方案中使用相同的代碼時,它可以工作。任何幫助將真誠感謝。謝謝...邁克
此外,本地主機使用IIS Express來接收HTTP請求。 –