本文介绍了使用统一IOC时,可以在拦截的方法中抑制异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想在我们的应用程序中删除多余的try catch块.

We would like to remove redundant try catch blocks in our application.

显然,统一拦截器可以实现一个通用处理程序,并节省大量重复代码.

Obviously an unity interceptor can implement a common handler and save lots of duplicate code.

但是我还没有找到一种在拦截的方法中抑制异常的方法.

But i havent found a way to suppress the exception in the intercepted method.

当前:

void InterceptedMethod
{
  try 
   { 
   }
  catch()
   {
    } 
}

目标:

Intended:

void InterceptedMethod
{
  //no try catch blocks

 }

例如(使用StreamReader sr = new StreamReader(某些无效路径))将在拦截的方法中引发异常,如果我删除现有的try catch块,则不会捕获该异常.

for example (using StreamReader sr= new StreamReader(some invalid path)) will throw an exception in the intercepted method, that will not be caught if i remove the existing try catch block.

(result.Exception!= null)之后的代码成功执行.

The code after (result.Exception ! = null) is executing successfully.

但是它目前仅在进入之前"服务.以及退出后"场景.

But it currently serves only "before enter" and "after exit" scenarios.

我仍然需要在拦截的方法中删除try catch块.我知道postharp或温莎城堡允许我们设置属性.

I still need to remove the try catch blocks in the intercepted method. I know postsharp or castle windsor allows us to set properties.

推荐答案

谢谢您在这里发布.

对于您的问题,您可以参考MSDN文章.它介绍了如何通过Unity进行拦截而不是尝试捕获.

For your question, you could refer to the MSDN article. It introduce how to interception via Unity not for try catch.

https://msdn. microsoft.com/en-us/library/dn178466%28v=pandp.30%29.aspx?f=255&MSPPError=-2147217396

最好的问候,

Wendy


这篇关于使用统一IOC时,可以在拦截的方法中抑制异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 04:46