How do I use InterfaceInterceptor with configuration?

Dec 9, 2008 at 4:30 PM
Hi All,

First, thanks for anyone who help me!

I have an interface IAccountContract and an implementation of the interface AccountService. Now, I want to use InterfaceInterceptor to add ExceptionCallHandler for the interface. I use following configuration file to do this, but failed. How do I achieve this goal with configuration?

<configuration>

  <configSections>

    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

  </configSections>

  <exceptionHandling>

    <exceptionPolicies>

      <add name="UIExceptionPolicy">

        <exceptionTypes>

          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

            postHandlingAction="None" name="Exception">

            <exceptionHandlers>

              <add type="AOPLearning.MessageBoxHandler, AOPLearning, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

                name="Custom Handler" />

            </exceptionHandlers>

          </add>

        </exceptionTypes>

      </add>

    </exceptionPolicies>

  </exceptionHandling>

  <unity>

    <typeAliases>

      <typeAlias alias="string" type="System.String, mscorlib" />

      <typeAlias alias="Type" type="System.Type, mscorlib" />

      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

 

      <typeAlias alias="InterfaceInterceptor" type="Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptor, Microsoft.Practices.Unity.Interception" />

      <typeAlias alias="ExceptionCallHandler" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers" />

 

      <typeAlias alias="IAccountContract" type="AOPLearning.IAccountContract, AOPLearning" />

      <typeAlias alias="AccountService" type="AOPLearning.AccountService, AOPLearning" />

      <typeAlias alias="GetTypeConverter" type="AOPLearning.GetTypeConverter, AOPLearning" />

      <typeAlias alias="InstanceOfMatchingRule" type="AOPLearning.InstanceOfMatchingRule, AOPLearning" />

    </typeAliases>

    <containers>

      <container>

        <types>

          <type type="IAccountContract" mapTo="AccountService">

            <lifetime type="singleton" />

          </type>

          <type type="ExceptionCallHandler">

            <typeConfig>

              <constructor>

                <param name="exceptionPolicyName" parameterType="string">

                  <value value="UIExceptionPolicy" />

                </param>

              </constructor>

            </typeConfig>

            <lifetime type="singleton" />

          </type>

        </types>

        <extensions>

          <add type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />

        </extensions>

        <extensionConfig>

          <add name="interception" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration">

            <policies>

              <policy name="AccountContractPolicy">

                <matchingRules>

                  <matchingRule name="InstanceOfIAccountContractRule" type="InstanceOfMatchingRule">

                    <injection>

                      <constructor>

                        <param name="type" parameterType="Type">

                          <value value="AOPLearning.IAccountContract, AOPLearning" type="Type" typeConverter="GetTypeConverter" />

                        </param>

                      </constructor>

                    </injection>

                    <lifetime type="singleton" />

                  </matchingRule>

                </matchingRules>

                <callHandlers>

                  <callHandler name="ExceptionCallHandler" type="ExceptionCallHandler" />

                </callHandlers>

              </policy>

            </policies>

            <interceptors>

              <interceptor type="InterfaceInterceptor">

                <key type="IAccountContract" name="IAccountContract" />

              </interceptor>

            </interceptors>

          </add>

        </extensionConfig>

      </container>

    </containers>

  </unity>

</configuration>


Best Regards,
Wei Zhou
Dec 11, 2008 at 5:51 AM
Do you encounter any errors?  Try adding a <default> element to under the <interceptor> element.



Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Dec 11, 2008 at 2:36 PM

I did not encounter any problems and just my call handler is not called, but if I use call handler attribute, it works well. Could you help me to test my code? The following is my code.

C#

namespace AOPLearning

{

class Program

{

static void Main(string[] args)

{

try

{

IUnityContainer unityContainer =3D new UnityContainer();

UnityConfigurationSection section =3D (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

section.Containers.Default.Configure(unityContainer);

IAccountContract service =3D unityContainer.Resolve<IAccountContract>();

service.Create();

}

catch (Exception ex)

{

Console.WriteLine(string.Format("Type: {1}{0}{0}Message: {2}", Environment.NewLine, ex.GetType().Name, ex.Message));

}

Console.ReadKey(true);

}

}

public interface IAccountContract

{

//[ExceptionCallHandler("UIExceptionPolicy")]

void Create();

}

public class AccountService : IAccountContract

{

public void Create()

{

throw new InvalidOperationException("Withdraw error.");

}

}

[ConfigurationElementType(typeof(CustomHandlerData))]

public class NotificationHandler : IExceptionHandler

{

public NotificationHandler(NameValueCollection ignore)

{

}

public Exception HandleException(Exception exception, Guid handlingInstanceId)

{

Console.ForegroundColor =3D ConsoleColor.Red;

Console.WriteLine(exception.Message);

return exception;

}

}

}

Configuration

<configuration>

<configSections>

<section name=3D"exceptionHandling" type=3D"Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3D4.1.0.0, Culture=3Dneutral, PublicKeyToken=3D31bf3856ad364e35" />

<section name=3D"unity" type=3D"Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=3D1.2.0.0, Culture=3Dneutral, PublicKeyToken=3D31bf3856ad364e35" />

</configSections>

<exceptionHandling>

<exceptionPolicies>

<add name=3D"UIExceptionPolicy">

<exceptionTypes>

<add type=3D"System.Exception, mscorlib, Version=3D2.0.0.0, Culture=3Dneutral, PublicKeyToken=3Db77a5c561934e089" postHandlingAction=3D"None" name=3D"Exception">

<exceptionHandlers>

<add type=3D"AOPLearning.NotificationHandler, AOPLearning, Version=3D1.0.0.0, Culture=3Dneutral, PublicKeyToken=3Dnull" name=3D"Custom Handler" />

</exceptionHandlers>

</add>

</exceptionTypes>

</add>

</exceptionPolicies>

</exceptionHandling>

<unity>

<typeAliases>

<typeAlias alias=3D"string" type=3D"System.String, mscorlib" />

<typeAlias alias=3D"singleton" type=3D"Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

<typeAlias alias=3D"interface" type=3D"Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptor, Microsoft.Practices.Unity.Interception" />

<typeAlias alias=3D"ExceptionCallHandler" type=3D"Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers" />

<typeAlias alias=3D"IAccountContract" type=3D"AOPLearning.IAccountContract, AOPLearning" />

<typeAlias alias=3D"AccountService" type=3D"AOPLearning.AccountService, AOPLearning" />

</typeAliases>

<containers>

<container>

<types>

<type type=3D"IAccountContract" mapTo=3D"AccountService">

<lifetime type=3D"singleton" />

</type>

<type type=3D"ExceptionCallHandler" name=3D"ExceptionCallHandler">

<typeConfig>

<constructor>

<param name=3D"exceptionPolicyName" parameterType=3D"string">

<value value=3D"UIExceptionPolicy" />

</param>

</constructor>

</typeConfig>

<lifetime type=3D"singleton" />

</type>

</types>

<extensions>

<add type=3D"Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />

</extensions>

<extensionConfig>

<add name=3D"interception" type=3D"Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration">

<interceptors>

<interceptor type=3D"interface">

<default type=3D"IAccountContract" />

<key type=3D"IAccountContract" />

</interceptor>

</interceptors>

<policies>

<policy name=3D"IAccountContractPolicy">

<callHandlers>

<callHandler type=3D"ExceptionCallHandler" name=3D"ExceptionCallHandler" />

</callHandlers>

</policy>

</policies>

</add>

</extensionConfig>

</container>

</containers>

</unity>

</configuration>

您好,

周巍

Best Regards,

Wei Zhou

Dec 14, 2008 at 12:56 AM
Edited Dec 15, 2008 at 2:39 AM
Are you sure you're not encountering any errors? I copy your configuration and I'm getting an error, seems like there's something wrong with the config. I'm still trying to figure out what's wrong but maybe you could repost again your configuration or better yet, send me your solution


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 15, 2008 at 10:52 AM
Should've noticed before, you should also define a matching rule.

<policy name="IAccountContractPolicy">
<
matchingRules>
<
matchingRule name="Hello" type="Microsoft.Practices.Unity.InterceptionExtension.TypeMatchingRule, Microsoft.Practices.Unity.Interception">
<
injection>
<
constructor>
<
param name="typeName" parameterType="System.String">
<
value value="AOPLearning.AccountService" type="System.String"/>
</
param>
<
param name="ignoreCase" parameterType="System.Boolean">
<
value value="true" type="System.Boolean" />
</
param>
</
constructor>
</
injection>
</
matchingRule>
</
matchingRules>
<
callHandlers>
<
callHandler type="ExceptionCallHandler" name="ExceptionCallHandler" />
</
callHandlers>
</
policy>

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com