Packaged WEBAPI client with NuGet package upload vs Expansion tools

Source: Internet
Author: User

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

  1. Using System.Configuration;
  2. Namespace Webapi2postman.client
  3. {
  4. // <summary>
  5. // Webapi2postman static resource class
  6. // </summary>
  7. public class Webapi2postmanstatic
  8. {
  9. // <summary>
  10. /// service address
  11. // </summary>
  12. public static string serviceurl = configurationmanager.appsettings["Webapi2postmanserviceurl"];
  13. // <summary>
  14. /// get all products
  15. // </summary>
  16. public static string routeproductgetall = "Api/product/all";
  17. // <summary>
  18. /// Add product
  19. // </summary>
  20. public static string routeproductadd = "Api/product/add";
  21. }
  22. }

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.

  1. <summary>
  2. Webapi Help Class
  3. </summary>
  4. Public class Webapihelper
  5. {
  6. public static T1 callpostwebapi<t1, t2> (string URL, T2 request, string serviceurl, int? timeOut = ten)
  7. public static T1 callgetwebapi<t1> (string url, string serviceurl, int? timeOut = ten)
  8. public static list<tresponse> callwebapibatch<trequest, Tresponse> (HttpMethod method, string Endpoint, list<trequest> batchrequestmodels, string url, string serviceurl, int? timeOut = ten)
  9. public static Async task<t1> callpostwebapiasync<t1, t2> (string URL, T2 request, string serviceurl, int? timeOut = ten)
  10. public static async task<t1> callgetwebapiasync<t1> (string url, string serviceurl, int ? TimeOut = ten)
  11. public static Async task<list<tresponse>> callwebapibatchasync<trequest, tresponse> ( HttpMethod method,string endpoint,list<trequest> batchrequestmodels,string url,string serviceurl, int? timeout=10)
  12. }

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.

  1. Using System.Collections.Generic;
  2. Using Webapi2postman.webmodel;
  3. Namespace Webapi2postman.client
  4. {
  5. // <summary>
  6. // Webapi2postman client
  7. // </summary>
  8. public class Webapi2postmanclient
  9. {
  10. // <summary>
  11. /// get all products
  12. // </summary>
  13. // <param name= "Timeout" > timeout period </param>
  14. /// <returns> Product List </returns>
  15. public static ienumerable<product> getallproduct (int? timeout = ten)
  16. {
  17. return webapihelper.callgetwebapi<ienumerable<product>> (webapi2postmanstatic.routeproductgetall  , webapi2postmanstatic.serviceurl,timeout);
  18. }
  19. // <summary>
  20. /// Add product
  21. // </summary>
  22. /// <param name= "request" > Added products </param>
  23. // <param name= "Timeout" > timeout period </param>
  24. /// <returns> Add results </returns>
  25. public static string addproduct (Product request,int? timeout = ten)
  26. {
  27. return webapihelper.callpostwebapi<string, product> (Webapi2postmanstatic.routeproductadd, request,  Webapi2postmanstatic.serviceurl, timeout);
  28. }
  29. }
  30. }
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

    1. 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

    1. 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.

  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <selfserver>
  3. <URL>http://localhost:88</url>
  4. <ApiKey>123</ApiKey>
  5. </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

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.