2

I want to know, is there the possibility to ask Unity "do not wrap any user exceptions at resolve time"?

Really, why Unity do wrappings to ResolutionFailedException? It is changing "construction service contract", IMHO the fact of objects initialization using unity should be transparent, that means if client waiting for example for IOException from "new" operator it should get it unwrapped also even object is created using Unity.

Do other IoC containers behave the same?

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142

4 Answers4

2

Because your constructors should contain no logic.

I know that's not a completely satisfying answer, but I'm not aware that you can switch off this behavior in Unity - I also do agree that it's a strange design decision...

However, if you keep your constructors simple, you will not have that problem.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • Constructors should contain no logic, but there are many situations when they do. :/ – Roman Pokrovskij Aug 23 '11 at 18:17
  • Let say we have the method that should be called only per whole instance life, this function/test must be done before using other methods (like custom authentication stuff). Very naturally to place this test to the constructor. There other variants (first produce proxy, and do authentication together with "GetIntance"; or test isAuthenticated flag on each call, if false then do authentication and set to true) but I can't share the opinion that those algorithms are better. Thank you to your article. – Roman Pokrovskij Aug 23 '11 at 20:24
  • @Roman Pokrovksij: It's natural to do that, but that doesn't make it right. That responsibility should be delegated elsewhere, not in the object constructor. – jason Aug 23 '11 at 20:53
  • 1
    Agreed. Security is a Cross-Cutting Concern and much better addressed with a Decorator (or Interception). Here's an example that talks about caching, but authentication and authorization can be handled the same way: http://blog.ploeh.dk/2010/04/07/DependencyInjectionIsLooseCoupling.aspx – Mark Seemann Aug 24 '11 at 04:55
  • I don't understand how this is an answer to the question. A constructor with a guard inside it, throws an exception. Unity swallows that exception and causes the object to become null, which is caught by the next guard. So at the end, it looks like the object we were constructing was somehow null, instead of one of its dependencies. – hyankov Mar 13 '19 at 23:00
1

Check out ResolutionFailedException.InnerException.

It is changing "construction service contract"

What contract?

IMHO the fact of objects initialization using unity should be transparent

A point of IoC containers is to make object construction less of a focus so developers can focus on adding business value.

that means if client waiting for example for IOException from "new" operator it should get it unwrapped also even object is created using Unity.

Whoa, what client is using the container that expects IOException? I think you might be misusing your IoC container.

jason
  • 236,483
  • 35
  • 423
  • 525
  • I want to keep using Unity transparent for my client. – Roman Pokrovskij Aug 23 '11 at 18:11
  • "A point of IoC containers.. business value.." I'm asking about concrete Unity feature. Sorry, I hate all this "business value" brainwashing. – Roman Pokrovskij Aug 23 '11 at 18:22
  • @Roman Pokrovskij: No, there's no business value brainwashing here. You made a statement; I don't agree with it. Your statement is that object construction using Unity should be transparent. The whole point of IoC containers is take care of object construction for you (among other things). The reason we use IoC containers to take care of object construction for us is because it's a boring problem that spending time solving doesn't add value to our stakeholders. – jason Aug 23 '11 at 20:16
  • Ok I get it. You convert number of characters in code to Joules and than to $$. – Roman Pokrovskij Aug 23 '11 at 20:48
1

The reason Unity wraps exceptions is all about the contract - the contract of the Resolve method.

What should an application catch when Resolve throws? Suppose you did resolve a class that you know throws IOException. So you put a catch for that exception around the resolve call.

Then the implementation changes. Or just the configuration. Now the services throw something else.

Now you've got a configuration change that will require a code change. Not good.

A second reason that with the wrapped exception the container has a place to put diagnostic information about where in the resolve process the failure occurred.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
0

I had a similar need and found a solution here: catch all unhandled exceptions in ASP.NET Web Api

Here's what I learned when reviewing answers from the linked article:

  • If you only want to catch the exceptions and log them, then add an IExceptionLogger.
  • If you want to catch the exception and manipulate the response, then replace IExceptionHandler.
  • Neither solution captures ALL exceptions. I'm still using Application_Error to capture exceptions that aren't caught within my ApiController methods.
KrimblKrum
  • 183
  • 3
  • 11