国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何創建一個RESTful WCF Service

2019-11-17 01:49:34
字體:
來源:轉載
供稿:網友

如何創建一個RESTful WCF Service

原創地址:http://m.survivalescaperooms.com/jfzhu/p/4044813.html

轉載請注明出處

(一)web.config文件

要創建REST WCF Service,endpoint binding需要用webHttpBinding,參見《webHttpBinding、basicHttpBinding和wsHttpBinding的區別》。

web.config

<?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"            mult

在《如何創建一個Ajax-Enabled WCF Service》中的web.config,因為需要AJAX,endpointBehaviors用了<enableWebScript />,但是enableWebScript 和REST需要的UriTemplate是有沖突的,所以這里不再使用。

endpointBehaviors中設置<webHttp helpEnabled="true"/>可以生成WCF Service的Help頁面。

image

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

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

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

image

image

image

image

(三)webHttpBinding JSON格式

將每個方法的ResponseFormat改為Json

CostService.svc.cs

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

image

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聊城市| 南城县| 安西县| 高淳县| 体育| 都兰县| 措美县| 永丰县| 汽车| 鞍山市| 肥西县| 鲁甸县| 阳信县| 安义县| 澎湖县| 桐城市| 宁阳县| 漠河县| 同仁县| 德安县| 湟中县| 清远市| 平顺县| 金溪县| 卢湾区| 德钦县| 安丘市| 泸定县| 城固县| 军事| 尉犁县| 志丹县| 宜黄县| 溧阳市| 镇远县| 泉州市| 小金县| 兴化市| 余江县| 兴化市| 漠河县|