LiteSpeedLink: The .NET lightweight, compile-time, contract-first and source generated communication framework
This project implements a custom API communication channel using the UDP
, TCP
or QUIC
. The communication model is contract-first, with two primary implementation variants: Minimal and Class-first, totally source generated
- Contract-First: Both endpoints, host and client must define implementors, either service interfaces or delegates.
- Hosts: Server implementations ready to receive any client message.
- Clients:
IConnection
implementations ready to chat with any implemented server preiously mentioned. - Communication Contracts: Defined using source generation with two variants:
- Minimal Variant: Methods are declared in
Program.cs
with attribute markers (EndpointHandlerAttribute
). - Class Variant: Methods inside classes implementing interfaces inheriting
IServiceUnit
.
- Minimal Variant: Methods are declared in
Host service partial classes are generated when they have the ServiceHostAttribute
together with any of the following attributes from the Jab
library:
TransientAttribute<TInterface, TImplementation>
TransientAttribute<TImplementation>
ScopedAttribute<TInterface, TImplementation>
ScopedAttribute<TImplementation>
SingletonAttribute<TInterface, TImplementation>
SingletonAttribute<TImplementation>
These attributes should be applied to the class owning the ServiceHostAttribute
.
[ServiceHost]
[Singleton<IMySingletonService, MySingletonService>]
[Scoped<IMyScopedService, MyScopedService>]
[Transient<IMyTransientService, MyTransientService>]
public partial class MyHostService;
Used in the minimal variant to declare service methods.
Every method in Program.cs
marked with EndpointHandlerAttribute
will serve as host endpoint (a dedicated delegate will be registered)
Program.cs
[ServiceHost]
// other services...
public partial class MyHostService;
[EndpointHandler]
internal async ValueTask<bool> SaveConfigurationAsync(string key, string value, [Service] IConfigurationManager configManager = default!, CancellationToken token = default)
{
return await configManager.CreateOrUpdateAsync(key, value, token);
}
Interfaces inheriting this must implement the service endpoints.
public interface IProductServiceUnit : IServiceUnit
{
async ValueTask<CreatedProduct> CreateProductAsync(NewProductMessage message);
}
To generate clients you just must add interfaces defined in contracts or minimal variant generated delegates to service client class (owning ServiceClientAttribute),
[ServiceClient]
public partial class MyClientService : IMyService, IServiceHandler<CreateProductAsync>;
This project uses the following libraries:
System.IO.Pipelines
: For managing bytes sequences to be serialized/sdeserialized.Jab
: For dependency injection and service management.
- Pipeline implementation for middlewares
- Binary mappers (a fork of SourceCrafter)
- Streaming processor (suitable for gaming, duplex services and real-time communication)