目錄html
ServiceStack Web Service 建立與調用簡單示列 json
上篇文章介紹了ServiceStack是什麼,本章進入主題,如何快速簡單的搭建ServiceStack服務環境和調用。瀏覽器
ServiceStack服務搭建session
服務的搭建有兩種方法,我會一一介紹。框架
第一種方法以下:ide
1.建立網站項目工具
2.安裝發佈組件網站
packages.confing文件爲您提供每個ServiceStack dll 的版本和運行時信息。ui
定義是 ServiceStack <Ticket> 請求數據契約
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceStack; namespace Model { /// <summary> /// 建立票據 請求實體 /// </summary> public class TicketRequest : IReturn<TicketResponse> { public int TicketId { get; set; } public int TableNumber { get; set; } public int ServerId { get; set; } } public class TicketDeleteRequest { public int TicketId { get; set; } } }
定義ServiceStack <Ticket> 響應數據契約
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Model { /// <summary> /// 建立票據響應數據實體 /// </summary> public class TicketResponse { public int TicketId { get; set; } public int TableNumber { get; set; } public int ServerId { get; set; } public List<OrderResponse> Orders { get; set; } public DateTime Timestamp { get; set; } } /// <summary> /// 票據所屬訂單 /// </summary> public class OrderResponse { public int OrderId { get; set; } } }
定義ServiceStack 接口方法
using Model; using System.Collections.Generic; namespace Interface { public interface ITicketService { /// <summary> /// 方法請求模式和路由規則 後面再說 /// 請求票據列表 /// </summary> /// <param name="request"></param> /// <returns></returns> List<TicketResponse> Any(TicketRequest request); /// <summary> /// 刪除指定票據 /// </summary> /// <param name="request"></param> /// <returns></returns> bool POST(TicketDeleteRequest deleteid); } }
實現ServiceStack 接口方法
using Interface; using Model; using System; using System.Collections.Generic; namespace Service { public class TicketService : ServiceStack.Service, ITicketService { /// <summary> /// 方法請求模式和路由規則 後面再說 /// 請求票據列表 /// </summary> /// <param name="request"></param> /// <returns></returns> public List<TicketResponse> Any(TicketRequest request) { List<TicketResponse> result = new List<TicketResponse>(); List<OrderResponse> orderlist = new List<OrderResponse>(); orderlist.Add(new OrderResponse() { OrderId = 1 }); result.Add(new TicketResponse() { Orders = orderlist, ServerId = 1, TableNumber = 1, TicketId = 1, Timestamp = DateTime.Now }); result.Add(new TicketResponse() { Orders = orderlist, ServerId = 2, TableNumber = 2, TicketId = 2, Timestamp = DateTime.Now }); return result; } /// <summary> /// 刪除指定票據 /// </summary> /// <param name="request"></param> /// <returns></returns> public bool POST(TicketDeleteRequest deleteid) { return true; } } }
實現ServiceStack服務宿主
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Service.Host { public class TickServiceHost : ServiceStack.AppHostBase { // Register your Web service with ServiceStack. public TickServiceHost() : base("Ticket Service", typeof(Service.TicketService).Assembly) { } public override void Configure(Funq.Container container) { // Register any dependencies your services use here. } } }
啓動ServiceStack服務宿主
<%@ Application Language="C#" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { // 在應用程序啓動時運行的代碼 new Service.Host.TickServiceHost().Init(); } void Application_End(object sender, EventArgs e) { // 在應用程序關閉時運行的代碼 } void Application_Error(object sender, EventArgs e) { // 在出現未處理的錯誤時運行的代碼 } void Session_Start(object sender, EventArgs e) { // 在新會話啓動時運行的代碼 } void Session_End(object sender, EventArgs e) { // 在會話結束時運行的代碼。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式設置爲 // InProc 時,纔會引起 Session_End 事件。若是會話模式設置爲 StateServer // 或 SQLServer,則不引起該事件。 } </script>
添加ServiceStack網站集成配置
<?xml version="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> <!--Required for IIS 7 (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能夠理解成實際的調用方法,裏面定義的參數能夠理解成方法的請求參數,後續調用會繼續講解
整個項目解決方案的框架以下:
第二種建立方法比較簡單,快速,利用ServiceStack官方提供的模板直接生成,具體操做方法以下:
1.打開VS,工具擴展和更新
2.搜索servicestack提供的組件
3.安裝完成後建立新項目,來自模板
建立完成後的項目框架以下
5.改爲咱們所須要的
ServiceStack客戶端調用
using ConsoleApplication1.model; using ServiceStack; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //客戶端採用json數據格式 JsonServiceClient client1 = new JsonServiceClient("http://localhost:32617/"); Console.WriteLine("http://localhost:32617/"); //TicketRequest 爲請求的方法 List<TicketResponse> responseSearch = client1.Get<List<TicketResponse>>(new TicketRequest() { TicketId = 1 }); Console.WriteLine(responseSearch.Count); bool responseDelete = client1.Post<bool>(new TicketDeleteRequest() { TicketId = 1 }); Console.WriteLine(responseDelete); Console.WriteLine("http://localhost:36107/"); List<TicketResponse> responseSearch1 = client1.Get<List<TicketResponse>>(new TicketRequest() { TicketId = 1 }); Console.WriteLine(responseSearch1.Count); bool responseDelete1 = client1.Post<bool>(new TicketDeleteRequest() { TicketId = 1 }); Console.WriteLine(responseDelete1); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1.model { /// <summary> /// 建立票據 請求實體 /// IReturn<TicketResponse> 指該方法返回的數據實體 /// </summary> public class TicketRequest { public int TicketId { get; set; } public int TableNumber { get; set; } public int ServerId { get; set; } } public class TicketDeleteRequest { public int TicketId { get; set; } } }
瀏覽器端調用:
請求URL以下:
瀏覽器輸入http://localhost:36107/json/reply/TicketRequest?TicketId=1
瀏覽器默認請求爲GET,若是方法定義POST請求類型,能夠採用第三方模擬Rest請求工具
參考:
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其餘相關文章。