原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html
转载请注明出处
(一)web.config文件
要创建REST WCF Service,endpoint binding需要用webHttpBinding,参见《webHttpBinding、basicHttpBinding和wsHttpBinding的区别》。
web.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version= "1.0" ?> <configuration> <system.web> <compilation debug= "true" targetFramework= "4.0" /> <authentication mode= "None" /> </system.web> <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name= "SandwichServices.CostServiceBehavior" > <webHttp helpEnabled= "true" /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name= "SandwichServices.CostServiceServiceBehavior" > <serviceMetadata httpGetEnabled= "true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled= "true" multipleSiteBindingsEnabled= "true" /> <services> <service name= "SandwichServices.CostService" behaviorConfiguration= "SandwichServices.CostServiceServiceBehavior" > <endpoint address= "" behaviorConfiguration= "SandwichServices.CostServiceBehavior" binding= "webHttpBinding" contract= "SandwichServices.CostService" /> <endpoint contract= "IMetadataExchange" binding= "mexHttpBinding" address= "mex" /> </service> </services> </system.serviceModel> </configuration> |
在《如何创建一个AJAX-Enabled WCF Service》中的web.config,因为需要AJAX,endpointBehaviors用了<enableWebScript />,但是enableWebScript 和REST需要的UriTemplate是有冲突的,所以这里不再使用。
endpointBehaviors中设置<webHttp helpEnabled="true"/>可以生成WCF Service的Help页面。
HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE。一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查,改,增,删4个操作。GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。
对于PUT和DELETE,需要身份验证信息,所以我们先暂时只允许匿名访问,在web.config中将authentication mode设置为None。
(二)webHttpBinding的XML格式
Employee.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System; using System.Runtime.Serialization; namespace SandwichServices { [DataContract] public class Employee { private Guid id; private string name; private DateTime birthdate; [DataMember] public Guid Id { get { return id; } set { id = value; } } [DataMember] public string Name { get { return name; } set { name = value; } } [DataMember] public DateTime Birthdate { get { return birthdate; } set { birthdate = value; } } } } |
CostService.svc.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using System; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; namespace SandwichServices { [ServiceContract(Namespace = "SandwichServices" )] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class CostService { // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json) // To create an operation that returns XML, // add [WebGet(ResponseFormat=WebMessageFormat.Xml)], // and include the following line in the operation body: // WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; [OperationContract] [WebInvoke(Method = "PUT" , UriTemplate = "Employees/AddEmployee" , ResponseFormat = WebMessageFormat.Xml)] public Guid AddEmployee(Employee employee) { return Guid.NewGuid(); } [OperationContract] [WebInvoke(Method = "DELETE" , UriTemplate = "Employees/DeleteEmployee?id={id}" , ResponseFormat = WebMessageFormat.Xml)] public void DeleteEmployee( string id) { return ; } [OperationContract] [WebInvoke(Method = "POST" , UriTemplate = "Employees/UpdateEmployee" , ResponseFormat = WebMessageFormat.Xml)] public void UpdateEmployee(Employee employee) { return ; } [OperationContract] [WebGet(UriTemplate = "Employees/GetEmployee?id={id}" , ResponseFormat = WebMessageFormat.Xml)] public Employee GetEmployee( string id) { return new Employee() { Id = new Guid(id), Name = "Neil Klugman" , Birthdate = new DateTime(1930, 1, 1) }; } } } |
(三)webHttpBinding JSON格式
将每个方法的ResponseFormat改为Json
CostService.svc.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using System; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; namespace SandwichServices { [ServiceContract(Namespace = "SandwichServices" )] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class CostService { // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json) // To create an operation that returns XML, // add [WebGet(ResponseFormat=WebMessageFormat.Xml)], // and include the following line in the operation body: // WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; [OperationContract] [WebInvoke(Method = "PUT" , UriTemplate = "Employees/AddEmployee" , ResponseFormat=WebMessageFormat.Json)] public Guid AddEmployee(Employee employee) { return Guid.NewGuid(); } [OperationContract] [WebInvoke(Method = "DELETE" , UriTemplate = "Employees/DeleteEmployee?id={id}" , ResponseFormat = WebMessageFormat.Json)] public void DeleteEmployee( string id) { return ; } [OperationContract] [WebInvoke(Method = "POST" , UriTemplate = "Employees/UpdateEmployee" , ResponseFormat = WebMessageFormat.Json)] public void UpdateEmployee(Employee employee) { return ; } [OperationContract] [WebGet(UriTemplate = "Employees/GetEmployee?id={id}" , ResponseFormat = WebMessageFormat.Json)] public Employee GetEmployee( string id) { return new Employee() { Id = new Guid(id), Name = "Neil Klugman" , Birthdate = new DateTime(1930, 1, 1) }; } } } |
(四)总结
- RESTful WCF Service需要使用webHttpBinding
- endpointBehaviors不要用<enableWebScript />
- endpointBehaviors中设置<webHttp helpEnabled="true"/>可以生成WCF Service的Help页面
- GET(查),POST(改),PUT(增),DELETE(删)
- 对于PUT和DELETE,需要身份验证信息
- webHttpBinding的数据格式有两种:XML和JSON,可以通过ResponseFormat来设置
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2015-07-01 WebConfig配置文件详解