我开发了一个带有xamarin表单的应用程序。当我让应用程序在后台运行很长时间(2-3小时)时,应用程序将进入一个无法进行任何休息呼叫的状态。它抛出一个未设置为对象实例的"对象引用"我尝试创建httpClient时出错。
这是代码,
var client = new HttpClient(new NativeMessageHandler());
这是执行我的一个服务调用
var response = await Session.Instance.LoadList<Entity>();
2.it触发此
public Task<GetListResponse<T>> LoadList<T>()
{
return LoadList<T>(null);
}
3.then,
async public Task<GetListResponse<T>> LoadList<T>(GetListRequest request)
{
var typeName = typeof(T).Name;
if (request == null)
{
request = new GetListRequest() { Index = 0 };
}
var response = await Request<GetListResponse<T>>(string.Format("{0}/list", typeName.ToLower()), Newtonsoft.Json.JsonConvert.SerializeObject(request));
return response;
}
最后这里是几个处理api调用的方法,
async public Task<T> Request<T>(string subUrl, string content) where T : ApiResponse
{
if (content == null)
{
System.Diagnostics.Debug.WriteLine("GET. " + subUrl);
return await Request<T>(subUrl);
}
else
{
System.Diagnostics.Debug.WriteLine("POST. {0}, Content: {1}", subUrl, content);
return await Request<T>(subUrl, new StringContent(content, UTF8Encoding.UTF8, "application/json"));
}
}
请求()实施,
async public Task<T> Request<T>(string subUrl, HttpContent content = null, int transientRetryCount = 5) where T : ApiResponse'
{
try
{
HttpResponseMessage response = null;
using (var client = CreateClient())
{
if (content == null)
response = await client.GetAsync(Session.Instance.m_url + string.Format("/api/{0}", subUrl));
else
response = await client.PostAsync(Session.Instance.m_url + string.Format("/api/{0}", subUrl), content);
}
var responseContent = string.Empty;
if (response.Content != null)
responseContent = await response.Content.ReadAsStringAsync();
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
System.Diagnostics.Debug.WriteLine("Response. {0}, Content: {1}", response.StatusCode, responseContent);
if ((response.StatusCode == System.Net.HttpStatusCode.BadGateway ||
response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable) && transientRetryCount > 0)
{
//retry...this appears to be a transient error
return await Request<T>(subUrl, content, transientRetryCount--);
}
return null;
}
我实际上并没有做任何想象。但无法找到问题所在:/
答案 0 :(得分:0)
你的问题与xamarin表格没什么关系。我的猜测是垃圾收集运行并删除了你引用的一个对象。但在不知道你的应用程序的情况下很难分辨。你在测试什么平台?如果是它的android,也许一项服务更适合你的任务。