5 Star 6 Fork 5

allenwdj / WebApiProxy

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

https://github.com/RestCode/WebApiProxy 此源码根据个人需求,做了一些更改。

WebApiProxy

This project comprises of a server-side extension that provides an proxy end-point for serving service metadata & a client-side proxy in JavaScript and also a client-side task that generates a client proxy in C#.

Service-side Proxy provider

Getting started Install via NuGet

Extend your ASP.NET Web API service by installing this package from NuGet:

Install-Package WebApiProxy

Note: This package requires the core libraries of ASP.NET Web API (version 5 or higher)

Register proxy endpoint

You need to explicitly register the proxy endpoint route. You can do this by using the RegisterProxyRoutes extension method that extends HttpConfiguration:

config.RegisterProxyRoutes();

Note: Make sure to include the WebApiProxy.Server namespace

This will register your service with an additional endpoint with the default address as /api/proxies'. It is also possible to specify a custom address by passing therouteTemplate` parameter:

config.RegisterProxyRoutes("$metadata");

Usage

This extension provides a proxy endpoint in your service (with /api/proxies as the default) that serves JavaScript and service metadata.

Given a Person API on the server:

public class PeopleController : ApiController
{
    public Person[] Get() {
    }

    public Person Get(int id) {
    }
}

allows you to use it like this in JavaScript on the client:

$.proxies.person.get()
  .done(function(people) {
    //do something with people
  });

$.proxies.person.get(2)
  .done(function(person) {
    //do something with person
  });

JavaScript proxy Simply reference the proxy endpoint provided inside your HTML and you're good to go:

<script src="/api/proxies" type="text/javascript"></script>

This functionality was adopted from ProxyApi - kudos to Stephen Greatrex :)

Getting the service Metadata

Invoke the service on its proxy endpoint api/proxies with the request header X-Proxy-Type as "metadata" and the service metadata including documentation will be in the response.

Excluding controllers

You can exclude your controllers by simply decorating them with the ExcludeProxy attribute

Client-side Proxy Generator

Generate a C# proxy on the client-side based on the metadata provided by a service implementing WebApiProxy. Install via NuGet

Install this package from NuGet:

Install-Package WebApiProxy.CSharp

Note: This package requires the libraries of ASP.NET Web API Client (version 5.2 or higher)

Generation

Note: The automatic generation of code upon project build is now completely opt-in. You need to explicitly specify if you want the proxy to be generated on every build.

The C# proxy code will be generated every time you re-build the project (only if you explicitly specified generateOnBuild="true") and is based on specific configuration in the WebApiProxy.config file:

<proxy endpoint="http://myservice.net/api/proxies" />

The endpoint property is mandatory and provides the generator the URI of the metadata endpoint. See the WebApiProxy Configuration File page to read more on the config file.

Note: The generated code is cached to avoid compilation errors if the service isn't reachable at that time

Proxy generation using the Nuget Package Manager Console

You can also generate proxy code on demand by using the Nuget Package Manager Console. Simply run the commandlet:

WebApiProxy-Generate-CSharp

Usage

Given a PeopleController on the service-side:

public class PeopleController : ApiController
{
        /// <summary>
        /// Gets all people with a given the name
        /// </summary>
        /// <param name="name">The criteria to search for</param>
        /// <returns>People collection</returns>
        public Person[] Get(string name)
        {
            return new Person[]{
                new Person { Id = 3, FirstName = "sss", LastName = "sqqqq"},
                new Person { Id = 3, FirstName = "sss", LastName = "sqqqq"}
            };
        }
}

can be used like this on the client-side:

using (var client = new PeopleClient())
{
    var response = await client.GetAsync("Fanie");

    var people = await response.Content.ReadAsAsync<Person[]>();
}

Code snippet

Use the webapiproxy code snippet to quickly create the code structure above. Just type "webapiproxy" followed by the TAB key.

Note: If the types are not found (or resolved) after build, simply give your project a restart or restart Visual Studio.

Great developer experience with Intellisense

It even has nice Intellisense including documentation provided by the documentation provider:

输入图片说明

Note: The documentation on the Intellisense will only appear if the service uses the documentation provider. You can use the ASP.NET Web API Help Page package on NuGet

MIT License Copyright (c) 2020 allenwdj Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

AspNet WebAPI Proxy JavaScript CSharp expand collapse
C#
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
C#
1
https://gitee.com/jucheap/WebApiProxy.git
git@gitee.com:jucheap/WebApiProxy.git
jucheap
WebApiProxy
WebApiProxy
master

Search