WCF - Using WebHttpBinding for REST services

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:

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");

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-05-05 Anti-money laundering反洗钱
2020-05-05 如何阅读git手册,方括号和尖括号的区别
2018-05-05 我最在行 英语
2015-05-05 C#编码标准
2015-05-05 面向对象设计Object Oriented Design
点击右上角即可分享
微信分享提示