![](/img/trans.png)
[英]Xamarin form ios get exception when calling web service in iOS
[英]How to get exception information when calling JSON web service
在.NET 3.5中,我有以下代码:
[WebService(Namespace = "http://kitchenpc.com/schemas/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class KitchenPC : System.Web.Services.WebService
{
[WebMethod]
public LogonResult Logon(string username, string password)
{
//If username and password are not valid...
throw new InvalidUsernameOrPasswordException();
}
}
当我调用它时,如果我传入了无效的用户名和密码,则会抛出InvalidUsernameOrPasswordException
,我可以通过查看error.get_exceptionType()
来捕获Javascript中的异常。 这是因为Web服务将在JSON中序列化异常信息。
但是,一旦我升级到.NET 4.5,这就破了。 现在,当我传入无效的用户名和密码时,我收到HTTP响应:
HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
jsonerror: true
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 24 Nov 2012 22:42:33 GMT
Content-Length: 91
{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
基本上,异常类型丢失并替换为通用错误消息。
导致此行为发生变化的原因是什么,还有一种方法可以在JSON中返回异常信息吗? 几乎我的整个API都依赖于这种行为。
异常类型不可序列化。 请改用FaultException。
throw new FaultException(..);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.