WCF Web HTTP Service: why and how

Very often I talk on WCF to user groups and find developers confused on various forms of WCF. For example some of the questions I get are as follows

  • How to create REST Service
  • Can we create REST and SOAP Service together
  • What is WCF Web HTTP Service
  • How Web HTTP Service and WCF REST Service are different to each other
  • Can I host HTTP Service and REST Service in a console application
  • Why we need REST and HTTP Services
  • What kind of security can be applied on REST and HTTP Services and many more questions of this kind

In this post I will answer few of the above questions regarding WCF Web HTTP Services.

By definition, WCF Web HTTP programming model allows services to be exposed on non-SOAP EndPoints and helps to create WEB HTTP Services. Now a question may arise that why we need HTTP Services and what are the advantages of using that.

Mainly HTTP Services are designed and created for wide range of clients. Any application capable of following can be potential clients

  • Understand HTTP protocols and verbs
  • Can process URI and URL Schemes
  • Can work with different types of message and data formats
  • Perform HTTP Get and HTTP Post operations

Web HTTP Services doesn’t work on SOAP message format however any serilizable type can be returned from Web HTTP Services. Since HTTP Service does not work with SOAP so all WCF Security features cannot be applied on this. However basic Transport level security can be applied to WCF Web HTTP Services.

Let us see how we can create a very basic WCF Web HTTP Service. Create WCF Service Application project from WCF tab to create Web HTTP Service

image

After creation of project we need to create Service Contract.


 [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebGet]
        string HelloGet(string   name);

        [OperationContract]
        [WebInvoke]
        string  HelloPost(string  name);
       
    }


Above I have created very basic service. Service contains only two methods. One method will work on HTTP GET and other will work on HTTP POST operation. There are restrictions on passing parameters in query string. For example string can be passed in query parameter.


public class Service1 : IService1
    {       

        public string HelloGet(string name)
        {
            return "Hello GET " + name; 
        }

        public string HelloPost(string name)
        {
            return "Hello POST" + name; 
        }
    }



Above Service Contract is implemented as below. It’s very simple implementation in which service is returning string with concatenating of input string parameters.

Now there are various ways WCF WEB HTTP Services can be hosted and its EndPoint can be configured. You can configure and host service

  • In IIS
  • In Windows Services Application
  • In Managed Application like Console Application
  • Windows Azure Services

In this post let us learn how EndPoint can be configured for Web Server hosting like IIS. It’s very simple to configure EndPoints for HTTP Services. You can use Factory method System.ServiceModel.Activation.WebServiceHostFactory to create WEB HTTP Service.

image

You can fetch service from browser by navigating to URL as below. Keep in mind that port number could be different in your scenario.

http://localhost:55400/Service1.svc/HelloGet?name=dj

You can notice that we are passing input dj as string in query parameter. Response from service will be as below,

image

Another way service can be configured and hosted in a console application. In Console Application first you need to add reference of System.ServiceModel and System.ServiceModel.Web in the project.


using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using Demo1;

namespace ConsoleAppHostingService
{
    class Program
    {
        static void Main(string[] args)
        {
            WebServiceHost host = new WebServiceHost(typeof(Service1), new Uri("http://localhost:8989/"));
            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService1), new WebHttpBinding(), "");
            ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
            sdb.HttpHelpPageEnabled = false;
            host.Open();
            Console.WriteLine("Service is running");
            Console.WriteLine("Press enter to quit...");
            Console.ReadLine();
            host.Close();

        }
    }
}


Few points about above hosting code are as follows

  • WEB HTTP Service should be hosted in a WebServiceHost instance.
  • WebHttpBinding is used to created HTTP Service
  • Help page is disabled

On running Console Application, service can be accessed on URL passed in WebServiceHost instance

image

In this way you can create WCF WEB HTTP Service. In further posts we will learn how to consume this service in various kind of clients and work with different kind of data format. I hope this post will help you.

3 thoughts on “WCF Web HTTP Service: why and how

  1. Pingback: Dew Drop – March 10, 2014 (#1739) | Morning Dew

  2. Pingback: Create Image Server using WCF WEB HTTP Service | debug mode......

  3. Hi Dj.

    You did’t tell in your post that where to write “System.ServiceModel.Activation.WebServiceHostFactory”.

    I am confused in this. Please help me out

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s