Extensions - WCF Http Services

Extensions Series

WCF Http Extensions

Often the case arrises that you need to host a WCF service in your site, FunnelWeb extensions can help you with that too! This guide will show you how to setup your WCF services so they are hosted in FunnelWeb and have DI support from Autofac.


WCF Web APIs Preview 3



Autofac Integration

First off we need to add a new class to our project, call it AutofacConfigurableServiceHostFactory

public class AutofacConfigurableServiceHostFactory : AutofacServiceHostFactory, IConfigurableServiceHostFactory
    // Methods
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        return new WebHttpServiceHost(serviceType, Configuration, baseAddresses);

    public HttpHostConfiguration Configuration { get; set; }

This is required to let Autofac work with the new Wcf Http bits.

Creating the service

You can now create your service contract, we don't have to create any interfaces or .svc files for this =)

public class DemoService
    [WebGet(UriTemplate = "getdata")]
    public List<string> GetDemoData()
        return new List<string>
Registering the service

And finally in your extension Initialize method register your service with autofac


Then register the service route

Routes.AddServiceRoute<DemoService, AutofacConfigurableServiceHostFactory>("services/demoservice", new HttpHostConfiguration());

And that is all there is to it, you can now browse to /services/demoservice/getdata and you will get this:

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Json Support

If you happen to want your Wcf services to support Json, you simply have to create a new HttpHostConfiguration with Extra request/response processors. These processors kick in based on the Accept header in the http request.

public class JsonValueConfiguration : HttpHostConfiguration, IProcessorProvider
    public void RegisterRequestProcessorsForOperation(HttpOperationDescription operation, IList<Processor> processors, MediaTypeProcessorMode mode)
        processors.Add(new FormUrlEncodedProcessor(operation, mode));
        processors.Add(new JsonProcessor(operation, mode));

    public void RegisterResponseProcessorsForOperation(HttpOperationDescription operation, IList<Processor> processors, MediaTypeProcessorMode mode)
        processors.Add(new JsonProcessor(operation, mode));

Now all you need to do is update your service route to pass in the JsonValueConfiguration.

Routes.AddServiceRoute<JsonService, AutofacConfigurableServiceHostFactory>("services/demoservice", new JsonValueConfiguration());

So see WCF's magic at work the easiest way is to bust open fiddler.

Lets start with building a request with an Accept header of application/xml:

Fiddler xml Request Builder

When we hit Execute this is the response we will get

Xml Response Headers Xml Response

Next step is to ask our WCF service for a json response (jQuery will automatically do this for us).

Fiddler json Request Builder

And execute will return:

Json Response Headers Json Response

Hope that helps! If you check out the WCF project on codeplex they have some pretty cool examples of what you can do with Accept headers, their png processor is pretty interesting =)

Posted by: The FunnelWeb Team
Last revised: 05 Feb, 2011 02:41 AM History


No comments yet. Be the first!

No new comments are allowed on this post.