ServiceStack extensibility using MEF

A while back, I had started playing with the Managed Extensibility Framework (MEF) from Big Mike – an extensibility framework that lets you discover components that implements your interface, thus allowing you to do Software Composition. For close to a year now, I have also been playing with ServiceStack – a clean and elegant Open Source solution for Web Services.

I’ve wondered for quite a while now if the two could be combined, i.e. if it was possible to do Service Composition by using MEF and ServiceStack. I figured it could be done, but could never find the time to verify my assumptions…until now.

“Why would you ever want to do such a thing?”, you may ask. Outside of the obvious “because you can”, there is value in making your service layer extensible, especially on a large project in which multiple teams are involved. So as to protect code boundaries and prevent a big mess in which multiple teams would have access to the same code repository, each team could simply supply their DLLs containing their services and our Service Layer would pick them up and configure them.

We first need to define an interface. The IComposobaleBootstrapper is a simple interface with two methods: Compose and GetAssembly.

  • Compose: this method lets our implementors define what objects they need injected, as well as the routes they want for their service
  • GetAssembly: this method is necessary for our Application Host. At launch time, ServiceStack loads up all the services in your application based on the Assemblies you pass in. Because we do not know in which assemblies our implementors have defined their Services, we put the Onus on them to give us that information

Now that we have an interface, we can implement it in our sample service below.

The last piece is the Application host

The most important line in the Application Host is line #43, which is where we tell ServiceStack to load up the Assemblies containing our Composable Service Definitions.

Voila! I hope this helps!

ServiceStack extensibility using MEF

One thought on “ServiceStack extensibility using MEF

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s