前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RESTful WCF

RESTful WCF

作者头像
张善友
发布2018-01-29 11:08:14
7580
发布2018-01-29 11:08:14
举报
文章被收录于专栏:张善友的专栏张善友的专栏

相较 WCF、WebService 使用 SOAP、WSDL、WS-* 而言,几乎所有的语言和网络平台都支持 HTTP 请求。我们无需去实现复杂的客户端代理,无需使用复杂的数据通讯方式既可以将我们的服务暴露给任何需要的人,无论他使用 VB、Ruby、JavaScript,甚至是 HTML FORM,或者直接在浏览器地址栏输入。

WCF 3.5 引入了 WebGetAttribute、WebInvokeAttribute、UriTemplate 来增加对 REST 的支持,这使得我们用很简单的方式就可以实现 RESTful WCF Service。

可参考以下几篇文章:

《深入浅出REST》 : 作者 Stefan Tilkov译者 苑永凯

《Web 编程模型》 : MSDN文档 《使用 WCF 和 .NET Framework 3.5 进行 HTTP 编程》 : Justin Smith

Twitter WCF Client

下面我们来看一个简单的例子:

[ServiceContract]
public interface IService
{
     [OperationContract]
     [WebGet]
     string EchoWithGet(string s); 
     [OperationContract]
     [WebInvoke]
     string EchoWithPost(string s);
} 
public class Service : IService
    {
        public string EchoWithGet(string s)
        {
            return "You said " + s;
        } 
        public string EchoWithPost(string s)
        {
            return "You said " + s;
        }
    } 
static void Main(string[] args)
       {
           AppDomain.CreateDomain("Server").DoCallBack(delegate
           {
               ServiceHost host = new ServiceHost(typeof(Service),new Uri("http://localhost:8020"));
               host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Soap");
               ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "Web");
               endpoint.Behaviors.Add(new WebHttpBehavior()); 
               host.Open();
           });
           using (WebChannelFactory<IService> wcf = new WebChannelFactory<IService>(new Uri("http://localhost:8020/Web")))
           {
               IService channel = wcf.CreateChannel(); 
               string s; 
               Console.WriteLine("Calling EchoWithGet by HTTP GET: ");
               s = channel.EchoWithGet("Hello, world");
               Console.WriteLine("   Output: {0}", s); 
               Console.WriteLine("");
               Console.WriteLine("This can also be accomplished by navigating to");
               Console.WriteLine("http://localhost:8020/EchoWithGet?s=Hello, world!");
               Console.WriteLine("in a web browser while this sample is running."); 
               Console.WriteLine(""); 
               Console.WriteLine("Calling EchoWithPost by HTTP POST: ");
               s = channel.EchoWithPost("Hello, world");
               Console.WriteLine("   Output: {0}", s);
           }
           Console.ReadLine();
       }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2008-12-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档