IPersonRetriever:

/*
* 由SharpDevelop创建。
* 用户: Administrator
* 日期: 2017/6/2
* 时间: 22:13
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text; namespace WcfRESTful
{
/// <summary>
/// Description of IPersonRetriever.
/// </summary>
[ServiceContract]
public interface IPersonRetriever
{
[OperationContract]
[WebInvokeAttribute(UriTemplate = "Persons",Method="POST", ResponseFormat = WebMessageFormat.Json)]
Person GetPerson();
} [DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Birthday { get; set; }
}
}

PersonRetriever:

/*
* 由SharpDevelop创建。
* 用户: Administrator
* 日期: 2017/6/2
* 时间: 22:15
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.ServiceModel.Web; namespace WcfRESTful
{
/// <summary>
/// Description of PersonRetriever.
/// </summary>
public class PersonRetriever: IPersonRetriever
{
public Person GetPerson()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new Person { Name = "Test", Age = 22, Birthday = DateTime.Now.ToString("yyyy-mm-dd HH:MM:ss:ffff") };
}
}
}

Program :

/*
* 由SharpDevelop创建。
* 用户: Administrator
* 日期: 2017/6/2
* 时间: 22:19
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.ServiceModel;
using System.ServiceModel.Description; namespace WcfRESTful
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!"); // TODO: Implement Functionality Here Uri baseAddress = new Uri("http://127.0.0.1:9998/PersonRetriever");
using (ServiceHost host = new ServiceHost(typeof(PersonRetriever), baseAddress)) {
WebHttpBinding binding = new WebHttpBinding();
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IPersonRetriever), binding, baseAddress);
WebHttpBehavior httpBehavior = new WebHttpBehavior();
endpoint.Behaviors.Add(httpBehavior);
host.Opened += delegate {
Console.WriteLine("Hosted successfully.");
};
host.Open();
Console.ReadLine();
} Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}

截图 :

源码: http://files.cnblogs.com/files/Areas/WcfRESTful.zip

最新文章

  1. 计算机程序的思维逻辑 (38) - 剖析ArrayList
  2. Tomcat 日志清除(含扩展AccessLogValve)
  3. angular学习笔记(二十八-附1)-$resource中的资源的方法
  4. loadrunner11遇到的问题汇总及相应的解决方案(持续更新)
  5. 20135316王剑桥 linux第十二周课实验笔记
  6. JavaWeb 7 Servlet
  7. $where $options: &#39;g&#39;,&#39;i&#39;
  8. android ListView_新闻案例
  9. 【转】JAVA程序中Float和Double精度丢失问题
  10. python3--(变量)
  11. MYSQL数据库学习十七 日志管理
  12. [UOJ UNR#1]奇怪的线段树
  13. mysql配置文件参数详解
  14. mysql数据的基本操作
  15. Ubuntu 16.04.4 LTS + Ambari 2.6.1.5 + HDP 2.6.4.0 安装部署
  16. .NET BackgroundWorker的一般使用方式
  17. php字符串 统计个数
  18. 大数加法java版
  19. http-server
  20. linux通过wget直接下载jdk

热门文章

  1. PHP中new static()与new self()的区别异同分析
  2. JSP展示两位小数
  3. requirejs amd module load example
  4. 新版本的bettercap不好用, 如何安装和编译旧版本的bettercap
  5. 人人网框架导入uidGenerator的ID生成方式
  6. Android之Wifi学习(1)
  7. TRAC-IK机器人运动学求解器
  8. Xcode10.1 import头文件无法索引
  9. android Glide简单使用
  10. go 源码学习之---Tail 源码分析