2010-08-20 76 views
2

我有一个System.Web.Services.WebService包含几个WebMethods。如果这些WebMethods中的任何一个引发异常,我想记录传递给该WebMethod的值。我想以一种通用的方式处理这个问题,以便我可以使用相同的方法,而不管参数的数量或类型如何。我以为我会记录原始的JSON,但我无法确定如何访问它。我搜索了整个Context.Request对象(包括InputStream属性),但没有找到它。从asp.net WebMethod内部访问原始JSON

这里是我想怎样做:

[WebMethod(EnableSession = true)] 
public IEnumerable MyWebMethod(int a, string b) 
{ 
    try 
    { 
     //do something 
    } 
    catch (Exception e) 
    { 
     LogException(e, this.Context); 
     throw; 
    } 
} 

//All WebMethods should be able to call LogExceoption regardless of param type/count 
protected void LogException(Exception ex, HttpContext context) 
{ 
    string parameters = context.Request. //?? i don't know how to get to the JSON 
    //write exception detail to log 
} 

我使用C#和.NET Framework 3.5的

回答

3

下面是如何访问原始JSON:

protected void LogException(Exception ex, HttpContext context) 
{ 
    context.Request.InputStream.Position = 0; 
    string rawJson = null; 
    using (StreamReader reader = new StreamReader(context.Request.InputStream)) 
    { 
     rawJson = reader.ReadToEnd(); 
    } 
    //write exception detail to log 
} 
+0

疯狂,这是这么简单。 – 2010-08-22 04:42:24