Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Azure移动应用后端捕获异常?_Azure_Xamarin_Xamarin.forms_Azure Mobile Services - Fatal编程技术网

如何从Azure移动应用后端捕获异常?

如何从Azure移动应用后端捕获异常?,azure,xamarin,xamarin.forms,azure-mobile-services,Azure,Xamarin,Xamarin.forms,Azure Mobile Services,在我的Azure移动应用后端中,我对post方法中的数据进行了验证。在某些情况下,后端服务器会抛出异常,但在应用程序端,我无法捕获此异常。我该怎么做 这是我的方法 // POST tables/Paciente public async Task<IHttpActionResult> PostPaciente(Paciente novoPaciente) { //other things if (paciente != null) {

在我的Azure移动应用后端中,我对post方法中的数据进行了验证。在某些情况下,后端服务器会抛出异常,但在应用程序端,我无法捕获此异常。我该怎么做

这是我的方法

// POST tables/Paciente
public async Task<IHttpActionResult> PostPaciente(Paciente novoPaciente)
{   

    //other things

    if (paciente != null)
    {
        var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest)
        {
            Content = new StringContent("Já existe um paciente com esse token cadastrado.")
        };

        //throw new HttpResponseException(responseMessage);
        return InternalServerError(new Exception("Já existe um paciente com esse token cadastrado."));
    }
}
//POST表格/Paciente
公共异步任务PostPaciente(Paciente novoPaciente)
{   
//其他事情
如果(paciente!=null)
{
var responseMessage=新的HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content=新的StringContent(“Jáexiste um paciente com esse token cadastrado.”)
};
//抛出新的HttpResponseException(responseMessage);
返回InternalServerError(新异常(“Jáexiste um paciente com esse token cadastrado.”);
}
}

我尝试抛出HttpResponseException并返回InternalServerException,但没有一个有效。

您需要检查http调用响应的状态代码(应该有一个
IssuccessStatusCode
属性进行检查)。

我建议使用
EnsuccessStatusCode()
存在于
HttpResponseMessage
类中。如果
StatusCode
不是
OK
(或200级状态代码的某个变体),此方法将引发异常

下面是一个通用类,
BaseHttpClientServices
,我在所有项目中都使用它来发出RESTAPI请求。它遵循了使用HttpClient的所有最佳实践,如,和使用

以Xamarin.Forms格式发送Post请求 公共抽象类HttpClientServices:BaseHttpClientServices { const string apiUrl=“您的api url”; 公共静态无效后处理(新处理) { 尝试 { var响应=等待PostObjectToAPI(APIRL,novoPaciente); response.EnsureSuccessStatusCode(); } 捕获(例外e) { //处理异常 } } } 通用HttpClient实现
使用系统;
使用System.IO;
Net系统;
使用系统文本;
使用System.Net.Http;
使用System.Threading.Tasks;
使用System.Net.Http.Header;
使用System.Runtime.CompilerServices;
使用Newtonsoft.Json;
使用Xamarin.Forms;
名称空间
{
公共抽象类BaseHttpClientService
{
#区域常数场
静态只读惰性_serializerHolder=new Lazy();
静态只读惰性_clientHolder=新惰性(()=>CreateHttpClient(TimeSpan.FromSeconds(60));
#端区
#区域字段
静态int_网络指示器计数=0;
#端区
#区域属性
静态HttpClient客户端=>\u clientHolder.Value;
静态JsonSerializer序列化程序=>\u serializerHolder.Value;
#端区
#区域方法
受保护的静态异步任务GetObjectFromAPI(字符串apiUrl)
{
使用(var responseMessage=await GetObjectFromAPI(apiUrl)。ConfigureAwait(false))
返回等待反序列化响应(responseMessage)。配置等待(false);
}
受保护的静态异步任务GetObjectFromAPI(字符串apiUrl)
{
尝试
{
UpdateActivityIndicatorStatus(真);
返回await Client.GetAsync(apiUrl).ConfigureAwait(false);
}
捕获(例外e)
{
报告(e);
投掷;
}
最后
{
UpdateActivityIndicatorStatus(false);
}
}
受保护的静态异步任务PostObjectToAPI(字符串APIRL、TRequest requestData)
{
使用(var responseMessage=await PostObjectToAPI(apiUrl,requestData)。配置await(false))
返回等待反序列化响应(responseMessage)。配置等待(false);
}
受保护的静态任务PostObjectToAPI(字符串apiUrl,T requestData)=>SendAsync(HttpMethod.Post,apiUrl,requestData);
受保护的静态异步任务PutObjectToAPI(字符串APIRL、TRequest requestData)
{
使用(var responseMessage=await PutObjectToAPI(apiUrl,requestData)。配置await(false))
返回等待反序列化响应(responseMessage)。配置等待(false);
}
受保护的静态任务PutObjectToAPI(字符串apiUrl,T requestData)=>SendAsync(HttpMethod.Put,apiUrl,requestData);
受保护的静态异步任务PatchObjectToAPI(字符串APIRL、TRequest requestData)
{
使用(var responseMessage=await PatchObjectToAPI(apirl,requestData)。ConfigureAwait(false))
返回等待反序列化响应(responseMessage)。配置等待(false);
}
受保护的静态任务PatchObjectToAPI(字符串apiUrl,T requestData)=>SendAsync(新的HttpMethod(“补丁”),apiUrl,requestData);
受保护的静态异步任务DeleteObjectFromAPI(字符串apiUrl)
{
使用(var responseMessage=await DeleteObjectFromAPI(apiUrl)。ConfigureAwait(false))
返回等待反序列化响应(responseMessage)。配置等待(false);
}
受保护的静态任务DeleteObjectFromAPI(字符串apiUrl)=>SendAsync(HttpMethod.Delete,apiUrl);
静态HttpClient CreateHttpClient(TimeSpan超时)
{
HttpClient;
交换机(设备运行时平台)
{
case Device.iOS:
case设备。Android:
client=新的HttpClient();
打破
违约:
client=newhttpclient(newhttpclienthandler{AutomaticDecompression=DecompressionMethods.GZip});
打破
}
client.Timeout=超时;
client.DefaultRequestHeaders.AcceptEncoding.Add(新的StringWithQualityHeaderValue(“gzip”);
client.DefaultRequestHeaders.Accept.Add(新MediaTypeWith