Swagger + AutoRest generate web api client (. Net) and swaggerautorest
For. for net, using web APIs to build services is a good choice, all of which are http requests and simple calls. However, if you really want to call them in a program, you still have to do some work, for example, we need to manually call httpClient and map the Model. If there is less service, it will be complicated. Swagger's information about Swagger has been introduced by other blogs. For more information, see http://chuansong.me/n/322685748559 After Swagger is used, it can solve the problem that the Service does not have documents or documents and services are not synchronized. At the same time, the Swagger comes with the function of simulating requests, so you can directly test the interface on the page. These are not only for development, but also for testing students. The following is a simple Web API. After adding comments to reference swagger, access http://XXX/swagger/ui/index (XXX is your domain name). The result is as follows: Your service and service description are displayed on the page. Click a service to view the parameters you have defined and initiate a test on the page. AutoRest has Swagger, which makes it easy to view the service. Next we will call the service. In the past, we usually wrote an HttpUtils class, which contains two methods: SendGet and SendPost. If it is a simple parameter, it is okay, however, if the Get request returns an object or an object needs to be sent during Post, you need to write these classes for encapsulation. Here is a component: AutoRest ( https://github.com/Azure/AutoRest ), In short, it is an EXE tool that can generate a service client based on the Swagger structure. This client allows you to call the service just like calling a local method. The method encapsulates Http requests. On the Swagger page, you can click the emerge e button. You can see in the browser that a Json string is returned here. The page is also rendered based on this string, and AutoRest also generates call classes based on this structure. Select Nuget and download AutoRest. Find the pageage folder under solution. The directory is as follows: Open it in the command line and enter AutoRest.exe-CodeGenerator CSharp-Modeler Swagger-Input http://XXX/swagger/docs/v1 -Namespace UserServiceClient. The Input parameter is the Json address of Swagger. For details about the parameter meanings, see the document. After successful execution, a Generated directory will be Generated under the current directory to open the file as follows: Create a console program and copy the entire folder to the console program. The folder name can be defined at will. At the same time, the console program must reference a Client in Nuget to write and call code, and support both synchronous and asynchronous interfaces. Now, all are completed. Conclusion 1. the above method can be completed by the server, and then the generated code, after simple modification, is compiled and released to the company nuget. Other students can download the Code directly to the company's nuget 2. the server can only integrate Swagger, and other students can use the tool to generate it. 3. The above is only one way for the company to facilitate service calling before RPC is used. I will introduce other service calling methods under. Net, such as Hession and serverstack.
Reference page: http://qingqingquege.cnblogs.com/p/5933752.html