Migrating from ‘asmx’ webservices to WCF with Spring.Net

Building webservices with Spring.Net is a piece of cake. The framework supported a single programming model long before WCF was available, exposing your service was just a different entry in your configuration. If you’re interested how you need to do this, please read my previous blog entries.

WCF introduced this same concepts to the main .Net Framework but also provides a lot of other functionality out of the box. Security, logging, … just change some xml tag and you’re done.

I make it sound a bit too easy here 🙂 figuring out what is wrong in your service section of the app/web.config is frustrating even with all the tools that are available.

So what do we need to change in our application to switch from ‘old’ webservices to the WCF counterpart?

Well if you’ve been following some best practices then your service is already behind an interface, and that means the only thing you have to change is the way Spring injects this service in your application.

public class OrderPresenter
	: IOrderPresenter
{
	public IOrderService OrderService { get; set; }
	public IOrderView View { get; set; }
}

With the code example above the presenter expects an instance which implements the IOrderService interface, it doesn’t care if it’s WCF, ‘asmx’ webservice or just a local object.

To add a WCF client object you do not need to change anything in your existing codebase, only the way Spring can find the Service you’re connecting to as illustrated below.

<object id="OrderService" type="ServiceContract.IOrderService, ServiceContract"
		factory-object="OrderServiceChannelFactory"
		factory-method="CreateChannel" />
 
<object id="OrderServiceChannelFactory"
	type="System.ServiceModel.ChannelFactory&lt;ServiceContract.IOrderService>, System.ServiceModel">
	<constructor-arg name="endpointConfigurationName" value="OrderServiceEndpoint" />
</object>

The OrderService above will be used by my presenter, and is created via the OrderServiceChannelFactory. Anyone who has some experience with WCF sees that this is just the same as you would do in code when using channels.

So we’re done!

No, not really. There is one, rather big, caveat.

In the ‘old’ days my client service reference would still function if there had been an exception. For example if the network cable was unplugged and I called a method on the server I’d get an exception. If I’d plug the cable back in and call the method again, my client service reference would still function and call the server successfully.

This is not the case with WCF channels, if there is an exception, either communication wise, like the unplugged network cable, or the server throws an exception my client channel will transition in a ‘faulted’ state. Any calls I make on this instance will fail, I need a new instance from the channelfactory to get my application going again.

I could change my presenter to check the service reference, but that would mean I need to couple my presenter to WCF and need to add new behaviour. This is not something you’d want to do. I can’t get around this problem by ‘normal’ AOP techniques. I can’t let the channel check it’s own status and have it magically change it’s own memory reference to a new instance.

Wouldn’t it be great if that boilerplate code (checking the state, creating new channels,….) was automagically done when calling a method on my client service reference?

Well using the Spring framework I’ve been able to accomplish this. You won’t need to change or add any code as long as your service is exposed with an interface. At runtime a new subclass of my ChannelManager class is created which implements the service interface. All calls are forwarded to the ExecuteInChannel method, which creates a channel, calls the method you originally wanted to execute, returns the result and closes the channel. You can also subclass the ChannelManager class to change the way you want to manage your channels.

Over the next few days I hope to add some more functionality, like channel pooling and test it a bit better. Source code will be available.

Series:

Leave a Reply

Your email address will not be published. Required fields are marked *