Skip to content

dma198/commanet.Http.RPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

commanet.Http.RPC


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:

  1. Common Library (commanet.Http.RPC.Common)
  2. Server Side Library (commanet.Http.RPC.Server)
  3. Client Side Library (commanet.Http.RPC.Client)

HTTPRPC Architecture

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.

Getting Started

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((); 
}

Server Side

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
}

Client Side

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);    
}

API Interface Attributes

  1. HTTPRPCInterface - used on interface definition level
  2. HTTPHandler - used on inteface method definition level

All attributes declared in commanet.Http.RPC.Common assembly is shared between server and client sides.

HTTPRPCInterface Attribute Parameters

Base Path
string BasePath = "api"

Defines base path to HTTP API calls. Full URL to API call will be :

http://<yourserver>[:port]/<BasePath>/<MethodName>
AddInterfaceNameToPath
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.

HTTPHandler Attribute Parameters

Path
HttpMethods Method = HttpMethods.Get

Defines HTTP method to be used

ParametersIn
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&parameter2=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.

CacheMilliseconds
int CacheMilliseconds = 0

Optionally (if > 0) defines time for caching call results. Useful for performance tuning for high-loaded apps.

HTTP API Interface Method Restrictions

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 Timeouts

Client timouts available in HTTPRPCClient static public properties.

Timout Default Value [bytes]
WaitResponseTimeoutMs 30000 (milliseconds)
ReadContentTimeoutMs 5000 (milliseconds)

Input/Output Data Size Limitation

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

About

Http RPC library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published