The first ServiceStack service framework, servicestack framework
The first ServiceStack service framework
Recently, when I first came into contact with ServiceStack, I tried to write the first service framework. It is inevitable that an error will occur. I hope my colleagues can correct me more.
The concepts related to ServiceStack are not described in detail, and there are many great online researchers in this field.
ServiceStack's official website address is:
Https://github.com/ServiceStack/ServiceStack
The main steps are as follows:
1. Create a new Window form program in.
Configure the port in AppConfig of the default program
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.5.2"/> </startup> <etettings> <! -- Port --> <add key = "ServerPort" value = "8001"/> </appSettings> </configuration>
2. Add reference library files related to ServiceStack
3. Add the APPHost. cs file and configure ServiceStack
Public class AppHost: cancelfhostbase {public AppHost (Assembly [] Assemblys): base ("ZBDService", Assemblys) {}// Configure public override void Configure (Container container) {Plugins. add (new CorsFeature (); JsConfig. export denullvalues = true; SetConfig (new HostConfig {DebugMode = true });}}
4. Configure the ServiceStack listener in the Program. cs file in the default Program list.
static void Main(string[] args) { Assembly[] assembly = { Assembly.Load("ZBDServiceApp") }; string port = ConfigurationManager.AppSettings["ServerPort"].ToString(); var listeningOn = args.Length == 0 ? "http://*:" + port + "/" : args[0]; var appHost = new AppHost(assembly) .Init() .Start(listeningOn); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
5. Create a cs file to store request routes and return value types
// Define a Route for [Route ("/ZBD/Test/{name}", verbs: "GET")] public class ZBD: IReturn <IList <string >>{ public string name {get; set ;}}
6. Create a cs file for specific operations, including database operations.
Public class ZBDServer: Service {public List <string> Get (ZBD request) {List <string> list = new List <string> (); list. add (request. name); list. add ("first service"); return list ;}
7. Run the program, start the service, and test it in the browser.
The above is only the most basic configuration, and you need to learn more.