Creating a REST (and SOAP) Enabled WCF Service

By: Nicklas Møller Jepsen

In this post I am going to walk you through the process of creating a REST/SOAP enabled WCF service using .NET 4.5.1 and Visual Studio 2013.

Select New Project > Templates > C# > WCF > WCF Service Application

This will create a project containing only the basics – the interface, implementation and a .config file.

If you’re wondering about the name Trusic then I can tell you that it is the name of a service I’m writing for use in an in-depth guide to object oriented thinking and programming using the newest .NET technologies.

You can try the service generated by the template out by marking the Service1.svc file in solution explorer and hit F5 to debug the service. (By marking Service1.svc Visual Studio is starting the WCF Test Client for you).

Now select the GetData operation and enter a value and hit Invoke.

This invokes the default SOAP endpoint which is fine, but SOAP is a bit old-school and we are of course going to be publishing our service through a RESTful JSON endpoint also. This requires minimal code changes and only a bit changes to the web.config file.

In you service’s SVC file add the following annotation to the GetData method: [WebGet] it should look like so:

public string GetData(int value)
	return string.Format("You entered: {0}", value);

In the Web.config insert the following after :

      <service name="WcfService1.Service1">
        <endpoint address="soap" binding="basicHttpBinding"  behaviorConfiguration="soapBehavior" contract="WcfService1.IService1"/>
        <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="WcfService1.IService1"/>

Make sure that the service name is set to the name of your service SVC file and that the contract is set to the name of your interface.

In the element add:

        <behavior name="soapBehavior">
        <behavior name="jsonBehavior">

Now hit F5 and open your browser, navigate to: http://localhost:/.svc/json/GetData?value=10 and look for the magic to happen:

If that isn’t good enough we still have our SOAP endpoint available. For instance in soapUI try to invoke your service by adding a new endpoint. Now soapUI has generated a proxy client you can use to invoke the SOAP endpoint of the service.