PDA

View Full Version : Exceptions in proxied objects


valdis99
10-13-2005, 05:24 PM
I have a question regarding exceptions and proxied objects.

I have defined a proxied object which can throw a (subclass of ) System.Exception. This proxy is injected via the IoC container into my "service" object. If the proxied object throws the exception, a TargetInvocationException is returned to my service class, with the InnerException set as the thrown exception.

However, if I add a IThrowsAdvice which simply logs the custom exception via log4net, my "service" class receives the real thrown exception, typed correctly.

Is this expected behavior or a bug? I wouldn't expect that I'd have to catch a TargetInvocationException in my "service" class.

Aleks Seovic
10-13-2005, 10:03 PM
Hmm, sounds like a bug...

Do you have a code example you can share that demonstrates the problem? If you can email it to me I'll take a look at it.

Regards,

Aleks

Rick Evans
10-14-2005, 12:41 AM
Hi Aleks

Do you have a code example you can share that demonstrates the problem?

[Test]
public void ProxiedObjectUnwrapsTargetInvocationException()
{
ProxyFactoryObject fac = new ProxyFactoryObject();
fac.AddInterface(typeof(ICommand));
fac.AddAdvice(new DebugAdvice());
fac.Target = new BadCommand();

ICommand cmd = (ICommand) fac.GetObject();
try
{
cmd.Execute();
}
catch (NotImplementedException)
{
// this is good, we want this exception to bubble up...
}
catch (TargetInvocationException)
{
Assert.Fail("Must have unwrapped this.");
}
}

public interface ICommand
{
void Execute();
}

public sealed class BadCommand : ICommand
{
public void Execute()
{
throw new NotImplementedException();
}
}

You can find this method in the ProxyObjectfactoryTests class, line ~470 (currently [Ignore]d though). I'll address this (http://opensource2.atlassian.com/projects/spring/browse/SPRNET-201) tomorrow night unless you get to it first ;)

Ciao
Rick

Aleks Seovic
10-14-2005, 03:31 AM
Yeah, that test shouldn't be ignored...

I have a lot of other stuff to work on, so I would appreciate if you could fix this.

Thanks,

Aleks

Rick Evans
10-17-2005, 04:44 PM
Hi

Fixed (http://opensource2.atlassian.com/projects/spring/browse/SPRNET-201), and thanks for raising this. The only way you are now going to get a TargetInvocationException thrown up to your service objects is if your advice / proxied object itself actually throws a TargetInvocationException ;)

The fix will be in the next (interim) release (1.0.1) that is due out in a few weeks time.

Cheers
Rick