2015-05-14 95 views
0

我想在WCF服务中获得完整的异常详细信息,但是,我没有收到发生异常的行号。WCF故障异常行号

try 
{ 
    ... 
} 
catch (Exception e) 
{ 
    throw new FaultException(e.ToString()); 
} 

我试过各种方式,如返回e.StackTrace等没有运气。请帮助我如何获得发生异常的行号。

回答

0

用途:

catch (Exception e) 
    { 
     // Get stack trace for the exception with source file information 
     var st = new StackTrace(e, true); 
     // Get the top stack frame 
     var frame = st.GetFrame(0); 
     // Get the line number from the stack frame 
     var line = frame.GetFileLineNumber(); 
} 

它会给你你需要的信息。

+0

我已经试过了。它没有给出行号。它只给出像'试图除以零的信息。' – SiD

+0

好吧,它已被编辑。 –

0

此代码将使堆栈跟踪行号

try 
{ 
    ///... 
} 
catch (Exception e) 
{ 
    throw new FaultException<Exception>(ex, ex.Message); 
} 
+0

我试过这个,但我得到这个'现有连接被远程主机强行关闭' – SiD

0

我会说你必须删除整个trycatch块:

一个更好的方法将只是注释掉它:

//try 
//{ 
     Code.DoSomething(); //of course this code line is only a example and not working 

     /*the code inside the try block should now stop on the line, where the error appears*/ 
     // might look like this: http://www.homeandlearn.co.uk/csharp/images/conditional_logic/Error_Parse.gif 
//} 
//catch (Exception e) 
//{ 
     //throw new FaultException(e.ToString()); 
//}  

你应该得到的东西这样的:我在咕发现(只图像gle根据这个)

error window

+1

这没有任何意义。它是一个WCF服务。 – SiD

+0

对不起,我完全在winforms中...我的头脑在哪里,我可能会重读这个... – cramopy