项目作者: nats-io

项目描述 :
The official C# Client for NATS
高级语言: C#
项目地址: git://github.com/nats-io/nats.net.git
创建时间: 2015-11-05T17:58:38Z
项目社区:https://github.com/nats-io/nats.net

开源协议:Apache License 2.0

下载


License Apache 2.0
NuGet
Build
Slack

NATS .NET

NATS .NET is a client library designed to connect to the NATS messaging server,
fully supporting all NATS features.
It integrates seamlessly with modern .NET asynchronous interfaces such as
async enumerables and channels, and leverages advanced .NET memory, buffer and IO features. (supports server v2.11)

Check out NATS .NET client library documentation for guides and examples.

[!NOTE]
Don’t confuse NuGet packages!
NATS .NET package on NuGet is called NATS.Net.
There is another package called NATS.Client which is the older version of the client library
and will be deprecated eventually.

[!TIP]
NATS .NET now supports .NET Standard 2.0 and 2.1 along with .NET 6.0 and 8.0,
which means you can also use it with .NET Framework 4.6.2+ and Unity 2018.1+.

What is NATS?

NATS is a high-performance, secure, distributed messaging system.
It’s a connective technology tailored for modern distributed systems,
facilitating efficient addressing, discovery, and message exchange.
It supports dynamic service and stream processing across various locations and devices,
enhancing mobility, security, and independence from traditional constraints such as DNS.

Head over to NATS documentation for more information.

Quick Start

Install the NATS.Net package from NuGet:

  1. dotnet add package NATS.Net

Run a local nats-server to use or connect to the demo server if you’re not behind a firewall:

  1. await using var client = new NatsClient("demo.nats.io");

Basic messaging:

  1. // NATS core M:N messaging example
  2. await using var client = new NatsClient();
  3. // Subscribe on one terminal
  4. await foreach (var msg in client.SubscribeAsync<string>(subject: "foo"))
  5. {
  6. Console.WriteLine($"Received: {msg.Data}");
  7. }
  8. // Start publishing to the same subject on a second terminal
  9. await client.PublishAsync(subject: "foo", data: "Hello, World!");

Persistence with JetStream:

For this you need to run the server with JetStream enabled if you’re using a local server.

  1. // NATS JetStream basic publish-consume example
  2. await using var client = new NatsClient();
  3. var js = client.CreateJetStreamContext();
  4. // Create a stream to store the messages
  5. await js.CreateStreamAsync(new StreamConfig(name: "ORDERS", subjects: new[] { "orders.*" }));
  6. // Publish a message to the stream. The message will be stored in the stream
  7. // because the published subject matches one of the the stream's subjects.
  8. var ack = await js.PublishAsync(subject: "orders.new", data: "order 1");
  9. ack.EnsureSuccess();
  10. // Create a consumer on a stream to receive the messages
  11. var consumer = await js.CreateOrUpdateConsumerAsync("ORDERS", new ConsumerConfig("order_processor"));
  12. await foreach (var jsMsg in consumer.ConsumeAsync<string>())
  13. {
  14. Console.WriteLine($"Processed: {jsMsg.Data}");
  15. await jsMsg.AckAsync();
  16. }

See more details, including how to download and start NATS server and JetStream in our documentation.

Additionally check out NATS by example - An evolving collection of runnable, cross-client reference examples for NATS.

NATS .NET Goals

  • Only support Async I/O (async/await)
  • Target .NET Standard 2.0, 2.1, and the two most recent .NET LTS releases (currently .NET 6.0 & .NET 8.0)

Packages

  • NATS.Net: Meta package that includes all other packages except extensions
  • NATS.Client.Core: Core NATS
  • NATS.Client.JetStream: JetStream
  • NATS.Client.KeyValueStore: Key/Value Store
  • NATS.Client.ObjectStore: Object Store
  • NATS.Client.Services: Services
  • NATS.Client.Simplified: simplify common use cases especially for beginners
  • NATS.Client.Serializers.Json: JSON serializer for ad-hoc types
  • NATS.Extensions.Microsoft.DependencyInjection: extension to configure DI container

Contributing

You are welcome to contribute to this project. Here are some steps to get you started:

Reporting Bugs and Feature Requests

You can report bugs and request features
by opening an issue on GitHub.

Join the Community

You can join the community asking questions, sharing ideas, and helping others:

Contributing Code

[!NOTE]
Please make sure to sign your commits. All commits must be signed before a Pull Request can be merged.

  • Read the Contributor Guide
  • Fork the repository and create a branch
  • Open NATS.Net.sln solution in Visual Studio, Rider or VS Code (or any other editor of your choice)
  • Make changes and write tests
  • Run tests against a locally installed NATS server in your PATH
  • Note that some tests are still not reliable locally, so CI will run all tests
  • For a quick check, run NATS.Client.Platform.Windows.Tests which is a subset of tests that should pass on Windows
  • You can also locally run NATS.Client.CoreUnit.Tests and NATS.Client.Core2.Tests which are more stable
  • Run dotnet format at root directory of project to clear warnings that can be auto-formatted
  • Run dotnet build at root directory and make sure there are no errors or warnings
  • Submit a pull request

Please also check out the Contributor Guide and Code of Conduct.

Attribution

This library is based on the excellent work in Cysharp/AlterNats