AOP with WCF.

Jan 17, 2011 at 8:39 PM

I have a WCF application that I would like to intercept every message that comes in. The examples given show a new Target created for each call. Where would be the best place to 'configure' the interface for future interception without having to create a new target each time? The problem as I see it is that the Target isn't created until the message request is deserialized so it would not be feasible to do a 'new' with each message. Ideas?

Coordinator
Jan 18, 2011 at 5:14 AM
Edited Jan 18, 2011 at 6:25 AM

Hi Kevin!

You wrote: "The examples given show a new Target created for each call".

 fluentAOP allows you to intercept all calls with a single instruction: InterceptAll(). Example:

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptAll()
	.OnBefore(() => Console.WriteLine("--OnBefore--"))
	.OnAfter(() => Console.WriteLine("--OnAfter--"))
	.Save();

I created an entry in the QuickStart page to show how to use it.

You wrote: "Where would be the best place to 'configure' the interface for future interception without having to create a new target each time?"

You could use a factory method to hide the proxy creation:

public static TTarget Create<TTarget>(TTarget target)
{
	return new Proxy<TTarget>()
	   .Target(target)
	   .InterceptAll()
	   .OnBefore(()=> Console.WriteLine("--OnBefore--"))
	   .Save();
}
...
var foo = ProxyFactory.Create<IFoo>( target );

You wrote: "The problem as I see it is that the Target isn't created until the message request is deserialized so it would not be feasible to do a 'new' with each message"

WCF provides its own extensibility points. This is more likely the kind of solution you are looking for. You can customize the dispatcher runtime behavior and intercept messages before/after they are serialized into objects. Search for WCF custom behaviors. Hopefully this will point you in the right direction.

Jan 18, 2011 at 8:24 AM

I guess I need to get back to some detail with WCF. As part of the service contract an interface is defined (say IFoo) and an implementation Foo. As you suggested I need to look at WCF extensibility points. Assuming that WCF instantiates Foo for me I would have:

var foo = new Proxy<IFoo>()
       .Target(fooInstance)
       .InterceptAll()
       .OnBefore(() => Console.WriteLine("--OnBefore--"))
       .OnAfter(() => Console.WriteLine("--OnAfter--"))
       .Save();

Would I need to call any particular method off of ‘var foo’? Or will Save() persist what I need for the interception?

From: pablomx [email removed]
Sent: Monday, January 17, 2011 11:15 PM
To: rkevinburton@charter.net
Subject: Re: AOP with WCF. [fluentaop:242131]

From: pablomx

You wrote: "The examples given show a new Target created for each call".

fluentAOP allows you to intercept all calls with a single instruction: InterceptAll(). Example:

var foo = new Proxy<IFoo>()
       .Target(new Foo())
       .InterceptAll()
       .OnBefore(() => Console.WriteLine("--OnBefore--"))
       .OnAfter(() => Console.WriteLine("--OnAfter--"))
       .Save();

I created an entry in the QuickStart page to show how to use it.

You wrote: "Where would be the best place to 'configure' the interface for future interception without having to create a new target each time?"

You could use a factory method to hide the proxy creation:

public static TTarget Create<TTarget>(TTarget target)
{
           return new Proxy<TTarget>()
               .Target(target)
               .InterceptAll()
               .OnBefore(()=> Console.WriteLine("--OnBefore--"))
               .Save();
}
...
var foo = ProxyFactory.Create<IFoo>( target );
 

You wrote: "The problem as I see it is that the Target isn't created until the message request is deserialized so it would not be feasible to do a 'new' with each message"

WCF provides its own extensibility points. This is more likely the kind of solution you are looking for. You can customize the dispatcher runtime behavior and intercept messages before/after they are serialized into objects. Search for WCF custom behaviors. Hopefully this will point you in the right direction.

Jan 18, 2011 at 6:31 PM

I found:

http://weblogs.asp.net/fabio/archive/2009/03/24/inversion-of-control-with-wcf-and-unity.aspx

But I am unclear how to apply FluentAOP with this new found knowledge.

Any idea?

From: pablomx [email removed]
Sent: Monday, January 17, 2011 11:15 PM
To: rkevinburton@charter.net
Subject: Re: AOP with WCF. [fluentaop:242131]

From: pablomx

You wrote: "The examples given show a new Target created for each call".

fluentAOP allows you to intercept all calls with a single instruction: InterceptAll(). Example:

var foo = new Proxy<IFoo>()
       .Target(new Foo())
       .InterceptAll()
       .OnBefore(() => Console.WriteLine("--OnBefore--"))
       .OnAfter(() => Console.WriteLine("--OnAfter--"))
       .Save();

I created an entry in the QuickStart page to show how to use it.

You wrote: "Where would be the best place to 'configure' the interface for future interception without having to create a new target each time?"

You could use a factory method to hide the proxy creation:

public static TTarget Create<TTarget>(TTarget target)
{
           return new Proxy<TTarget>()
               .Target(target)
               .InterceptAll()
               .OnBefore(()=> Console.WriteLine("--OnBefore--"))
               .Save();
}
...
var foo = ProxyFactory.Create<IFoo>( target );
 

You wrote: "The problem as I see it is that the Target isn't created until the message request is deserialized so it would not be feasible to do a 'new' with each message"

WCF provides its own extensibility points. This is more likely the kind of solution you are looking for. You can customize the dispatcher runtime behavior and intercept messages before/after they are serialized into objects. Search for WCF custom behaviors. Hopefully this will point you in the right direction.

Coordinator
Jan 19, 2011 at 3:12 AM

I don't think you need fluentAOP, Unity or anything else to intercept WCF messages. You just have to extend the dispatcher and inject your own logic. I truely believe this is the easiest and cleanest way to do it.

Jan 19, 2011 at 2:58 PM
I hear what you are saying WCF does offer alot of extension points. But it is still very appealing to be able to 'configure in' a method without changing source code. Admittedly I could do the same thing by scanning some folder for extensions and/or looking in a config file but it seems that this has been taken care of with Unity and AOP. I still think that there is some benefit to merging the two technologies.


On Tue, Jan 18, 2011 at 9:12 PM, pablomx wrote:

From: pablomx
I don't think you need fluentAOP, Unity or anything else to intercept WCF messages. You just have to extend the dispatcher and inject your own logic. I truely believe this is the easiest and cleanest way to do it.
To add a post to this discussion, reply to this email ( fluentaop@discussions.codeplex.com)
To start a new discussion for this project, email fluentaop@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribeon CodePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com
Coordinator
Jan 20, 2011 at 5:37 AM

You wrote: "it is still very appealing to be able to 'configure in' a method without changing source code"

WCF extensions points do that. They will allow you to intercept the message and inject your logic before the target method get called. To the best of my knowledge, the only chance you have to hook up your logic to the message lifecycle is to use a custom extension.

Jan 20, 2011 at 4:57 PM
I guess I will have to ask a WCF guru. I didn't know that WCF could take arbitrary code (not developed with th WCF application itself), and inject it after a message request. Thank you.

On Jan 19, 2011, at 11:37 PM, "pablomx"<notifications@codeplex.com> wrote:

From: pablomx

You wrote: "it is still very appealing to be able to 'configure in' a method without changing source code"

WCF extensions points do that. They will allow you to intercept the message and inject your logic before the target method get called. To the best of my knowledge, the only chance you have to hook up your logic to the message lifecycle is to use a custom extension.