目录
ServiceStack 概念
ServiceStack Web Service 创建与调用简单示列
上篇文章介绍了ServiceStack是什么,本章进入主题,如何快速简单的搭建ServiceStack服务环境和调用。
ServiceStack服务搭建
服务的搭建有两种方法,我会一一介绍。
第一种方法如下:
1.创建网站项目
[flash=500,400]http://s1.knowsky.com/20160211/way14frl1as44.png[/flash]
2.安装发布组件
[flash=500,400]http://s1.knowsky.com/20160211/ww3iwpbio3d45.png[/flash]
[flash=500,400]http://s1.knowsky.com/20160211/yckfd4zi1jz45.png[/flash]
[flash=500,400]http://s1.knowsky.com/20160211/grrlliuj5lc45.png[/flash]
packages.confing文件为您提供每一个ServiceStack dll 的版本和运行时信息。
定义是 ServiceStack <Ticket> 请求数据契约
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingServiceStack;namespaceModel
{///<summary>///创建票据 请求实体///</summary>publicclassTicketRequest : IReturn<TicketResponse>{publicintTicketId {get;set; }publicintTableNumber {get;set; }publicintServerId {get;set; }
}publicclassTicketDeleteRequest
{publicintTicketId {get;set; }
}
}
定义ServiceStack <Ticket> 响应数据契约
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceModel
{///<summary>///创建票据响应数据实体///</summary>publicclassTicketResponse
{publicintTicketId {get;set; }publicintTableNumber {get;set; }publicintServerId {get;set; }publicList<OrderResponse> Orders {get;set; }publicDateTime Timestamp {get;set; }
}///<summary>///票据所属订单///</summary>publicclassOrderResponse
{publicintOrderId {get;set; }
}
}
定义ServiceStack 接口方法
usingModel;usingSystem.Collections.Generic;namespaceInterface
{publicinterfaceITicketService
{///<summary>///方法请求模式和路由规则 后面再说///请求票据列表///</summary>///<param name="request"></param>///<returns></returns>List<TicketResponse>Any(TicketRequest request);///<summary>///删除指定票据///</summary>///<param name="request"></param>///<returns></returns>boolPOST(TicketDeleteRequest deleteid);
}
}
实现ServiceStack 接口方法
usingInterface;usingModel;usingSystem;usingSystem.Collections.Generic;namespaceService
{publicclassTicketService : ServiceStack.Service, ITicketService
{///<summary>///方法请求模式和路由规则 后面再说///请求票据列表///</summary>///<param name="request"></param>///<returns></returns>publicList<TicketResponse>Any(TicketRequest request)
{
List<TicketResponse> result =newList<TicketResponse>();
List<OrderResponse> orderlist =newList<OrderResponse>();
orderlist.Add(newOrderResponse() { OrderId =1});
result.Add(newTicketResponse()
{
Orders=orderlist,
ServerId=1,
TableNumber=1,
TicketId=1,
Timestamp=DateTime.Now
});
result.Add(newTicketResponse()
{
Orders=orderlist,
ServerId=2,
TableNumber=2,
TicketId=2,
Timestamp=DateTime.Now
});returnresult;
}///<summary>///删除指定票据///</summary>///<param name="request"></param>///<returns></returns>publicboolPOST(TicketDeleteRequest deleteid)
{returntrue;
}
}
}
实现ServiceStack服务宿主
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceService.Host
{publicclassTickServiceHost : ServiceStack.AppHostBase
{//Register your Web service with ServiceStack.publicTickServiceHost()
:base("Ticket Service",typeof(Service.TicketService).Assembly) { }publicoverridevoidConfigure(Funq.Container container)
{//Register any dependencies your services use here.}
}
}
启动ServiceStack服务宿主
<%@applicationLanguage="C#"%>
<script runat="server">voidApplication_Start(objectsender, EventArgs e)
{//在应用程序启动时运行的代码newService.Host.TickServiceHost().Init();
}voidApplication_End(objectsender, EventArgs e)
{//在应用程序关闭时运行的代码}voidApplication_Error(objectsender, EventArgs e)
{//在出现未处理的错误时运行的代码}voidsession_Start(objectsender, EventArgs e)
{//在新会话启动时运行的代码}voidSession_End(objectsender, EventArgs e)
{//在会话结束时运行的代码。//注意: 只有在 Web.config 文件中的 sessionstate 模式设置为//InPRoc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer//或 SQLServer,则不引发该事件。}</script>
添加ServiceStack网站集成配置
<?xmlversion="1.0"encoding="utf-8"?>
<!--有关如何配置asp.net应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433-->
<configuration>
<system.web>
<compilation debug="true"targetFramework="4.0"/>
</system.web>
<!--RequiredforIIS7(and above) -->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<add path="*.aspx"name="DefaultHttpApplication"type="System.Web.UI.PageHandlerFactory"verb="*"/>
<add path="*"name="ServiceStack.Factory"type="ServiceStack.HttpHandlerFactory, ServiceStack"verb="*"preCondition="integratedMode"resourceType="Unspecified"allowPathInfo="true"/>
</handlers>
</system.webServer>
</configuration>
至此ServiceStack服务已经完成,运行程序可以看到界面如下,其中TicketRequest和TicketDeleteRequest可以理解成实际的调用方法,里面定义的参数可以理解成方法的请求参数,后续调用会继续讲解
[flash=500,400]http://s1.knowsky.com/20160211/ac1oe0tnqdd45.png[/flash]
整个项目解决方案的框架如下:
[flash=500,400]http://s1.knowsky.com/20160211/4hpfvbexcd245.png[/flash]
第二种创建方法比较简单,快速,利用ServiceStack官方提供的模板直接生成,具体操作方法如下:
1.打开VS,工具扩展和更新
[flash=500,400]http://s1.knowsky.com/20160211/azdvl1kczic45.png[/flash]
2.搜索servicestack提供的组件
[flash=500,400]http://s1.knowsky.com/20160211/t3mt3btkjg046.png[/flash]
3.安装完成后创建新项目,来自模板
[flash=500,400]http://s1.knowsky.com/20160211/dtzq4da12e346.png[/flash]
创建完成后的项目框架如下
[flash=500,400]http://s1.knowsky.com/20160211/qyzxp5pueib46.png[/flash]
5.改成我们所需要的
[flash=500,400]http://s1.knowsky.com/20160211/1gj41dlstkm46.png[/flash]
ServiceStack客户端调用
usingConsoleApplication1.model;usingServiceStack;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1
{classProgram
{staticvoidMain(string[] args)
{//客户端采用json数据格式JsonServiceClient client1 =newJsonServiceClient("http://localhost:32617/");
Console.WriteLine("http://localhost:32617/");//TicketRequest 为请求的方法List<TicketResponse> responseSearch = client1.Get<List<TicketResponse>>(newTicketRequest() { TicketId =1});
Console.WriteLine(responseSearch.Count);boolresponseDelete = client1.Post<bool>(newTicketDeleteRequest() { TicketId =1});
Console.WriteLine(responseDelete);
Console.WriteLine("http://localhost:36107/");
List<TicketResponse> responseSearch1 = client1.Get<List<TicketResponse>>(newTicketRequest() { TicketId =1});
Console.WriteLine(responseSearch1.Count);boolresponseDelete1 = client1.Post<bool>(newTicketDeleteRequest() { TicketId =1});
Console.WriteLine(responseDelete1);
Console.ReadLine();
}
}
}
[flash=500,400]http://s1.knowsky.com/20160211/uxzfbjhpjpc47.png[/flash]
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1.model
{///<summary>///创建票据 请求实体///IReturn<TicketResponse>指该方法返回的数据实体///</summary>publicclassTicketRequest
{publicintTicketId {get;set; }publicintTableNumber {get;set; }publicintServerId {get;set; }
}publicclassTicketDeleteRequest
{publicintTicketId {get;set; }
}
}
浏览器端调用:
[flash=500,400]http://s1.knowsky.com/20160211/ynnqpiwug2m47.png[/flash]
请求URL如下:
[flash=500,400]http://s1.knowsky.com/20160211/iywhmrlt51q47.png[/flash]
浏览器输入http://localhost:36107/json/reply/TicketRequest?TicketId=1
[flash=500,400]http://s1.knowsky.com/20160211/h1sisvm0rth47.png[/flash]
浏览器默认请求为GET,如果方法定义POST请求类型,可以采用第三方模拟Rest请求工具
[flash=500,400]http://s1.knowsky.com/20160211/auoitjzimx247.png[/flash]
参考:
https://servicestack.net/features
https://msdn.microsoft.com/zh-cn/magazine/dn342871.aspx
http://www.cnblogs.com/shanyou/p/3348347.html
http://www.cnblogs.com/yangecnu/p/Introduce-ServiceStack.html
作者:释迦苦僧 出处:http://www.cnblogs.com/woxpp/p/5012947.html 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
本文为新手试写,如果有问题,欢迎博友指正,在下不甚感激,后续还会继续更新ServiceStack其他相关文章。