Unity config file newbie <Please help!>

Sep 15, 2010 at 7:47 PM
Edited Sep 15, 2010 at 7:48 PM

I'm pretty new to unity and i can't seem to get the policy injection via web.config (as opposed to the API) working. I've followed the example code in the help chm (the version for vs2008), noticed a typo or two, and still have very little luck. here's the (reduced-fat) config:

<?xml version="1.0"?>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">    <!--The Unity Configuration Schema-->
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>
    <namespace name="UI"/>
      <extension type="Interception" />
      <register type="TestClass" name="whatever">
        <lifetime type="singleton" />
        <interceptor type="InterfaceInterceptor" name="logger"/>
        <addInterface type="ITest" />
      <register type="ICallHandler" mapTo="LoggingCallHandler" name="logger"></register>
      <register type="IMatchingRule" mapTo="MemberNameMatchingRule"></register>
        <policy name="log" >
          <matchingRule name="mr"></matchingRule>
          <callHandler name="logger"></callHandler>

I apologize in advance if I've done something really stupid, but I've tried what seems like a million different things with no luck whatsoever, and so the config is truly garbled at this stage. all i'm trying to do is have a policy that has a matchrule and a callhandler that intercepts any method implementing the ITest interface.

The TestClass is returned by the container when I call the container to resolve the ITest interface. it's method (there's only 1) gets called, but that's it. no match check, no callhandler...nothing.

MemberNameMatchingRule match method would always return true (by design), but it never gets called. neither does the callhandler (probably because the match algorithm is never performed).

I can get much further using the Unity 2.0 api and can accomplish this 100% using PIAB v 4.1 but have no luck at all with the config file...

Please help!


Sep 16, 2010 at 2:12 AM

A sample configuration for configuring policies is included in the documentation.  Here's the link to it.

When you register the call handler and matching rule, you need to configure its constructor injection in order to tell the container what constructor and parameter values to use when creating the call handler/matching rule.  It's lacking in the config you posted.  I don't suppose that that is the exact version of the config you're using when you described the behavior you're getting?  Another thing, you used the InterfaceInterceptor but does the type which you're registering implements an interface?  If yes, the registration should look like:

<register type="ITestClassInterface" mapTo="TestClass" name="whatever" >.......


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Sep 16, 2010 at 11:56 AM

thanks for your input Sarah!

here's my modified (still failing config (reduced to essentials))

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <assembly name="UI"/>
      <extension type="Interception" />
      <register type="UI.ITest" mapTo="UI.TestClass" ></register>
      <register type="UI.TestClass" name="whyDoesThisFailWithoutAName">
        <!-- Other children, like constructor or property -->
        <interceptor type="InterfaceInterceptor" />
        <policyInjection />
        <policy name="webMethods">
          <matchingRule name="MatchEverything" />
          <callHandler name="LogWebMethodHandler" type="UI.LoggingCallHandler" />
      <register name="MatchEverything" type="IMatchingRule" mapTo="UI.AlwaysReturnTrueRule"></register>
      <register type="ICallHandler" mapTo="UI.LoggingCallHandler"></register>

here's some more detail:

TestClass implements ITest, AlwyasReturnTrueRule implements IMatchingRule and LoggingCallHandler implements ICallHandler. I create an instance of the container, resolve ITest, call it's single method (no parameters) and it fires, but that's it. no interception, no handling, no matching!

I've looked at the link (which is the same as my vs2008 documentation for Unity 2.0) and have attempted to recreate the given scenario....still no luck...where am I going wrong? also, what does the interceptionBehavior element do and why isn't it included in the documentation example?

Sep 17, 2010 at 10:49 AM

I must be missing something! Why doesn't this work?

IUnityContainer container = new UnityContainer();
        new Interceptor<InterfaceInterceptor>(),
        new AdditionalInterface(typeof(IOtherInterface)),
        new InterceptionBehavior<CustomBehavior>("someBehavior"));
ITest t= container.Resolve<MyTestClass>();

It's getting confusing because the documentation says that the behavior pipeline replaced the callhandler pipeline. The example above was taken directly from the documentation. I've replaced all the relevant types with mine but no interception/injection/matching takes place.

Sep 20, 2010 at 3:31 AM

Am I right that this is already a different scenario from the one you previously posted?

Anyway, in your first problem, did you load the unity container from the configuration? ( container.LoadConfiguration(); )

In your second problem, is there a registration for CustomBehavior named "someBehavior"? 

I might be able to point out the causes if you could send a small repros for this.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Sep 20, 2010 at 9:54 AM
Edited Sep 20, 2010 at 10:21 AM


Thanks again for your attention. The 2nd scenario was my attempt to wire up via the API since i had little luck with the config. the first instance i did use the loadconfiguration method. in the 2nd scenario i did not register the custombehavior (it's so clear to me now where i was failing!).

with regard to the first scenario i posted - I think my overall problem was that I didn't grasp all the various things that had to be wired up and then how to leverage the functionality from the container. i was indeed loading configuration as you stated. my MAIN problem is that i was attempting to write my own matching rule which isn't one of the options (custome attribute is though). needless to say, once i corrected that (i just used a namespace matching rule), it worked. is there a way to implement a custom matching rule? will the xml "accept" it? thanks for all your input.

by the way, could you explain the cash-value difference between a callHandler and an interceptionBehavior (i've asked elsewhere but it's always good to get several perspectives).