1: Define data entities
Because ServiceStack defines request routing based on request parameters, the key is that the request parameters must be well defined, and the routing name and request method can be customized on the request parameters as the external interface name
Above code:
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: Interface implementation
Interface and implementation class, I won't go into details here. In fact, I think that for simple projects, the interface layer is completely unnecessary and completely redundant. Note that you must implement your interface (if any) and inherit Service (must be Yes, this is the core), the request and return parameters are just those defined just now. In addition, the interface name Any means that any request method is accepted, and Get or Post can also be used as the method name, so that only the specified request method will be accepted. If the request method is incorrect, an exception will be thrown
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: Prepare Host Deployment
There are two scenarios for deployment here, one is Host in the console program, the other is IHost in IIS
3.1 Host Console
The Host console inherits the AppSelfHostBase class
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 class
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) { } }
Run the console program, then open localhost:1337 and you will see the following image, indicating that the Host is successful and the service is ready to use.
3.2 Host in IIS
Host inherits from the AppHostBase class in IIs
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. } }
Initialize the service in Global.asax
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { new TicketServiceHost().Init(); } }
After compiling, deploy the Web project on IIS, and start it directly, you can see the following picture, indicating success
4: Test
You can use the method recommended by him to call the address, or use our custom alias. You can specify the returned data format. There are the following methods. If you are interested, you can try to replace json with 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
A brief summary, and I hope it will help you
By the way, I almost forgot, a very important point is that if the Host is in IIS, the webconfig must add ServiceStack nodes. I just didn't add nodes, so the Host has been unsuccessful.
<system.webServer> <handlers> <add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true"/> </handlers> </system.webServer>
Add this node and you're done!