Library for using HTTP APIs. Hide all details of implementation for HTTP Client/Server. Let developer to be focused on business logic development with standard c# classes.
Consists from:
- Common Library (commanet.Http.RPC.Common)
- Server Side Library (commanet.Http.RPC.Server)
- Client Side Library (commanet.Http.RPC.Client)
Server and Client parts can be used separately in different applications or together if app plays both roles: Provides API for external programs and use external API(s) provided by others.
using commanet.Http.RPC;
...
// Define common interface
// Can do it in server side application or, if supposed
// to use client in DotNet, better create separate assembly
// shared between client and server implementations
[HTTPRPCInterface] // Attribute it to let know RPC Engine
IMyAPI = interface
{
string HelloHttpRPCWorld(();
}
using commanet.Http.RPC;
...
// Define Class Implements our Interface
public class MyServerImplementation : IMyAPI
{
string HelloHttpRPCWorld(){return "Hello World!!!"};
}
...
// Start server
static int Main(string[] args)
{
var server = new HTTPRPCServer("localhost",5010);
server.Run();
server.WaitForShutdown();
// Notice that classes implements HTTPRPC interfaces will be
// discovered automatically - you do not need to
// create instances in code
}
using commanet.Http.RPC;
// Start server
static int Main(string[] args)
{
var client = HTTPRPCClient.Client<IMyAPI>(new Uri("http://localhost:5010/api"));
var result=client.HelloHttpRPCWorld();
Console.WriteLine(result);
}
- HTTPRPCInterface - used on interface definition level
- HTTPHandler - used on inteface method definition level
All attributes declared in commanet.Http.RPC.Common assembly is shared between server and client sides.
string BasePath = "api"
Defines base path to HTTP API calls. Full URL to API call will be :
http://<yourserver>[:port]/<BasePath>/<MethodName>
bool AddInterfaceNameToPath = false
If parameter is true then in URL will be added interface name. It is useful in case if application implements multiple interfaces. Leading 'I' character in interface name will be suppressed. Full URL for calling method will be:
http://<yourserver>[:port]/<BasePath>/<InterfaceName>/<MethodName>
Notice that all names in url will be converted into low case.
HttpMethods Method = HttpMethods.Get
Defines HTTP method to be used
public enum ParamsPlacedIn {URLPath, Query, Body}
ParamsPlacedIn ParametersIn = ParamsPlacedIn.URLPath
Defines how call parameters will be passed to server.
Pass parameters in URL path (call arguments by its position):
http://server/api/method/value1/value2
Pass parameters in URL Query part. Arguments can be passed by name or by position:
http://server/api/method?parameter1=value1¶meter2=value2
http://server/api/method?value1&value2
Notice that calls generated by commanet.Http.RPC.Client library, in case of choosing Query parameters location will use named parameters. Server side automatically discovers way used and supports all of them. Third party client applications can use both URL and QUERY parameters locations.
int CacheMilliseconds = 0
Optionally (if > 0) defines time for caching call results. Useful for performance tuning for high-loaded apps.
Output method parameters are not supported.
All types used as input parameters and results are to be serializable into JSON.
Types tested and confirmed as supported:
- int/uint
- long/ulong
- double
- float
- string
- DateTime
- c# Classes with published properties All published properties should be JSON serializable.
- byte[] Used for binary data transfer. see data size limitations description below
Client timouts available in HTTPRPCClient static public properties.
Timout | Default Value [bytes] |
---|---|
WaitResponseTimeoutMs | 30000 (milliseconds) |
ReadContentTimeoutMs | 5000 (milliseconds) |
For safety, amount of data to be tranfered by one HTTP request is limited on server side. URL size (used in GET reqests) is limited too. It can be changed in HTTPRPCServer class constructor input parameters.
Limit | Default Value [bytes] |
---|---|
MaxRequestLineSize | 8192 |
MaxRequestBodySize | 1048576 |
MaxRequestBufferSize | 1048576 |
MaxResponseBufferSize | 6553 |