ServiceStack的Hello创建
1.在vs中创建一个ASP.NET 空项目
2.用NuGet安装ServiceStack组件
3.创建AppHost类

using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Web; using ServiceStack; using ServiceStackWebApplication.Services; using ServiceStackWebApplication.Requests; public class AppHost : AppHostBase { public AppHost() : base("StarterTemplate ASP.NET HOST", typeof(HelloService).Assembly) { } public override void Configure(Funq.Container container) { } }
4.创建请求类以及相应类和服务类

using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ServiceStackWebApplication.Requests { public class Hello { public string Name { get; set; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Web; using ServiceStack; namespace ServiceStackWebApplication.Responses { public class HelloResponse { public string Result { get; set; } public ResponseStatus ResponseStatus { get; set; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Web; using ServiceStack; using ServiceStackWebApplication.Requests; using ServiceStackWebApplication.Responses; namespace ServiceStackWebApplication.Services { public class HelloService:Service { public object Any(Hello request) { return new HelloResponse { Result = "Hello" + request.Name }; } } }
5.启动服务,创建全局配置文件global

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; namespace ServiceStackWebApplication { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { new AppHost().Init(); } protected void Session_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { } } }
6.整个项目结构
7.在web.config中添加

<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>
8.配置好IIS,访问结果:
通过json访问服务接口: