Possible to disable automatic concrete instance resolution?

Sep 19, 2013 at 1:50 AM
Hi there, I'm looking for a way to throw up an error when a request is made to get an unregistered concrete type from the container. Is this possible?

Sep 19, 2013 at 2:23 AM
Edited Sep 19, 2013 at 6:09 AM
Figured it out, I'm handling it via ResolveUnregisteredType... although given I still want the default handling behaviour for enumerables it means I need to do the same check that Container.BuildInstanceProducerForCollection does before I check if its a concrete type.
Sep 19, 2013 at 6:50 AM
You are right, you need the ResolveUnregisteredType event. A few pointers:
  • The ResolveUnregisteredType event is used under the covers by the framework and its extension methods (such as RegisterOpenGeneric), so to prevent interfering with other subscribers, register this event as last thing in the container.
  • Prevent throwing an exception in case another subscriber already handled that event by checking the e.Handled flag.
  • Since you only want to handle concrete unregistered types, you can check e.UnregisteredServiceType.IsAbstract is false.
The following will do the trick:
container.ResolveUnregisteredType += (s, e) =>
    if (!e.Handled && !e.UnregisteredServiceType.IsAbstract)
        throw new Exception(e.UnregisteredServiceType.Name +
            " is a concrete unregistered type.");
Also note that the container's Diagnostic Services warn about unregistered types. Instead of throwing an exception you can use the diagnostic services to analyze the container (although throwing an exception will allow you to find a possible bug much faster).
Marked as answer by dot_NET_Junkie on 3/2/2014 at 10:54 AM
Sep 23, 2013 at 2:18 AM
Great, thanks for the help :)