need help on RegisterInstance

Dec 24, 2008 at 7:17 AM
Edited Dec 24, 2008 at 8:57 AM
Hi, I have a problem with RegisterInstance() method.

Below partial of my test code. If I change the flag to true to execute the 1st condition block, it worked as expected

But if I toggle the flag to execute 2nd block, AOPErrHandler never get invoked!

Probably some problem with my RegisterInstance approach, can you help rectify it?

 What I wanna to do is like the 1st block, but over an already instantiated object 

 

 

 

IUnityContainer uc = new UnityContainer();
uc.AddNewExtension<
Interception>();
uc.Configure<
Interception>().SetInterceptorFor<Class1>(new VirtualMethodInterceptor());

 

 

if (!true) // <-- toggle this flag to test
{
    c1 = uc.Resolve<
Class1>();
    c1.Method01();
}
else
{
    c1 =
new Class1();
    uc.RegisterInstance<Class1>(c1);

    Class1 c2 = uc.Resolve<Class1>();
    c2.Method01();
}


 

 

public class Class1
{
[AOPErr]
public virtual void Method01()
{
    Debug.WriteLine("xxxxxxxxxxxxxxxx");
}
}

 

public

 

class AOPErrHandler : ICallHandler { ... }
public class AOPErrAttribute : HandlerAttribute { ... }

 

 

 

 

 

 

 

Dec 25, 2008 at 3:09 AM
The interceptor only gets created if you resolve the object through the container. Doing a new and registering an instance just puts the raw object in place without interception.

You could new up the object, call container.BuildUp(obj) to get the resulting interceptor out, and register that, but it's a lot easier to just register the type with interception and the ContainerControlledLifetimeManager.

Dec 26, 2008 at 2:38 AM
Edited Dec 26, 2008 at 2:38 AM
>>The interceptor only gets created if you resolve the object through the container.

That's what I attempted : Class1 c2 = uc.Resolve<Class1>();


>>Doing a new and registering an instance just puts the raw object in place without interception.

You mean in this way?

IUnityContainer uc = new UnityContainer();

    c1 = new Class1();
    uc.RegisterInstance<Class1>(c1);

    Class1 c2 = uc.Resolve<Class1>();
    c2.Method01();

I try it, same result, no interception over the Method01()



>>The interceptor only gets created if you resolve the object through the container. Doing a new and registering an instance just puts the raw object in place without interception.

>>You could new up the object, call container.BuildUp(obj) to get the resulting interceptor out, and register that, but it's a lot easier to just register the type with interception and the ContainerControlledLifetimeManager.

Sorry, I can't grasp this point.

I trial around by replace the RegisterInstance with BuildUp or RegisterType, but I still can't get the expected result.
Can you help give a simple sample or URL of the proper API to implement this?
Jan 7, 2009 at 9:51 AM
ok, I found this on http://msdn.microsoft.com/en-us/library/dd140045.aspx :

Interception only happens on virtual methods. You must set up interception at object creation time and cannot intercept an existing object.

***

So that tell me it won't for me for my case.

Anyway, I already dig into using PostSharp, and gonna taking it up for AOP implementation.