6.WCF Rest

契约:

注意:所有传入的参数必须设置成字符串

using System;
using System.Collections.Generic;
using System.ServiceModel;
using Rhythmk.Model;
using System.ServiceModel.Web;

namespace Rhythmk.Contracts
{
/// HTTP方法中:
/// PUT相当于Update
/// GET相当于Read
/// POST相当于Create
/// DELETE相当于Delete

[ServiceContract]
[DataContractFormat]
public interface IRest
{
// WebInvoke - 指示服务操作在逻辑上就是调用操作,而且可由 Web 编程模型调用
// UriTemplate - 用于服务操作的统一资源标识符 (URI) 模板。URI模板可以将一个 URI 或一组 URI 映射到服务操作。有关 URI 模板的更多信息,请参见 UriTemplate 和 UriTemplateTable
// Method - 与操作关联的协议方法,默认为 POST
// ResponseFormat - 指定从服务操作发出的响应的格式。Xml 或 Json
[OperationContract]
/* [WebInvoke(
UriTemplate="User/POST/{name}/{age}/{time}",
Method="POST",
ResponseFormat=WebMessageFormat.Json
)]
*/
[WebGet(UriTemplate
= "/User/POST/{name}/{age}/{time}", BodyStyle = WebMessageBodyStyle.Bare)]
User CreateUser(
string name, string age, string time);

[OperationContract]
/* [WebInvoke(
UriTemplate = "User/PUT/{name}/{age}",
Method="PUT",
ResponseFormat=WebMessageFormat.Json
)]
*/
[WebGet(UriTemplate
= "/User/PUT/{name}/{age}", BodyStyle = WebMessageBodyStyle.Bare)]

bool UpdateUser(string name, string age);

[OperationContract]
/* [WebInvoke(
UriTemplate = "User/DELETE/{name}",
Method="DELETE",
ResponseFormat=WebMessageFormat.Json
)]
*/
[WebGet(UriTemplate
= "/User/DELETE/{name}", BodyStyle = WebMessageBodyStyle.Bare)]

bool DelUser(string name);
[OperationContract]
/* [WebInvoke(
UriTemplate = "User/GET/{age}",
Method="GET",
ResponseFormat=WebMessageFormat.Json
)]
*/
[WebGet(UriTemplate
= "/User/GET/{age}", BodyStyle = WebMessageBodyStyle.Bare)]

List
<User> GetUserListByAge(string age);

}
}

服务:

using System;
using System.Collections.Generic;
using Rhythmk.Contracts;
using Rhythmk.Model;
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace Rhythmk.Services
{
[ServiceBehavior(InstanceContextMode
= InstanceContextMode.PerSession)]
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Required)]
public class RestService : IRest
{
#region IRest 成员

public User CreateUser(string name, string age, string time)
{
User user
= new User();
user.Name
= name;
user.Age
= Convert.ToInt32(age);
user.Birthday
=DateTime.Parse( time);
return user;
}

public bool UpdateUser(string name, string age)
{
return true;
}

public bool DelUser(string name)
{
return true;
}

public List<User> GetUserListByAge(string age)
{
List
<User> list = new List<User>();

for (int i =Convert.ToInt32( age); i <Convert.ToInt32(age) + 10; i++)
{
User user
= new User();
user.Age
= i;
user.Name
= "Name" + i;
user.Birthday
= DateTime.Now;
list.Add(user);
}
return list;

}

#endregion
}
}

数据契约:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;


namespace Rhythmk.Model
{

[DataContract]
public class User
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }

[DataMember]
public DateTime Birthday { get; set; }

}
}

配置:

<%@ ServiceHost Language="C#" Service="Rhythmk.Services.RestService" Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metaBehavior">

<serviceMetadata httpGetEnabled="true" />


</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webBinding">


</binding>
</webHttpBinding>
</bindings>
<services>
<service name="Rhythmk.Services.RestService" behaviorConfiguration="metaBehavior" >
<endpoint address=""
binding
="webHttpBinding" bindingConfiguration="" contract="Rhythmk.Contracts.IRest" >
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:5726/Host/RestService.svc"/>
</baseAddresses>
</host>
</service>
</services>

</system.serviceModel>
</configuration>

注意:绑定为:webHttpBinding 

调用:http://localhost:5726/Host/RestService.svc/User/GET/123

返回:

 

 

- <ArrayOfUser xmlns="http://schemas.datacontract.org/2004/07/Rhythmk.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
-
<User>
<Age>123</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name123</Name>
</User>
-
<User>
<Age>124</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name124</Name>
</User>
+
<User>
<Age>125</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name125</Name>
</User>
-
<User>
<Age>126</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name126</Name>
</User>
-
<User>
<Age>127</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name127</Name>
</User>
-
<User>
<Age>128</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name128</Name>
</User>
-
<User>
<Age>129</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name129</Name>
</User>
-
<User>
<Age>130</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name130</Name>
</User>
-
<User>
<Age>131</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name131</Name>
</User>
-
<User>
<Age>132</Age>
<Birthday>2011-06-09T10:01:54.6746286+08:00</Birthday>
<Name>Name132</Name>
</User>
</ArrayOfUser>



posted @ 2011-06-08 19:30  Rhythmk  阅读(279)  评论(0编辑  收藏  举报
Rhythmk 个人笔记