Replace for <extensionConfig>

Mar 22, 2010 at 3:51 PM

Previously (v1.2), for the interception extension configuration, I had been using the following xml:

<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">
		<interceptors>
			<interceptor type="transparentProxy">
				<key type="IMyService"/>
			</interceptor>
		</interceptors>
	</add>
</extensionConfig>

How could make the same configurations in v 2.0. Please advice.

Mar 23, 2010 at 6:19 AM

This has changed pretty radically, for the better (Well, I think so at least).

The first thing you need to do is add a section extension to enable the interception schema. Then you can specify the interceptor types directly on your type. Finally (this is new), you need to specify you want to do policy injection on the type.

So it ends up looking like this:

<unity>
<sectionExtension type="Microsoft.Practices.Unity.Interception.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />

<container>

<register type="IMyService" mapTo="MyImplemetation">
<interceptor type="TransparentProxyInterceptor" />
<policyInjection />
</register>

</container>

</unity>

You can also configure interception separately (to allow you to do some cut-and-paste from old config files, don't do this for new stuff please). That looks like this:

<unity>

  <sectionExtension type="Microsoft.Practices.Unity.Interception.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />

  <container>

    <interceptors>
      <interceptor type="TransparentProxyInterceptor">
        <key type="IMyService" />
      </interceptor>
    </interceptors>
  </container>

</unity>

I personally find the former a LOT more readable and understandable, since you have everything about a type in the same spot instead of spread through several parts of the file.

You will still need to put the <policyInjection /> element on the <register> element in order to get policy injection to actually happen.

 

Mar 23, 2010 at 9:55 AM

Thank you for the great explanation.

I personally find the former a LOT more readable and understandable, since you have everything about a type in the same spot instead of spread through several parts of the file.

Yep! I'm totally agree, that is definitely looks much better.

You will still need to put the <policyInjection /> element on the <register> element in order to get policy injection to actually happen.

Is there could be a case, when I need to specify the interceptor, but without the policyInjection tag? If it is so, could you please say two words about such cases. If it is not, I still do not understand why the policyInjection tag is required. Please advice.

Mar 23, 2010 at 6:24 PM

We've added another layer to the interception pile - the idea of an interception behavior. A behavior is a lot like a call handler, except that behaviors execute for ALL intercepted method calls. Call handlers, on the other hand, always only executed for methods they were attached to. This gives us a bunch of new capabilities, not the least of which is to use an interception behavior to add entire new interfaces to intercepted objects.

Policy Injection is now implemented as an interception behavior. So that tag is turning it on. You could also say:

 

<interceptionBehavior type="PolicyInjectionBehavior" />

but we figured that policy injection would be so common that it was worth having a special purpose shorthand tag for it.

So, yes, there are situations where you have interception without policy injection, if you have some other behaviors you want run instead.

 

May 4, 2010 at 3:32 PM

What about <extensions> , suppose you have some of the extensions already added like DataAccessBlockExtension 

in the extensions tag

Also , is there any sample code avaliable for exception/logging/validation handlers with Entlib 5.0 Unity 2.0 . There are error thorwn for <sectionExttension> as unrecognized element..

 

 

May 4, 2010 at 6:10 PM

The <extension> tag is still supported and works exactly the same as before.

The docs are coming. What actual error are you seeing, and what's the config file? "there are error thorwn" and an obvious typo don't help much in trying to debug what's going on.

 

May 5, 2010 at 9:53 AM

Let me explain it. When I use the extensionConfig in Unity Configuration,Unrecognized element 'extensionConfig'. (D:\RND\AOPWithUnity\AOPWithUnity\bin\Debug\AOPWithUnity.vshost.exe.Config line 20)

<unity>
    <containers>
       <container name="Default">
        <types>
          <type type="AOPWithUnity.IDoWork, AOPWithUnity"
                mapTo="AOPWithUnity.DoWork, AOPWithUnity"  />
        </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">
         <!--<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration" />-->
            <policies>
              <policy name="InterceptingDoWorkPolicy">
                <matchingRules>
                  <matchingRule name="MatchDoWork" type="Microsoft.Practices.Unity.InterceptionExtension.TypeMatchingRule, Microsoft.Practices.Unity.Interception">
                    <injection>
                      <constructor>
                        <param name="typeName" parameterType="System.String">
                          <value value="AOPWithUnity.DoWork" type="System.String"/>
                        </param>
                        <param name="ignoreCase" parameterType="System.Boolean">
                          <value value="true" type="System.Boolean" />
                        </param>
                      </constructor>
                    </injection>
                  </matchingRule>
                </matchingRules>
                <callHandlers>
                  <callHandler name="LogHandler" type="AOPWithUnity.LogHandler, AOPWithUnity" />
                </callHandlers>
              </policy>
            </policies>
            <interceptors>
              <interceptor type="Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptor, Microsoft.Practices.Unity.Interception">
                <default type="AOPWithUnity.IDoWork, AOPWithUnity" />
              </interceptor>
            </interceptors>
           </add>
         </extensionConfig>
      </container>
    </containers>
  </unity>

So, looking at the above post, I replace with sectionExtension as below and I get Unrecognized element 'sectionExtension'. (D:\RND\AOPWithUnity\AOPWithUnity\bin\Debug\AOPWithUnity.vshost.exe.Config line 22)

<unity>
    <containers>
       <container name="Default">
        <types>
          <type type="AOPWithUnity.IDoWork, AOPWithUnity"
                mapTo="AOPWithUnity.DoWork, AOPWithUnity"  />
        </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">-->
         <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration" />
            <policies>
              <policy name="InterceptingDoWorkPolicy">
                <matchingRules>
                  <matchingRule name="MatchDoWork" type="Microsoft.Practices.Unity.InterceptionExtension.TypeMatchingRule, Microsoft.Practices.Unity.Interception">
                    <injection>
                      <constructor>
                        <param name="typeName" parameterType="System.String">
                          <value value="AOPWithUnity.DoWork" type="System.String"/>
                        </param>
                        <param name="ignoreCase" parameterType="System.Boolean">
                          <value value="true" type="System.Boolean" />
                        </param>
                      </constructor>
                    </injection>
                  </matchingRule>
                </matchingRules>
                <callHandlers>
                  <callHandler name="LogHandler" type="AOPWithUnity.LogHandler, AOPWithUnity" />
                </callHandlers>
              </policy>
            </policies>
            <interceptors>
              <interceptor type="Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptor, Microsoft.Practices.Unity.Interception">
                <default type="AOPWithUnity.IDoWork, AOPWithUnity" />
              </interceptor>
            </interceptors>
           <!--</add>
         </extensionConfig>-->
      </container>
    </containers>
  </unity>

Is there something incorrect/incomplete with configuration? Please suggest correct configuration.Thanks

May 5, 2010 at 7:01 PM

<sectionExtension> goes directly under the <unity> tag, not within a <container> tag.

Also, you can take out all the parameterType="" attributes, and the type="" attributes on values. They're automatically deduced now.

This is all in the documentation, and you have no idea how frustrated I am right now about our inability to get it shipped.

 

Jun 4, 2010 at 2:51 PM

Hi, I have configured my unity container for extension using API, since not available thru configuration ..

 IUnityContainer container = AppContainer.Instance.UnityContainer;

 container.AddNewExtension<Interception>();

 container.RegisterInstance<ICallHandler>("ExceptionCallHandler", new ExceptionCallHandler("ExceptionPolicy"));

container.Configure<Interception>()

.SetInterceptorFor<IDoWork>(new TransparentProxyInterceptor())

.AddPolicy("ExcpetionPolicy")

.AddMatchingRule(new MemberNameMatchingRule("Work"))

.AddCallHandler("ExceptionCallHandler");

I have 3 points:

1. if I use .SetInterceptorFor(typeof(IDoWork), "AOPWithUnityWOConfig.IDoWork", (new TransparentProxyInterceptor())) instead of .SetInterceptorFor<IDoWork>(new TransparentProxyInterceptor()) my type does not get recognized and interception does not happen.

2. At application level I am loading all the assemblies and checking if type is interface then I want to set the interception.. for ex.

foreach (Assembly objAssembly in assemblies)

{//Load all the types in the assembly

Type [] types = objAssembly.GetTypes();

MethodInfo[] Methods = { };

PropertyInfo[] Props = { };

for (int  i = 0; i < types.Count(); i++)

{Type tempType = types[i];

if (tempType.IsInterface)

{container.Configure<Interception>()

.SetInterceptorFor(tempType, tempType.Name, (new TransparentProxyInterceptor()));

Methods = tempType.GetMethods();

foreach (MethodInfo m in Methods)

{container.Configure<Interception>()

.AddPolicy("ExceptionPolicy")

.AddMatchingRule(new MemberNameMatchingRule(m.Name))

.AddCallHandler("ech");

}

But the bold line does not set the interceptor. So what is the correct way to add it?

 

3. I have found piece of code http://weblogs.asp.net/ricardoperes/archive/2009/09/04/set-default-interceptor-unity-extension.aspx?CommentPosted=true#commentmessage, Could you please suggest if this is the right way or any sample to intercept the interfaces at application level.

Thanks..