CoAP.NET 1.0.5-pre-8

This is a prerelease version of CoAP.NET.
dotnet add package CoAP.NET --version 1.0.5-pre-8                
NuGet\Install-Package CoAP.NET -Version 1.0.5-pre-8                
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="CoAP.NET" Version="1.0.5-pre-8" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CoAP.NET --version 1.0.5-pre-8                
#r "nuget: CoAP.NET, 1.0.5-pre-8"                
#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 CoAP.NET as a Cake Addin
#addin nuget:?package=CoAP.NET&version=1.0.5-pre-8&prerelease

// Install CoAP.NET as a Cake Tool
#tool nuget:?package=CoAP.NET&version=1.0.5-pre-8&prerelease                

CoAP.NET - A CoAP library for .NET

The Constrained Application Protocol (CoAP) (https://datatracker.ietf.org/doc/draft-ietf-core-coap/) is a RESTful web transfer protocol for resource-constrained networks and nodes. CoAP.NET is an implementation in C# providing CoAP-based services to .NET applications. Reviews and suggestions would be appreciated.

Copyright (c) 2011-2013, Longxiang He <longxianghe@gmail.com>, SmeshLink Technology Co.
Copyright (c) 2016-2020, Jim Schaad <ietf@augustcellars.com>
Copyright (c) 2023-, Stephen Berard <stephen.berard@outlook.com>

How to Install

The C# implementation is available in the NuGet Package Gallery under the name CoAP.NET. To install this library as a NuGet package, enter 'Install-Package CoAP.NET' in the NuGet Package Manager Console.

Documentation

Coming soon

CoAP Client

Access remote CoAP resources by issuing a Request and receive its Response(s).

  // new a GET request
  Request request = new Request(Method.GET);
  request.URI = new Uri("coap://[::1]/hello-world");
  request.Send();
  
  // wait for one response
  Response response = request.WaitForResponse();

There are 4 types of request: GET, POST, PUT, DELETE, defined as Method.GET, Method.POST, Method.PUT, and Method.DELETE.

Responses can be received in two ways. By calling request.WaitForResponse() a response will be received synchronously, which means it will block until timeout or a response is arrived. If more responses are expected, call WaitForResponse() again.

To receive responses asynchronously, register a event handler to the event request.Respond before executing.

Use LinkFormat.Parse(String) to parse a link-format response. The returned enumeration of WebLink contains all resources stated in the given link-format string.

  Request request = new Request(Method.GET);
  request.URI = new Uri("coap://[::1]/.well-known/core");
  request.Send();
  Response response = request.WaitForResponse();
  IEnumerable<WebLink> links = LinkFormat.Parse(response.PayloadString);

See CoAP Example Client for more.

CoAP Server

A new CoAP server can be easily built with help of the class CoapServer

  static void Main(String[] args)
  {
    CoapServer server = new CoapServer();
    
    server.Add(new HelloWorldResource("hello"));
    
    server.Start();
    
    Console.ReadKey();
  }

See CoAP Example Server for more.

CoAP Resource

CoAP resources are classes that can be accessed by a URI via CoAP. In CoAP.NET, a resource is defined as a subclass of Resource. By overriding methods DoGet, DoPost, DoPut, or DoDelete, one resource accepts GET, POST, PUT or DELETE requests.

The following code gives an example of HelloWorldResource, which can be visited by sending a GET request to "/hello-world", and respones a plain string in code "2.05 Content".

  class HelloWorldResource : Resource
  {
      public HelloWorldResource()
          : base("hello-world")
      {
          Attributes.Title = "GET a friendly greeting!";
      }

      protected override void DoGet(CoapExchange exchange)
      {
          exchange.Respond("Hello World from CoAP.NET!");
      }
  }
  
  class Server
  {
      static void Main(String[] args)
      {
          CoapServer server = new CoapServer();
          server.Add(new HelloWorldResource());
          server.Start();
      }
  }

See CoAP Example Server for more.

Logging

Logging makes use of the Microsoft.Extensions.Logging package. Logging is configured using the static LogManager class. By default, logs will be output to the console. A custom logger can be specified by calling LogManager.SetLoggerFactory(ILoggerFactory) as follows:

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole();
});
LogManager.SetLoggerFactory(loggerFactory);

Building the sources

The project is built using Visual Studio 2022. The project is set up to use the latest .NET framework (version 7.0 as of this writing). The project can be built direction in Visual Studio or via the the command line as follows:

> dotnet build

License

BSD with attribution See LICENSE for more info.

Acknowledgements

This project is built on the CoAP-CSharp project of jimsch and the CoAP.NET project of smeshlink (which in turn is based on Eclipse Californium). This is a refresh of the original codebases as they were both no longer being maintained.
The package and class names have been reset to the original names per the CoAP.NET project.

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 was computed.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5-pre-8 215 12/19/2023
1.0.5-pre-7 137 12/18/2023
1.0.4 1,001 7/11/2023
1.0.4-pre-7 111 12/18/2023
1.0.4-pre-6 107 12/18/2023
1.0.4-pre 114 12/18/2023
1.0.3 311 6/5/2023
1.0.2 147 6/2/2023
1.0.1 150 5/30/2023
1.0.0 266 5/26/2023

This project is built on the CoAP-CSharp project of jimsch and the CoAP.NET project of smeshlink (which in turn is based on Californium).  This is a refresh of the original codebases as they were both no longer being maintained.  The package and class names have been reset to the original names per the CoAP.NET project.
           1.0.5
           - Fixed minor bug in observer handling

           1.0.4
           - Logging fixes and improvements

           1.0.3
           - Fixed a bug on the client side which prevented DTLS connections from being established
   
           1.0.2
           - Refactored logging to use Microsoft.Extensions.Logging (see README for usage details)
           
           1.0.1
           - Removed dependency on Com.AugustCellars.COSE; currently only DTLS with pre-shared keys (PSK) is supported

           1.0.0
           - Intial refresh of project codebase