WCF - Using WebHttpBinding for REST services

You can use WebHttpBinding to have REST endpoints in your WCF application to expose simple public service calls. 

Use a UriTemplate in your service contract and a WebHttpBinding endpoint.  Here's an example...


  (IContractName.cs)

namespace TestNamespace
{

    [ServiceContract(SessionMode=SessionMode.NotAllowed)]
    public interface IContractName
    {
        [WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)]
        [OperationContract]
        string GetDate(string day, string month, string year);
    }

}

(ServiceType.cs)

namespace TestNamespace
{

    public class ServiceType : IContractName
    { 
        public string GetDate(string day, string month, string year)
        {
           return new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)).ToString("dddd, MMMM dd, yyyy");
        }
    }

}


Creating a WebHttpBinding endpoint into your WCF service. 

If you get a "The Address property on ChannelFactory.Endpoint was null." exception, make sure to add a "behaviorConfiguration" property to your endpoint.  This value should point to a custom defined "endpointBehavior".

 (App.config)

<system.serviceModel>
    <services>
         <service behaviorConfiguration="Default" name="TestNamespace.ServiceType">
            <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="TestNamespace.ServiceContract" />
            <host>
                <baseAddresses>
                    <add
baseAddress="http://localhost:8080/testservice" />
                </baseAddresses>
            </host>
        </service>
    </services>
    <behaviors>

        <endpointBehaviors>
            <behavior
name="webBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>

        <serviceBehaviors>

            <behavior name="Default">
                <serviceMetadata httpGetEnabled="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

 


Using your browser to point to http://localhost:8080/testservice/date/1995/10/10 , the result will look like this:

testservice 


Moving futher ..... If you would like to add this running service into another application like a Web application, use a WebChannelFactory in your application.

 

WebChannelFactory<IContractName> cf = new WebChannelFactory<IContractName>(new Uri("http://localhost:8080/testservice"));
ServiceType channel = cf.CreateChannel();
string testDate = channel.GetDate("1995","10","10");

Published Wednesday, October 08, 2008 1:23 PM by Kiyoshi
Filed under:

Comments

# re: WCF - Using WebHttpBinding for REST services

Wednesday, October 20, 2010 11:26 AM by hsj

Thanks, that was actually really helpful!

# re: WCF - Using WebHttpBinding for REST services

Sunday, January 02, 2011 8:09 PM by Dwayne

Do you have sample code that works? I realize this is just an example, but the code does not match that config or something else is missing from the puzzle.

# re: WCF - Using WebHttpBinding for REST services

Wednesday, December 21, 2011 5:17 AM by Nilesh

thanks a lot, it was really helpful

# re: WCF - Using WebHttpBinding for REST services

Tuesday, January 24, 2012 7:35 AM by fsdfds@jld.com

All bag quality problems can return, to support round-trip courier, freight from the love of two-way networks for bags yet, no quality problems due to personal reasons

customers may also wish to Return the date of receipt no reason to return within 7 days . Getting goods to you really worry! Receiving convenient

# re: WCF - Using WebHttpBinding for REST services

Friday, February 10, 2012 8:33 PM by 561r@561r.com

possessions at the same time. The way to do that is with a neighborhood watch. If you discover that you have moved into a neighborhood without a watch program, contact law enforcement and get one going.

Leave a Comment

(required) 
(required) 
(optional)
(required)