【问题标题】:Capturing exceptions during request parsing在请求解析期间捕获异常
【发布时间】:2011-01-14 11:32:07
【问题描述】:

我有一个 ASP.NET 网络服务,请求中的一些字段被定义为枚举。输入空白或无效值时,响应返回为:

Parameter name: type ---> System.ArgumentException: Must specify valid information for parsing in the string.
   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

是否可以捕获这样的错误并返回基于 XML 的响应?

【问题讨论】:

    标签: web-services .net-2.0 asmx


    【解决方案1】:

    不,ASMX Web 服务无法做到这一点。

    当然,您可以使用 WCF 做到这一点。

    当然,如果您的客户端发送有效数据会更好。您可能想知道为什么它们不是。

    【讨论】:

      【解决方案2】:

      当然,它看起来像这样:

      try
      {
          Enum.Parse(Type enumType, String value, Boolean ignoreCase)
      }
      catch (ArgumentException e)
      {
         //Serialise exception information from 'e' into XML
         //(not shown here) and set it as the response
         Response.Write(xmlMessage);
         Response.End();
      }
      

      【讨论】:

      • 不,此异常由 Web 服务处理程序引发,而不是用户域代码。所以你不能把它包裹起来。
      • 不,如果您附加到 global.asax 中的错误处理程序,它不会触发。
      猜你喜欢
      • 2017-08-04
      • 2015-05-04
      • 2014-01-20
      • 1970-01-01
      • 2013-12-10
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多