ServiceStack是一個高性能的.NET Web Service 平台,能夠簡化開發高性能的REST (支持JSON,XML,JSV,HTML,MsgPack,ProtoBuf,CSV等消息格式)以及WCF SOAP風格的WebService。在內部實現上ServiceStack建立在原生的ASP.NET IHttpHandler之上,允許在.NET Framework和Mono之上。
本文針對ServiceStack框架在.NET程序中的使用做簡單介紹。
1.新建.net 4.5的web 空項目,然后用nuget命令來安裝ServiceStack
Install-Package ServiceStack -Version 5.0.2
或者直接安裝最新版本:
Install-Package ServiceStack
(.net版本必須大於等於4.5版本,4.0項目會安裝不成功)
2.在工程中新建model和service文件夾
創建請求和響應實體
namespace UserService.ServiceModel { #region 請求實體 [Route("/GetByNameUserInfo","Get,Post")] /// <summary> /// 通過姓名獲取資料 /// </summary> public class GetUserInfoByNameRequest : IReturn<UserInfo> { public string Name { get; set; } }
#endregion
//定義實體類 public class UserInfo { public string Name { get; set; } public int Age { get; set; } public string Phone { get; set; } } }
(因為ServiceStack是基於請求參數來定義請求路由的,所以關鍵的是請求參數一定要定義好,同時可以在請求參數上自定義路由名和請求方式,作為對外接口名。)
3.在service文件夾創建服務接口
namespace UserService.ServiceInterface { /// <summary> /// 接口 /// </summary> public interface IUserInfoService { UserInfo Post(GetUserInfoByNameRequest request); } /// <summary> /// 實現接口 /// </summary> public class UserInfoService : ServiceStack.Service,IUserInfoService { static UserInfoService() { //構造方法,可以用來實現數據庫連接操作 } //實現服務接口 public UserInfo Post(GetUserInfoByNameRequest request) {
UserInfo userInfo = new UserInfo(); return userInfo; } } }
4:新建全局應用程序類命名 Global.asax 到本項目(初始化服務)
public class Global : System.Web.HttpApplication { public class MyAppHost : ServiceStack.AppHostBase { // 基礎構造函數需要一個名稱和程序集,其中包含Web服務實現(自己定義的服務接口) public MyAppHost() : base("UserService.ServiceInterface", typeof(ServiceInterface.UserInfoService).Assembly) {} public override void Configure(Funq.Container container) { // 此方法應初始化Web服務類使用的任何IoC資源。 } } protected void Application_Start(object sender, EventArgs e) { new AppHost().Init(); } }
或者單獨創建AppHost.cs和Global.asax,內容分別為:
public class AppHost : AppHostBase { /// <summary> /// 基礎構造函數需要一個名稱和程序集,其中包含Web服務實現(自己定義的服務接口) /// </summary> public AppHost() : base("UserService.ServiceInterface", typeof(ServiceInterface.UserInfoService).Assembly) {} /// <summary> /// Application specific configuration /// 此方法應初始化Web服務類使用的任何IoC資源。 /// </summary> public override void Configure(Container container) { } }
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { new AppHost().Init(); } }
5:修改web.config配置,新增system.webServer節點
<configuration> <!--必須添加下面配置--> <!--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>
6.結果展示
直接在Visual Studio中點擊調試,或者將站點發布在IIS中,運行后可以看到如下界面,至此一個簡單的基於ServiceStack框架的web服務就創建完成了:

7.其他說明
ServiceStack中的服務方法名為Any,Get以及Post,這也是ServiceStack支持的請求類型,Any表示服務可以通過HTTP Get和HTTP Post兩種方式調用。這強化和簡化了RESTFull風格的WebService的實現。只需要在這些方法上添加[Route(…)]屬性即可。
在get請求參數后面加上format參數可以返回響應的類型,例如:format=json、format=xml等。
免費的ServiceStack服務最多包含10個接口,並且每小時的訪問次數被限制在6000此以內。(當前版本為5.4.0)
ServiceStack調用服務接口的示例:
http://127.0.0.1:8033/json/reply/GetUserInfoByNameRequest
8.開發遇到的問題
ServiceStack搭建過程中的問題:在HTML頁面中不能獲得返回值,此時需要設置請求頭。可以直接在web.config中設置,或者部署站點時在IIS中設置三個請求頭。
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> <add name="Access-Control-Allow-Methods" value="POST, GET, PUT, DELETE, OPTIONS" /> </customHeaders> </httpProtocol>
原文地址:https://www.cnblogs.com/imdeveloper/p/10126874.html
轉載請注明出處,謝謝!
