Exception - Cannot swallow exceptions for methods with non-null return type.

Apr 3, 2013 at 12:07 PM
Edited Apr 4, 2013 at 6:02 AM
We have developed a sample project to handle all exception by using entlib exception handler block and Unity.

When I try to invoke a method using unity container I am getting the following exception,
“Cannot swallow exceptions for methods with non-null return type.”
Below is the code ,
IUnityContainer unityContainer = new UnityContainer();
                unityContainer.LoadConfiguration("Container1");
                var service2 = unityContainer.Resolve<IABCServices>("Impl1");
                string dd= service2.DoSomething(55);
Its configuration file is given below,
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        
        <alias alias="string" type="System.String, mscorlib"/>
        <alias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
        <alias alias="transient" type="Microsoft.Practices.Unity.TransientLifetimeManager, Microsoft.Practices.Unity" />
        <alias alias="InterceptionConfigurationExtension" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
        <alias alias="EnterpriseLibraryCoreExtension" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />
        <alias alias="ExceptionCallHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
        <alias alias="IABCServices" type="ABC.GGG.Integration.Contracts.GGGServices.Interfaces.IABCServices, ABC.GGG.Integration.Contracts" />
        <alias alias="ABCServices" type="ABC.GGG.Integration.Implementation.GGGServices.ABCServices, ABC.GGG.Integration.Implementation" />
        <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
        
        <container name="Container1">
            <register type="string"></register>
            <extension type="EnterpriseLibraryCoreExtension"/>
            <extension type="Interception" />
            <register type="IABCServices">
                <interceptor isDefaultForType="true" type="TransparentProxyInterceptor"/>
            </register>
            <register name="Impl1" type="IABCServices" mapTo="ABCServices">
                <interceptionBehavior type="PolicyInjectionBehavior"/>
            </register>
            <interception>
                <policy name="MyPolicy">                    
                    <matchingRule name="exceptionHandling" type="TypeMatchingRule">
                        <constructor>
                            <param name="typeName">
                                <value value="ABC.GGG.Integration.Contracts.GGGServices.Interfaces.IABCServices"  />
                            </param>
                        </constructor>
                    </matchingRule>
                        <callHandler name="ExceptionHandlingCallHandler" type="ExceptionCallHandler">
                        <lifetime type="singleton" />
                        <constructor>
                            <param name="exceptionPolicy" dependencyName="Policy" />                            
                        </constructor>
                    </callHandler>
                    
                </policy>
            </interception>
            </container>
    </unity>
Could anyone please explain how to fix this issue?
Apr 4, 2013 at 7:22 AM
Edited Apr 4, 2013 at 7:22 AM
The documentation for ExceptionCallHandler Class states:

If the exception policy is configured to swallow exceptions, do not configure this call handler on a method with a non-void return value, as the handler doesn't know what value to return if the exception is swallowed.

This is a limitation/design of the current ExceptionCallHandler. You could create a new call handler based on the current ExceptionCallHandler source and implement the behavior for the value that you wish to return when swallowing an exception.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Apr 5, 2013 at 3:13 PM
Randy, Thanks for your information.