First, preface
The previous article, "Webapi using multiple XML file generation help documents," mentions why there is a solution for generating help documents based on multiple XML files, as the possible uses of the defined model are:
1: Unit Test
2: Other project references (possibly in the form of nuget packages)
The 3:WEBAPI client (encapsulated httpclient and WEBAPI interface calls are actually included in the 2nd: )
To the source code can be pulled directly to the bottom, the source code as always is still there.
Second, why to encapsulate WEBAPI client
1: Make Webapi "Transparent" to the caller, directly to the way the assembly is referenced.
2: Call the Portal within the Unified project (of course, it is also possible to bypass the direct-to-request interface, but this is a problem for team management).
3: Composite Interface Call
4: Versioning (through NuGet, either self-built or nuget.org)
Iii. what is included in the encapsulated WEBAPI client
This continues to be demonstrated using the Webapi2postman project.
First, because the Webapi interface is encapsulated in httpclient, at least one needs to define the request route for the interface, where only two are defined.
We create a new class library project in the solution and name it webapi2postman.client, then add a class named Webapi2postmanstatic
- Using System.Configuration;
- Namespace Webapi2postman.client
- {
- // <summary>
- // Webapi2postman static resource class
- // </summary>
- public class Webapi2postmanstatic
- {
- // <summary>
- /// service address
- // </summary>
- public static string serviceurl = configurationmanager.appsettings["Webapi2postmanserviceurl"];
- // <summary>
- /// get all products
- // </summary>
- public static string routeproductgetall = "Api/product/all";
- // <summary>
- /// Add product
- // </summary>
- public static string routeproductadd = "Api/product/add";
- }
- }
The interface request is nothing more than the HTTP method, but here we only think that our interface contains only get and post two HTTP request methods.
Based on this, we define a Webapihelper class.
- <summary>
- Webapi Help Class
- </summary>
- Public class Webapihelper
- {
- public static T1 callpostwebapi<t1, t2> (string URL, T2 request, string serviceurl, int? timeOut = ten)
- public static T1 callgetwebapi<t1> (string url, string serviceurl, int? timeOut = ten)
- public static list<tresponse> callwebapibatch<trequest, Tresponse> (HttpMethod method, string Endpoint, list<trequest> batchrequestmodels, string url, string serviceurl, int? timeOut = ten)
- public static Async task<t1> callpostwebapiasync<t1, t2> (string URL, T2 request, string serviceurl, int? timeOut = ten)
- public static async task<t1> callgetwebapiasync<t1> (string url, string serviceurl, int ? TimeOut = ten)
- public static Async task<list<tresponse>> callwebapibatchasync<trequest, tresponse> ( HttpMethod method,string endpoint,list<trequest> batchrequestmodels,string url,string serviceurl, int? timeout=10)
- }
In order to save space and easy to watch, the implementation has been deleted, you can see the definition of 6 methods, divided into synchronous and asynchronous altogether three classes, Get, Post, batch (bulk interface, interested in the next article to talk about).
Then, add a class webapi2postmanclient for encapsulation.
- Using System.Collections.Generic;
- Using Webapi2postman.webmodel;
- Namespace Webapi2postman.client
- {
- // <summary>
- // Webapi2postman client
- // </summary>
- public class Webapi2postmanclient
- {
- // <summary>
- /// get all products
- // </summary>
- // <param name= "Timeout" > timeout period </param>
- /// <returns> Product List </returns>
- public static ienumerable<product> getallproduct (int? timeout = ten)
- {
- return webapihelper.callgetwebapi<ienumerable<product>> (webapi2postmanstatic.routeproductgetall , webapi2postmanstatic.serviceurl,timeout);
- }
- // <summary>
- /// Add product
- // </summary>
- /// <param name= "request" > Added products </param>
- // <param name= "Timeout" > timeout period </param>
- /// <returns> Add results </returns>
- public static string addproduct (Product request,int? timeout = ten)
- {
- return webapihelper.callpostwebapi<string, product> (Webapi2postmanstatic.routeproductadd, request, Webapi2postmanstatic.serviceurl, timeout);
- }
- }
- }
Iv. using the NuGet Package Manager to publish Webapi2postman.client
How to build nugetserver don't go into it, create a new empty Web project then package console input
- Pm> Install-package Nuget.server
And then all of that will have to be published directly to IIS.
At this point, the local already has a nugetserver, browse below.
Right-click the project Webapi2postman.client = = Properties and the application = = assembly information, complete some information.
Run the command line and navigate to the current project directory to execute
- NuGet pack webapi2postman.client.csproj-s http://localhost: 123
The nuget here is a matching environment variable, or it can be replaced (path/nuget.exe), if the NuGet package restore is enabled, the folder in the solution directory. NuGet will have NuGet.exe and its configuration.
HTTP://LOCALHOST:88 is the server address, here must not add/nuget, otherwise will be reported 403. The password is followed by a warning and prompted to use the NuGet setapikey settings if it is not set by default.
In fact, it has always been a batch of scripts to pack and upload our packages.
Or dudu a long time ago, "use NuGet to manage your own bag I" and http://www.cnblogs.com/lzrabbit/tag/NuGet/to speak very detailed.
There's the ability to integrate nuget and vs directly into one-click uploads, but it's just a manual setup.
In order not to be so troublesome, I wrote a vs extension tool Push2nuget to simplify these operations.
First in the tool = "expand and update =" online = "Visual Studio Library =" Input push2nuget installation, restart the solution.
Because it was written in a breath, did not think of some of the service parameters to put, temporarily thrown into the. NuGet folder, therefore, you need to create a new Nuget.xml file under the. NuGet folder and refine the information.
- <? XML version= "1.0" encoding="Utf-8"?>
- <selfserver>
- <URL>http://localhost:88</url>
- <ApiKey>123</ApiKey>
- </selfserver>
Then right-click on the project to "pack and upload".
Displayed after success.
Create a new unit test project Webapi2postman.tests and set the NuGet source in NuGet.
Install the webapi2postman.client you just uploaded
Add two unit test methods
Five, the source code
Sample Source: Https://github.com/yanghongjie/WebAPI2PostMan
Expansion tool: Https://github.com/yanghongjie/Push2NugetServer
Since all see this, the evaluation of the best to reward a recommendation Bai!
Packaged WEBAPI client with NuGet package upload vs Expansion tools