All of the WCF REST samples use self-hosted services and many people have been asking for a sample that is hosted on IIS, so I'll explain how to do that here. Let's start with a simple service contract:

// IService.cs
namespace SimpleRESTService
{
    [ServiceContract]
    public interface IService
    {
        [Description("Simple echo operation over HTTP GET")]
        [WebGet]
        string EchoWithGet(string s);

        [Description("Simple echo operation over HTTP POST")]
        [WebInvoke]
        string EchoWithPost(string s);
    }

 Next, implement the IService contract:

// service1.cs
namespace SimpleRESTService
{
    public class Service1 : IService
    {
        public string EchoWithGet(string s)
        {
            return "You said " + s;
        }

        public string EchoWithPost(string s)
        {
            return "You said " + s;
        }
    }
}

Add the Service1.svc file:

 <%@ ServiceHost Language="C#" Debug="true" Service="SimpleRESTService.Service1" CodeBehind="Service1.svc.cs" %>

And finally add the web.config file:

 <?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="SimpleRESTService.Service1"
                behaviorConfiguration="RESTBehavior">
        <endpoint address=""
                  binding="webHttpBinding"
                  contract="SimpleRESTService.IService"
                   behaviorConfiguration="MyEndpointBehavior"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RESTBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="MyEndpointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />                             
  </system.serviceModel>
</configuration>

 The easiest way to get this code to run is to create a new WCF Service Application in Visual Studio 2010 (or earlier) and replace the generated files with the files listed here. You can then hit CTRL + F5 and the ASP.NET Development Server will host the WCF app. Don't be confused by the name of the ASP.NET Development Server it can host WCF services. Once the service is hosted you can then use IE to call the operation. Double click on the ASP.NET Development Server icon on the status bar (you may have to click the arrow on the lower right hand side of the status bar to see the icon.) It will display the ASP.NET Development Server dialog which will contain the port number being used by the server.  Then open up IE and type in the following URL: http://localhost%3cport%3e/ where <port> is replaced by the port number found in the ASP.NET Development Server dialog. You should see something like the following:

 Now, the URL we just used isn't very pretty. We'd like to be able to not have to specify the .svc file in the URL. Well, you're in luck the ASP.NET Routing Integration feature allows you to do just that. To use ASP.NET Routing Integration you need to do the following:

1) Turn on ASP.NET compatibility. This is done in the <serviceHostingEnvironment> tag in your web.config file. It can appear anywhere within the <system.ServiceModel> element. Here's an example:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>

2) Add the UrlRoutingModule and UrlRoutingHandler to your web.config file. These are placed within a <system.webServer> element, which appears outside of the <system.serviceModel> element. Here's an example:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
   <add name="UrlRoutingModule"
        type="System.Web.Routing.UrlRoutingModule,
          System.Web.Routing, Version=4.0.0.0,
          Culture=neutral,
          PublicKeyToken=31BF3856AD364E35" />

  </modules>
  <handlers>
   <add name="UrlRoutingHandler"
      preCondition="integratedMode"
      verb="*" path="UrlRouting.axd"
      type="System.Web.HttpForbiddenHandler,
         System.Web, Version=4.0.0.0, Culture=neutral,
         PublicKeyToken=b03f5f7f11d50a3a" />
  </handlers>
 </system.webServer>

3) Add a global.asax file to your project by right clicking on the project name in the solution explorer, select Add, New Item... and Global Application Class. Within the Application_Start method, add a Route to the RouteTable that specifies an optional relative path, the WebServiceHostFactory, and the service type. Here's an example:

protected void Application_Start(object sender, EventArgs e)
{
  RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service1)));
}

Now you can browse to your service when hosted on the ASP.NET Development Server by using this URL:

 http://localhost%3cport%3e/

So now we have the service hosted under the ASP.NET Development Server, we can use Visual Studio to publish the service to an IIS server. In Visual Studio go to the Build menu and select Publish SampleRESTService. This displays a dialog where you can specify where and how to publish the WCF service:

In the dialog above we specify that we want the service to be hosted in IIS under http://localhost/SimpleRESTService

Make sure the service1.svc file is not in the virtual directory that was created after you click publish. If it is there delete it. Then you can browse to your service by using the following URL:

http://localhost/SimpleRESTService/EchoWithGet?s=Hello

 

 


Other Languages

This article is also available in the following languages:

Brazilian Portuguese (pt-BR)