OoLunar.HyperSharp.Responders 0.3.0

dotnet add package OoLunar.HyperSharp.Responders --version 0.3.0                
NuGet\Install-Package OoLunar.HyperSharp.Responders -Version 0.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OoLunar.HyperSharp.Responders" Version="0.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OoLunar.HyperSharp.Responders --version 0.3.0                
#r "nuget: OoLunar.HyperSharp.Responders, 0.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OoLunar.HyperSharp.Responders as a Cake Addin
#addin nuget:?package=OoLunar.HyperSharp.Responders&version=0.3.0

// Install OoLunar.HyperSharp.Responders as a Cake Tool
#tool nuget:?package=OoLunar.HyperSharp.Responders&version=0.3.0                

HyperSharp Overview

HyperSharp is a C# implementation of the HTTP 1.1 protocol. It's designed with emphasis on speed, lightweight nature, and most importantly: user-friendliness.

To get started, you can install the NuGet package and follow the setup instructions.

To request support, you may open up a new GitHub issue, discussion or join the Discord.

To report bugs or request new features, please use GitHub issues.

Lastly, all API documentation and tutorials are available on the website, which is generated from the latest commit at the docs folder.

Table of Contents

Core Concept: Responders

  • The foundation of HyperSharp relies on the concept of responders.
  • A responder consists of a list of delegates.
  • Each delegate is a function that takes a request and produces a response.
  • Both request and response are generic types, allowing customization.
  • All responders and responder dependencies are executed sequentially.
  • If any delegate returns an error or value, all subsequent delegates are skipped and the response is returned.

Response Handling

  • Responders use results and errors for handling responses.
  • Synchronous (void) and asynchronous (Task/ValueTask) execution modes are supported, with no additional setup required from the user.

Setup

There are two ways to setup HyperSharp, depending on your needs. The recommended way is to use dependency injection, through IServiceCollection:

serviceCollection.AddHyperSharp((serviceProvider, hyperConfiguration) =>
{
    IConfiguration configuration = serviceProvider.GetRequiredService<IConfiguration>();
    string? host = configuration.GetValue("server:address", "localhost")?.Trim();
    if (!IPAddress.TryParse(host, out IPAddress? address))
    {
        IPAddress[] addresses = Dns.GetHostAddresses(host);
        address = addresses.Length != 0 ? addresses[0] : throw new InvalidOperationException("The server address could not be resolved to an IP address.");
    }

    hyperConfiguration.ListeningEndpoint = new IPEndPoint(address, configuration.GetValue("server:port", 8080));
    hyperConfiguration.AddResponders(new[] { typeof(HelloWorldResponder) });
});

IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
HyperServer hyperServer = serviceProvider.GetRequiredService<HyperServer>();
hyperServer.Start();

We recommend using the service collection method because the responders use dependency injection to resolve their dependencies. This allows for a more modular and testable design.

However, we understand that not everyone wants to use dependency injection. If you don't want to use dependency injection, you can use the HyperServer class directly:

HyperServer hyperServer = new(new HyperConfiguration(new HyperConfigurationBuilder()
{
    ListeningEndpoint = new IPEndPoint(IPAddress.Loopback, 8080),
    Responders = new[] { typeof(HelloWorldResponder) }
}));

hyperServer.Start();

Example: Hello World

using System;
using System.Threading;
using HyperSharp.Protocol;
using HyperSharp.Responders;
using HyperSharp.Results;

public class HelloWorldResponder : IResponder<HyperContext, HyperStatus>
{
    // Specifies any required types for this responder (empty in this case)
    public static Type[] Needs => Type.EmptyTypes;

    // Generates a response indicating success with a message "Hello World!"
    public Result<HyperStatus> Respond(HyperContext context, CancellationToken cancellationToken = default) => Result.Success(HyperStatus.OK("Hello World!"));
}

This example demonstrates the structure of a responder using HyperSharp to create a "Hello World!" response.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on OoLunar.HyperSharp.Responders:

Package Downloads
OoLunar.HyperSharp

A C# implementation of the HTTP 1.1 protocol.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.0 191 9/6/2023
0.2.0 181 8/9/2023