Connection-Based Asynchronous Messaging (CBAM) framework provides asynchronous task-based API and implementation for workflow which communicates with remote resources, e.g. SQL, HTTP, LDAP servers, etc.
The CBAM.Abstractions project defines an interface Connection which represents stateful or stateless connection to some kind of remote endpoint, e.g. SQL/HTTP server.
The Connection interface allows to prepare a statement for execution: e.g. string containing SQL, an object which contains HTTP request, etc.
This prepared statement is exposed as IAsyncEnumerable<T>, which can be asynchronously enumerated using e.g. EnumerateSequentiallyAsync extension method.
Each item encountered during enumeration may be e.g. SQL statement execution result (statement execution information, or data row), or HTTP response (with all headers read, but content not read).
With CBAM as common framework, it is possible to interact with remote in various protocols.
HTTP example:
varresponse=awaitnewSimpleHTTPConfiguration(){Host="www.google.com",Port=443,IsSecure=true}.CreatePoolAndReceiveTextualResponseAsync(HTTPFactory.CreateGETRequest("/"));// Access content as stringvarstringContents=response.TextualContent// Access headers
var headerDictionary = response.HeadersPostgreSQL example:
// SQL exampleusingMicrosoft.Extensions.Configuration;// For configurationusingCBAM.SQL.PostgreSQL;// For CBAM PostgreSQL typesvarconfigData=newConfigurationBuilder()// This line requires reference to Microsoft.Extensions.Configuration NuGet package.AddJsonFile(System.IO.Path.GetFullPath("path/to/config/jsonfile"))// This line requires reference to Microsoft.Extensions.Configuration.Json NuGet package.Build().Get<PgSQLConnectionCreationInfoData>();// This line requires reference to Microsoft.Extensions.Configuration.Binder NuGet package// Create connection poolInt32[]integers;using(varpool=PgSQLConnectionPoolProvider.Factory.BindCreationParameters(newPgSQLConnectionCreationInfo(configData)).CreateTimeoutingResourcePool()){// Quick example on using connection pool to execute "SELECT 1" statement, and print the result (number "1") to console// The prepared statements are also fully supported, but out of scope from this example// The code below only requires CBAM.SQL.PostgreSQL project, the CBAM.SQL.PostgreSQL.Implementation is only for access of PgSQLConnectionPoolProvider.Factoryintegers=awaitpool.UseResourceAsync(async pgConnection =>{returnawaitpgConnection.PrepareStatementForExecution("SELECT 1").IncludeDataRowsOnly().Select(async row =>awaitrow.GetValueAsync<Int32>(0)).ToArrayAsync();});}// Elsewhere, e.g. maybe in a separate background thread/loop:// This will close all connections that has been idle in a pool for over one minuteawaitpool.CleanUpAsync(TimeSpan.FromMinutes(1));NATS example:
varpool=NATSConnectionPoolProvider.Factory.BindCreationParameters(newNATSConnectionCreationInfo(newNATSConnectionCreationInfoData(){Connection=newNATSConnectionConfiguration(){Host="localhost",Port=4222,ConnectionSSLMode=ConnectionSSLMode.NotRequired}})).CreateOneTimeUseResourcePool().WithoutExplicitAPI();varmessageContentsByteArray=awaitpool.UseResourceAsync(async natsConnection =>{returnawaitnatsConnection.SubscribeAsync("MySubject").Select( message =>message.CreateDataArray()).FirstAsync();});The projects are contained in Source/Code folder, while the tests are in Source/Tests folder.
The most interesting projects, from end-user point of view, are most likely CBAM.SQL.PostgreSQL.Implementation, CBAM.SQL.ExecuteStatements.Application, CBAM.HTTP.Implementation, and CBAM.NATS.Implementation.
CBAM projects aim to be as portable as possible. The CBAM.SQL.MSBuild project targets .NET Standard 1.3, while the other projects target .NET Standard 1.0 and .NET 4.0, among others, as their most portable target frameworks.