I want to try the difference between ServiceStack Web and Microsoft's built-in webapi, and see what advantages there are, so try to build a ServiceStack Web Service application today
1: Create a new .NET 4.5 web empty project, and then use the nuget command to install ServiceStack:
.net version must be greater than or equal to version 4.5, the 4.0 project will not be installed successfully! As shown in the figure below.
2: Create a new model and service folder
Create a request and response entity with the following code:
3: Create a service interface in the service folder
4: Create a new global application class named Global.asax to this project, and the code is as follows:
5: Modify the web.config configuration, add a new system.webServer node, and the complete configuration is as follows:
So far our service has been completed, running the program can see the interface as follows,GetAllUserInfoRequest and GetByAgeUserInfoRequest can be understood as actual call methodsThe parameters defined in it can be interpreted as the request parameters of the method, as shown in the figure below:
The service methods in ServiceStack are named Any, Get, and Post, which are also supported request types by ServiceStack.Any means that the service can be called in both HTTP Get and HTTP Post。 This strengthens and simplifies the implementation of RESTFull-style WebServices. Just add love [Route(...)] on these methods Attributes. In ServiceStack,The difference between methods and methods is distinguished by the parameters of the service and the request object Request DTO, rather than differentiating by method name as in WCF. This meansA request DTO object cannot be reused across multiple Services in ServiceStack。
Adding the format parameter after the get request parameter can return the type of response, such as: format=json, format=xml, etc., and it seems that the jsonp format is also supported.
For example: http://localhost:52079//json/reply/GetAllUserInfoRequest?format=xml
To sum up, compared with Microsoft's web API, ServiceStack seems to have no concept of method in the web, it is a different request entity is a method, it can automatically generate interface documents, and the format of the response can be returned through format (although Microsoft's is also possible, but it feels simpler than Microsoft)
Reference: https://www.cnblogs.com/woxpp/p/5012947.html
https://msdn.microsoft.com/zh-cn/magazine/dn342871.aspx
https://github.com/ServiceStack/ServiceStack/wiki
Finally, attach the project source code!
Tourists, if you want to see the hidden content of this post, please Reply
|