spring.net aop advice

庚武讲堂 2010-08-13 02:25:21
请问,为一方法增加 IThrowsAdvice,在之方法里抛出异常,在vs中就抛出了,如何不让它抛出呢,而是在程序中打印出异常信息。

spring.net aop中的example是没有在vs中抛出的。差不多一样的代码,是不是vs哪里有设置?

public class ServiceCommand : ICommand
{
#region ICommand Members

public bool IsUndoCapable
{
get { return true; }
}

public void Execute()
{
Console.Out.WriteLine("---原生方法执行 ServiceCommand implementation : Execute()... ---");
throw new Exception("my exception");
}

public void UnExecute()
{
Console.Out.WriteLine("--- ServiceCommand implementation : UnExecute()... ---");

// Uncomment to see IThrowAdvice implementation in action
//throw new Exception("The method or operation is not supported.");
}

#endregion
}

/****************************************************/

static void Main(string[] args)
{
try
{
// Create AOP proxy programmatically.
ProxyFactory factory = new ProxyFactory(new ServiceCommand());
//factory.AddAdvice(new ConsoleLoggingBeforeAdvice());
//factory.AddAdvice(new ConsoleLoggingAfterAdvice());

//factory.AddAdvice(new ConsoleLoggingAroundAdvice());

factory.AddAdvice(new ConsoleLoggingThrowsAdvice());
ICommand command = (ICommand)factory.GetProxy();

command.Execute(); // //在此处就提示异常了,如何禁用?这里不是有捕获,可是还是出来了。

//if (command.IsUndoCapable)
//{
// command.UnExecute();
//}
}
catch (Exception ex)
{
Console.Out.WriteLine();
Console.Out.WriteLine(ex);
}
finally
{
Console.Out.WriteLine();
Console.WriteLine("done");
Console.Out.WriteLine("--- hit <return> to quit ---");
Console.ReadLine();
}
}








...全文
72 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ld1201 2010-08-17
  • 打赏
  • 举报
回复
使用环绕通知IMethodInterceptor
在Invoke中实现以下代码

try
{
//执行
object obj = invocation.Proceed();
return obj;
}
catch (Exception ex)
{
System.Console.WriteLine(ex.ToString()); //打印异常
logger.Error(ex); //log4net组件写入日志
return null;
}
finally
{
//结束
}

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧