ServiceStack 简单服务搭建
1:定义数据实体
因为ServiceStack是基于请求参数来定义请求路由的,所以关键的是请求参数一定要定义好,同时可以在请求参数上自定义路由名和请求方式,作为对外接口名
上代码:
namespace ServiceStack_Moudel { public class Ticket { public int TicketId { get; set; } public string Name { get; set; } } [Route("/allticket","Get,Post")] public class GetAllTicketsInQueueRequest { public string name { get; set; } } [Route("/ticket","Post")] public class QueueTicketRequest { public string name2 { get; set; } } [Route("/pull","Get")] public class PullTicketRequest { public string name3 { get; set; } } }
2:接口实现
接口和实现类,这革不赘述了,其实我觉得简单项目的话,接口层是完全没有必要的,完全是冗余,注意的是要实现你的接口(如果有的话),继承Service(必须要,这是核心),请求和返回参数就用刚才定义的那些就可以,另外 接口名 Any表示什么请求方式都接受,也可以用Get或Post作为方法名,这样只会接受指定的请求方式,请求方式不对会抛异常
public class TicketService : Service, ITicketService { public List<Ticket> Any(GetAllTicketsInQueueRequest request) { var ticketList = new List<Ticket>(); for (int i = 0; i < 3; i++) { var ticket = new Ticket { Name = i.ToString(), TicketId = i + 1 }; ticketList.Add(ticket); } return ticketList; } public string Any(QueueTicketRequest request) { return "query ticket successfully!"; } public Ticket Any(PullTicketRequest request) { var ticket = new Ticket { Name = "pull successfully!", TicketId = 99 }; return ticket; } }
public interface ITicketService { List<Ticket> Any(GetAllTicketsInQueueRequest request); string Any(QueueTicketRequest request); Ticket Any(PullTicketRequest request); }
3:准备Host 部署
这边分两种场景部署,一是Host在控制台程序,另一种是IHost在IIS
3.1 Host 控制台
Host控制台 继承的是AppSelfHostBase 这个类
class Program { static void Main(string[] args) { var listeningOn = args.Length == 0 ? "http://*:1337/" : args[0]; var appHost = new TicketServiceHost() .Init() .Start(listeningOn); Console.WriteLine("AppHost Created at {0}, listening on {1}", DateTime.Now, listeningOn); Console.ReadKey(); } }
Host类
public class TicketServiceHost : AppSelfHostBase { /// <summary> /// Default constructor. /// Base constructor requires a name and assembly to locate web service classes. /// </summary> public TicketServiceHost() : base("Ticket Service", typeof(TicketService).Assembly) { } /// <summary> /// Application specific configuration /// This method should initialize any IoC resources utilized by your web service classes. /// </summary> /// <param name="container"></param> public override void Configure(Container container) { } }
运行控制台程序,然后打开localhost:1337就会看到下图,表明Host成功,服务可以使用了。
3.2 Host在IIS
Host在IIs 继承的是AppHostBase类
public class TicketServiceHost : AppHostBase { //Register your web service with ServiceStack. public TicketServiceHost() : base("Ticket Service", typeof(TicketService).Assembly) { } public override void Configure(Funq.Container container) { //Register any dependencies your services use here. } }
在Global.asax中 初始化服务
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { new TicketServiceHost().Init(); } }
结束后编译下,把这个Web项目部署在IIS上,直接启动就可以看到如下图,说明成功了
4:测试
调用地址就使用他推荐的方式就可以,或者使用我们自定义的别名也行,可以指定返回的数据格式,有以下几种方式,感兴趣的可以把json换成xml试试看
1: http://localhost:8083/json/reply/GetAllTicketsInQueueRequest?name=123456
2: http://localhost:8083/allticket.json?name=12345678
3: http://localhost:8083/allticket?name=123456&format=json
简单的总结了一下,也希望对大家有帮助
对了 差点忘了,一个非常重要的点,就是Host在IIS的话,webconfig一定要增加ServiceStack节点,我就是因为没有增加节点,导致一直Host不成功
<system.webServer> <handlers> <add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true"/> </handlers> </system.webServer>
增加这个节点就大功告成了!
5:附带项目结构