快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

葡京娱乐场968119_龟发之家论坛



(一)web.config文件

要创建REST WCF Service,endpoint binding必要用webHttpBinding,拜见http://www.fengfly.com/plus/view-215497-1.html。

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葡京娱乐场968119>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>

在http://www.fengfly.com/plus/view-215496-1.html中的web.config,由于必要AJAX,endpointBehaviors用了,然则enableWebScript 和REST必要的UriTemplate是有冲突的,以是这里不再应用。

endpointBehaviors中设置可以天生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

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 o葡京娱乐场968119peration 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={葡京娱乐场968119id}", 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葡京娱乐场968119 new Employee() { Id = new Guid(id), Name = "Neil Klugman", Birthdate = new DateTime(1930, 1, 1) };}

}}

(三)webHttpBinding JSON款式

将每个措施的ResponseFormat改为Json

CostService.svc.cs

using System;

using System.ServiceModel;using System.ServiceModel.Activation;

using System.ServiceModel.Web;

namespace SandwichServices{

[ServiceContract(Namespace = "Sandwich葡京娱乐场968119Services")][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不要用

endpointBehaviors中设置可以天生WCF Service的Help页面

GET(查),POST(改),PUT(增),DELETE(删)

对付PUT和DELETE,必要身份验证信息

webHttpBinding的数据款式有两种:XML和JSON,可以经由过程ResponseFormat来设置

您可能还会对下面的文章感兴趣: