在Xamarin Forms中,我想在应用启动时调用node.js服务。 在App.xaml.cs中我有这个:
protected override async void OnStart()
{
await CheckDeviceRegistered("1231234");
}
private async Task<bool> CheckDeviceRegistered(string id)
{
try
{
return await _deviceService.GetDevice(id);
}
catch (Exception e)
{
return false;
}
}
称之为:
public class DeviceRegisteredService : IDeviceRegisteredService
{
#region IDeviceRegistered
public async Task<bool> GetDevice(string deviceID)
{
HttpResponseMessage result;
string url = Resources.DeviceApi.Get + "?id=" + deviceID;
result = await App.HttpClientInstance().GetAsync(url);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
return true;
}
return false;
throw new InvalidOperationException();
}
#endregion
}
public interface IDeviceRegisteredService
{
Task<bool> GetDevice(string ID);
}
我可以在浏览器中或通过Postman调用该服务,但是当我调试时,我点击了
result = await App.HttpClientInstance().GetAsync(url);
public static HttpClient _httpClientInstance;
public static HttpClient HttpClientInstance()
{
if (_httpClientInstance == null)
{
_httpClientInstance = new HttpClient();
}
return _httpClientInstance;
}
没有任何反应,没有错误或崩溃,但代码似乎停止了。 我想我在这里错过了一步,有人能指出我需要什么吗?