Package the WebAPI client, along with the Nuget package upload VS expansion tool, webapinuget

Source: Internet
Author: User

Package the WebAPI client, along with the Nuget package upload VS expansion tool, webapinuget
I. Preface

In the previous article, "use multiple xml files to generate a help document in WebAPI", we mentioned why there is a solution to generate a help document based on multiple xml files, because the defined model may be useful in:

1: unit test

2: other project references (may be in the form of Nuget packages)

3: WebAPI client (the encapsulated HttpClient and WebAPI calls are actually included in point 2nd ..)

 

Yes for source codeDirectly pull to the bottom, the source code is still there as always.

Ii. Why encapsulate the WebAPI client?

1. Make the WebAPI "Transparent" to the caller and reference the Assembly directly.

2: unify the call entry in the project (of course, it is okay to bypass the Direct Request interface, but this is a problem of team management ).

3: Combined interface call

4: Versioning (through Nuget, whether self-built or Nuget.org)

3. What are the encapsulated WebAPI clients?

Here we will continue to use the WebAPI2PostMan project for demonstration.

First, because the WebAPI interface is encapsulated with HttpClient, at least the request route of the interface needs to be defined. Only two links are defined here.

Create a class library project in the solution, name it WebAPI2PostMan. Client, and 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> // obtain 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 only contains the Get and Post 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 = 10) public static T1 CallGetWebApi <T1> (string url, string serviceUrl, int? TimeOut = 10) public static List <TResponse> CallWebApiBatch <TRequest, TResponse> (HttpMethod method, string endpoint, List <TRequest> batchRequestModels, string url, string serviceUrl, int? TimeOut = 10) public static async Task <T1> CallPostWebApiAsync <T1, T2> (string url, T2 request, string serviceUrl, int? TimeOut = 10) public static async Task <T1> CallGetWebApiAsync <T1> (string url, string serviceUrl, int? TimeOut = 10) 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 facilitate viewing, the implementation is deleted. We can see that six methods are defined, namely synchronous and asynchronous, Get, Post, and Batch (Batch interface, if you are interested, let's talk about it in the next article ).

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> // obtain all products /// </summary> /// <param name = "timeout"> timeout </param> /// <returns> Product list </returns> public static IEnumerable <Product> GetAllProduct (int? Timeout = 10) {return WebApiHelper. callGetWebApi <IEnumerable <Product> (WebApi2PostManStatic. routeProductGetAll, WebApi2PostManStatic. serviceUrl, timeout );} /// <summary> /// add product /// </summary> /// <param name = "request"> added product </param> /// <param name = "timeout"> timeout </param> // <returns> Add result </returns> public static string AddProduct (Product request, int? Timeout = 10) {return WebApiHelper. CallPostWebApi <string, Product> (WebApi2PostManStatic. RouteProductAdd, request, WebApi2PostManStatic. ServiceUrl, timeout );}}}
4. Use the Nuget Package Manager to publish WebAPI2PostMan. Client

Create an empty web project and enter the package Console

PM> Install-Package NuGet.Server

Then you can directly publish it to IIS.

Now there is an NugetServer in the local machine.

Nuget pack WebAPI2PostMan. Client. csproj-s http: // localhost: 88 123

Here, nuget is the configuration environment variable and can be replaced with (path/NuGet.exe). If the NuGet program package is enabled, NuGet.exe and Its configuration are included in the file folder.

Http: // localhost: 88 is the server address. You cannot add/nuget here. Otherwise, 403 is reported. The password is next to it. If it is not set by default, a warning is reported and the nuget setApiKey setting is prompted.

In fact, we have always been doing a batch processing script to package and upload our package.

Or dudu a long time ago issued "with Nuget management of their bags I" and http://www.cnblogs.com/lzrabbit/tag/NuGet/ are very detailed.

In addition, nuget is directly integrated with VS to implement one-click upload and other functions, which only need to be set manually.

In order not to be so troublesome, I wrote a VS extension tool Push2NuGet to simplify these operations.

First, enter Push2NuGet in tool =, expand and update =, online =, Visual Studio library =, and then restart the solution.

<? Xml version = "1.0" encoding = "UTF-8"?> <SelfServer> <Url> http: // localhost: 88 </Url> <ApiKey> 123 </ApiKey> </SelfServer>

Right-click the project and choose package and upload.

Displayed after success.

Create a unit test project WebAPI2PostMan. Tests and set the Nuget source in Nuget.

Install the uploaded WebAPI2PostMan. Client

Add two unit test methods

V. Source Code

Example source code: https://github.com/yanghongjie/WebAPI2PostMan

Expansion tools: https://github.com/yanghongjie/Push2NugetServer

Now that we can see this, we will enjoy another recommendation!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.