您的位置:首页 > 其它

那一年wcf Rest第一讲

2012-08-22 20:19 106 查看
今天终于勇敢的辞职,虽然非常不舍,但我还是选择了,为了自己想做服务端开发而牺牲现在的工作,很久没有学习wcf了,重新拿起!

以后的路还是做java和.net,.net的东西已经很多了,不过WPF和silverlight可以先放下,其实一直放下啦,(silverlight预计在2014年将放弃,这个我也是看新闻得来的

不过新闻嘛,只是新闻)。先把.Net里面的以前的东西复习一下,模糊了!如:WCF,socket,MVC2和MVC3,微软的东西确实更新很快,VS2012工具和.net 4.5、C#5.0都出来了!

java某一天我再用你!好了,先做事

WCF服务:

通常讲,WCF服务由三个部分构成:

服务类:包含服务契约、操作契约和数据契约的定义和实现;

宿主:一种应用程序域和进程,服务将在该环境中运行;

终结点:由客户端用于访问服务;

介绍:

REpresentational state Transfer(REST)是一种架构原则,其中将web服务视为资源,可以由其URL唯一标识。

REstful Web服务的关键特点是明确使用HTTP方法来表示不同的操作调用:

Rest的基本设计原则对典型CRUD使用HTTP协议方法:
POST-创建资源
GET-检索资源
PUT-更新资源
DELETE-删除资源

REST是一个架构风格而不是一个预先定义的构建Web服务的方式,你可以使用任何合适的技术实现REST。

REST的关键点在于它描述了一个无状态的,遵循与WWW相似的架构。举例来说,AdventureWorks提供客户和销售信息数据的访问,
把每个客户或订单的详细信息作为单个的资源。从AdventureWorks网站获取所有客户列表,数据可以按照多种格式返回,

但是最常见的模式包括XML和JSON及Bit类型。

实现一个操作客户信息的Demo

对了,对于WCF4.0用Rest很方便,封装得很好,基本上只要创建一个项目就好了,不过大伙还是看看它的底层和用到的对像吧!

1.元数据模型,这里可直接用ado ef里面的对像

2.创建服务契约 [ServiceContract]
public interface IUserInfoService
{
//[OperationContract]
//string GetData(int value);
//[OperationContract]
//CompositeType GetDataUsingDataContract(CompositeType composite);

/***************************第一种方式传对像*****************************/
[WebInvoke(UriTemplate = "/", Method = "POST")]
int Create(UserInfo pUserInfo);

[WebInvoke(UriTemplate = "/", Method = "PUT")]
void Update(UserInfo pUserInfo);

[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
void Delete(string pUserInfo);

[WebGet(UriTemplate = "all")]
IEnumerable<UserInfo> GetAll();

[WebGet(UriTemplate = "{id}")]
UserInfo Get(string id);

/****************************第二种方式*****************************/
[OperationContract]
[WebGet(UriTemplate = "UserInfo/{userId}/")]
ICollection<UserInfo> GetUserInfoList(string pUserId);

/*UriTemplate用于服务操作的统一资源标识符 (URI) 模板。(路由,就像请求网页的URL)*/
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "UserInfo?UserId={pUserId}&UserName={pUsername}&UserPass={pUserPass}")]
int CreateCustomer(int pUserId, string pUserName, string pUserPass);

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "UserInfo/{UserId}?UserName={pUsername}&UserPass={pUserPass}")]
void UpdateUserInfo(int pUserId, string pUserName, string pUserPass);

[OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate = "UserInfo/{pUserId}")]
void DeleteUserInfo(string pUserId);
}3.UserInfoService : IUserInfoService 实现接口
public class UserInfoService : IUserInfoService
{
//把这个当数据源了,没有访问数据库 UserInfoServiceInterfase.UserInfoService
private static IList<UserInfo> gUserInfo1 = new List<UserInfo>
{
new UserInfo{ UserId = 1001, UserName="张三1", UserPass="123456"},
new UserInfo{ UserId = 1002, UserName="李四1", UserPass="123456"}
};

//把这个当数据源了,没有访问数据库
private static IList<UserInfo> gUserInfo2 = new List<UserInfo>
{
new UserInfo{ UserId = 10012, UserName="张三2", UserPass="1234562"},
new UserInfo{ UserId = 10021, UserName="李四2", UserPass="1234562"}
};

public int Create(UserInfo pUserInfo)
{
UserInfo vUserInfo = pUserInfo;
gUserInfo1.Add(vUserInfo);
return 1;
}

public void Update(UserInfo pUserInfo)
{
for (int i = 0; i < gUserInfo1.Count; i++)
{
UserInfo vUserInfo = gUserInfo1[i];
if (vUserInfo.UserId == pUserInfo.UserId)
{
vUserInfo = pUserInfo;
gUserInfo1.RemoveAt(i);
gUserInfo1.Add(vUserInfo);
}
}
}

public void Delete(string pUserId)
{
throw new NotImplementedException();
}

public IList<UserInfo> GetAll()
{
return gUserInfo2;
}

public UserInfo Get(string id)
{
UserInfo vUserInfo = gUserInfo2.FirstOrDefault(e => e.UserId == int.Parse(id));
if (null == vUserInfo)
{
}
return vUserInfo;
}

public IList<UserInfo> GetUserInfoList()
{
return gUserInfo2;
}

public int CreateUserInfo(string pUserId, string pUserName, string pUserPass)
{
UserInfo vUserInfo = new UserInfo();
vUserInfo.UserId = int.Parse(pUserId);
vUserInfo.UserName = pUserName;
vUserInfo.UserPass = pUserPass;
gUserInfo2.Add(vUserInfo);
return 1;
}

public void UpdateUserInfo(string pUserId, string pUserName, string pUserPass)
{
throw new NotImplementedException();
}

public void DeleteUserInfo(string pUserId)
{
UserInfo vUserInfo = this.Get(pUserId);
gUserInfo2.Remove(vUserInfo);
}

}

4.编写host 宿主程序

static void Main(string[] args)
{
WebServiceHost vhost = new WebServiceHost(typeof(UserInfoServiceInterfase.UserInfoService));
vhost.Open();//打开host
Console.WriteLine("Service running");
Console.WriteLine("Press ENTER to stop the service");
Console.ReadLine();
vhost.Close();

}host config文件
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="">
<webHttp helpEnabled="true" faultExceptionEnabled="false" />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="UserInfoServiceInterfase.UserInfoService">
<endpoint address="http://localhost:8000/ShowUserInfo" binding="webHttpBinding"
bindingConfiguration="" contract="UserInfoServiceInterfase.IUserInfoService" />
</service>
</services>
</system.serviceModel>

</configuration>

5.编写client程序

//第一种代理方法
//using (ChannelFactory<IUserInfoService> channelFactory = new ChannelFactory<IUserInfoService>("UserInfoService"))
//{
// //
// IUserInfoService proxy = channelFactory.CreateChannel();
// proxy.GetAll();
// //proxy.
// proxy.CreateUserInfo("c","b","a");

//}

//第二种代理方法
UserInfoProxy vUserInfoProxy = new UserInfoProxy();
vUserInfoProxy.GetAll();

Console.ReadLine();
UserInfoProxy文件
class UserInfoProxy : ClientBase<IUserInfoService>, IUserInfoService
{
public IList<UserInfo> GetAll()
{
return this.Channel.GetAll();
}

public int Create(UserInfo pUserInfo)
{
throw new NotImplementedException();
}

public void Update(UserInfo pUserInfo)
{
throw new NotImplementedException();
}

public void Delete(string pUserId)
{
throw new NotImplementedException();
}

public UserInfo Get(string id)
{
throw new NotImplementedException();
}

public IList<UserInfo> GetUserInfoList()
{
throw new NotImplementedException();
}

public int CreateUserInfo(string pUserId, string pUserName, string pUserPass)
{
throw new NotImplementedException();
}

public void UpdateUserInfo(string pUserId, string pUserName, string pUserPass)
{
throw new NotImplementedException();
}

public void DeleteUserInfo(string pUserId)
{
throw new NotImplementedException();
}
}


client config文件
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint name="UserInfoService" address="http://localhost:8000/ShowUserInfo" binding="webHttpBinding"
bindingConfiguration="" contract="UserInfoServiceInterfase.IUserInfoService" />
</client>
</system.serviceModel>
</configuration>

源代码下载:

加QQ群:55877861
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: