Code Monkey home page Code Monkey logo

dotnet-echo's Introduction

Icon dotnet-echo

Version Downloads License CI Status CI Version

Installing or updating (same command can be used for both):

dotnet tool update -g dotnet-echo

Usage:

> dotnet echo -?
echo
  A trivial program that echoes whatever is sent to it via HTTP or gRPC

Usage:
  echo [options] [<port>...]

Arguments:
  <port>  Port(s) to listen on. [default: 80 or 443 with --ssl] [default: ]

Options:
  -ssl            Use HTTPS with self-signed SSL certificate, persisted as dotnet-echo.pfx in the current directory.
  -http2          Use HTTP/2 only. Prevents additional port for HTTP/2 to support gRPC.
  --version       Show version information
  -?, -h, --help  Show help and usage information

The program will automatically check for updates once a day and recommend updating if there is a new version available.

The service supports gRPC too, with echo.proto:

syntax = "proto3";

service chamber {
  rpc echo (message) returns (message);
}

message message {
  string payload = 1;
}

Since gRPC needs to use HTTP/2, dotnet-echo will use the specified port(s) + 1 to listen HTTP/2-only traffic (i.e. if you specify 8080, the gRPC endpoint will be available at http://localhost:8081). You can avoid the additional port by forcing HTTP/2-only with the --http2 option.

Example of a .NET client to run echo in the chamber service:

<Project>
  ...
  <ItemGroup>
    <PackageReference Include="Google.Protobuf" Version="*" />
    <PackageReference Include="Grpc.Net.Client" Version="*" />
    <PackageReference Include="Grpc.Tools" Version="*" />
  </ItemGroup>
  <ItemGroup>
    <Protobuf Include="echo.proto" GrpcServices="Client" />
  </ItemGroup>
</Project>
var channel = GrpcChannel.ForAddress("http://localhost:8081");
var service = new chamber.chamberClient(channel);

var response = await service.echoAsync(new message { Payload = "Hello World" }, cancellationToken: cancellation);

Console.WriteLine(response.Payload);

Example of a .NET client using HTTP/2 only mode for a regular HTTP POST:

var http = new HttpClient();

var send = await http.SendAsync(new HttpRequestMessage(
    HttpMethod.Post,
    "http://localhost:8081")
    {
        Content = new StringContent("Hello HTTP"),
        Version = new Version(2, 0),
        VersionPolicy = HttpVersionPolicy.RequestVersionOrHigher,
    });

Alternatively, you can force all HTTP requests to be sent with the required Version 2.0 property with a simple delegating HTTP handler like :

class Http2Handler : DelegatingHandler
{
    public Http2Handler() : this(new HttpClientHandler()) { }
    public Http2Handler(HttpMessageHandler inner) : base(inner) { }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Version = new Version(2, 0);
        request.VersionPolicy = HttpVersionPolicy.RequestVersionOrHigher;
        return base.SendAsync(request, cancellationToken);
    }
}

Which can be consumed like:

var http = new HttpClient(new Http2Handler());

var post = await http.PostAsync("http://localhost:8081", new StringContent("Hello HTTP"));

Since the handler automatically sets the relevant message properties, we can use the simpler Delete/Get/Post/Put methods instead.

An example of the output during execution:

And running on Ubuntu:

dotnet-echo's People

Contributors

dependabot[bot] avatar kzu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

dotnet-echo's Issues

Enable HTTP/2 only mode

Since it's quite simple to use HTTP/2-only for all HTTP requests, including gRPC, allow avoiding the extra port listening with a --http2 option.

If only .NET5 SDK is installed, tool fails to run

On a Pi, for example:

It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '3.1.0' was not found.
  - The following frameworks were found:
      5.0.6 at [/home/**/dotnet/shared/Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=3.1.0&arch=arm64&rid=debian.10-arm64

Add support for self-signed SSL certificate

Add a new --ssl switch that should:

  1. Read or create a dotnet-echo.pfx file in the current directory
  2. Use it to configure Kestrel for HTTPS
  3. Default port should be 80 if no --ssl switch is used, or 443 otherwise

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.