Creating a RESTful WCF service

DZone 's Guide to

Creating a RESTful WCF service

· Integration Zone ·
Free Resource

RESTful services are quite popular today and you can find them pretty much everywhere – image sharing services, weather services and whatnot. Using .NET development tools, you are also able to develop a RESTful service that will expose some XML data to the requesting client.

If you are thinking that the way to do this would be creating a ASMX web service, than this is not what I’m going to talk about. ASMX web services are outdated and using it instead of WCF wouldn’t be the right solution. At the same time, you probably know that you cannot directly invoke a WCF service through a web browser – only through a client, be that a desktop or web application.

In this article, I am going to show you how it is possible to build a RESTful WCF service and invoke it directly from the browser to get the needed output.

Developing the service

Open Visual Studio and create a new WCF Service Application:

In the existing solution, there is a Web.config file. Instead of editing it at this moment, I’d recommend deleting it since we won’t be using it for now. When I’ll reach the debugging stage, I will let the IDE generate a new one with just the debugging settings present.

As you look through the existing code, you can see that there is a lot of sample data, that might or might not be useful for you. I removed it, so I have a clean Service1 class and a clean IService1 interface.

NOTE: I am leaving the default names for the service and related components.

So after the cleanup, my service class looks like this:

using System;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Web;namespace REST_TEST{    public class Service1 : IService1    {            }}

And the interface for the above mentioned service looks like this:

using System;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Web;namespace REST_TEST{    [ServiceContract]    public interface IService1    {    }}

Now, in the IService1 interface, I am going to define a sample method, called GetDateTime, that will return a string. The only thing that I should add to it is an additional set of attributes that will control how this method will be used by the WCF service. So here is what I have:

[ServiceContract]public interface IService1{    [WebGet(UriTemplate="/")]    [OperationContract]    string GetDateTime();}

With the help of WebGet, I am specifying that the method will be available through a REST call. The UriTemplate defines the access point, this being set to root. Therefore, when testing the service in the browser, the address to call the GetDateTime method will be http://localhost:3660/Service1.svc/. I can easily modify this to something else, for example - /GetDateTime:


This way, when I will access the service by its root identifier, I will get an “Endpoint not found” error (since there is no endpoint defined for that). However, if I access http://localhost:3660/Service1.svc/GetTimeDate , I will call the needed method and get the correct output.

OperationContract specifies that the method is a part of an operation that the service is bound to via a contract. You need to have this attribute present in the method definition (inside the interface) in order to be able to call it from the current service.

Now let’s work on the actual method.

public string GetDateTime(){    return DateTime.Now.ToString();}

This will return the current date and time. Due to the nature of the service, the data will be serialized automatically when returned, therefore at this point you don’t have to worry about manual serialization.

Before going any further, you need to enable metadata publishing for the service. To do this, right click on Service1.svc in the Solution Explorer and select View Markup:

Once there, add the following property to the existing set:


Now, right click on the Service1.svc in Solution Explorer once again and select View in Browser. Once the browser opens, you should see a response similar to this:

This is absolutely normal, since I did not define an endpoint for the root location. Now, in the address bar, right after the service name, type /GetDateTime. When you navigate to that address, here is what you should get:

As I mentioned before, the data is automatically serialized (if possible) and returned to the user. You are now able to use the XML data provided by this service from any client, be it desktop or web. And of course, you can test it right from your web browser.

dotnet ,visual studio ,rest ,wcf ,data access ,c-sharp ,web-service

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}