PDA

View Full Version : Logging via AOP in WCF service


slein
06-06-2007, 09:57 AM
Hi,

I have been using dependency injection described by Orand under http://orand.blogspot.com/2006/10/wcf-service-dependency-injection.html.

Now I'd like to integrate logging via AOP into my service class. Therefore I think it's necessary to "wrap" my service object as Target of a ProxyFactoryObject. But how do I have to "connect" it with the requirements of the WCF (definition of endpoints etc.)?

Currently my implementation looks like this:

1. App.config:

<spring>
..
<objects xmlns="http://www.springframework.net">
<object id="loggingAroundAdvice" type="MyProject.Spring.LoggingAroundAdvice, MyProject">
<property name="Logger" value="MyService" />
</object>
<object id="myService" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target"><object type="MyProject.MyService, MyProject"/></property>
<property name="InterceptorNames">
<list><value>loggingAroundAdvice</value></list>
</property>
</object>
</objects>
</spring>
..
<system.serviceModel>
<services>
<service name="MyProject.MyService">
<endpoint address="MyServiceBasic" binding="basicHttpBinding" contract="MyProject.IMyService" />
</service>
</services>
</system.serviceModel>

2. Providing ProxyFactoryObject for my service (SpringInstanceProvider):

public class SpringInstanceProvider : IInstanceProvider
{
private Type _serviceType;

public SpringInstanceProvider(Type serviceType)
{
_serviceType = serviceType;
}

public object GetInstance(InstanceContext instanceContext, Message message)
{
object result = null;
IApplicationContext context = ContextRegistry.GetContext();
string[] objectNames = context.GetObjectNamesForType(typeof(ProxyFactoryO bject), true, true);
foreach (string name in objectNames)
{
ProxyFactoryObject factory = (ProxyFactoryObject)context.GetObject(name);
if (factory.TargetSource.TargetType.Equals(_serviceTy pe))
{
if (result == null)
{
result = factory.GetObject();
break;
}
}
}
return result;
}
}

3. Creation of ServiceHost:

IMyService myService = (IMyService)new SpringInstanceProvider(typeof(MyService)).GetInsta nce(null);
using (ServiceHost host = new ServiceHost(myService, baseAdresses))
{
host.Open();
..
}


By this way no endpoints can be found by the host because the service object is not of the desired type but a proxy object ..

Thanks for any hint!

Bruno Baia
06-06-2007, 10:37 AM
Hi,

You should take a look to the WCF integration we made, because it allows DI and AOP.
Get the latest nightly build, and take a look to the WcfQuickStart.


HTH,
Bruno

slein
09-14-2007, 12:53 PM
Thanks for the hint!


Now I have a WCF service method declaring one or more out parameters and a return value.

When I try to call this method the method invocation.Proceed() within my method interceptor throws a fatal error that it cannot proceed.

How can I add the logging functionality via AOP to those service methods?

Thanks in advance!

Bruno Baia
09-14-2007, 02:00 PM
Hi,

Are you using Spring.Net 1.1 RC1 ?

- Bruno

slein
09-17-2007, 07:26 AM
I'm using Spring.NET-1.1.0 Preview 3.

.ben
09-17-2007, 11:30 AM
I believe out parameters where not supported then, use RC1 or the latest build.

Bruno Baia
09-17-2007, 02:27 PM
Hi,

I believe out parameters where not supported then, use
RC1 or the latest build.
What you Believe is true :)

slein, read Chapter 4. Moving from M2 to RC1 (http://www.springframework.net/doc-latest/reference/html/migration.html) while updating to RC1.

- Bruno