-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update docs using the simple client (#655)
* Update docs using the simple client [nats:update-docs] * Add IDE0007 and IDE0008 pragma warnings in examples. * Fix example * Format * Add opts to client
- Loading branch information
Showing
46 changed files
with
908 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# API Documentation | ||
|
||
You can browse the latest NATS.Net API Documentation[^1] [here](NATS.html). | ||
You can browse the latest NATS.Net API Documentation[^1] [here](NATS.yml). | ||
|
||
[^1]: API Documentation is auto-generated using [DocFX](https://dotnet.github.io/docfx/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Native Ahead-of-Time Deployments | ||
|
||
For [Native Ahead-of-Time (AOT) deployments](https://learn.microsoft.com/dotnet/core/deploying/native-aot), | ||
you need to use the `NatsConnection` class directly. | ||
This is because the `NatsClient` class uses reflection to set up the ad hoc JSON serializers, which is not supported in AOT scenarios. | ||
|
||
If you started with the `NatsClient` class and need to switch to `NatsConnection`, you can do so without any changes to your code | ||
because both classes implement the `INatsClient` interface. | ||
|
||
NuGet packages that are compatible with AOT publishing are: | ||
- [NATS.Client.Core](https://www.nuget.org/packages/NATS.Client.Core) | ||
- [NATS.Client.JetStream](https://www.nuget.org/packages/NATS.Client.JetStream) | ||
- [NATS.Client.KeyValueStore](https://www.nuget.org/packages/NATS.Client.KeyValueStore) | ||
- [NATS.Client.ObjectStore](https://www.nuget.org/packages/NATS.Client.ObjectStore) | ||
- [NATS.Client.Services](https://www.nuget.org/packages/NATS.Client.Services) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Advanced Options | ||
|
||
For more advanced configuration, you can use the [`NatsOpts`](xref:NATS.Client.Core.NatsOpts) | ||
class to configure the connection to the NATS server. | ||
|
||
For example, you can hook your logger to `NatsClient` to make sure all is working as expected or | ||
to get help diagnosing any issues you might have: | ||
|
||
(For this example, you need to add [Microsoft.Extensions.Logging.Console](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console) from Nuget.) | ||
|
||
[!code-csharp[](../../../tests/NATS.Net.DocsExamples/Advanced/IntroPage.cs#logging)] | ||
|
||
## NatsClient vs NatsConnection | ||
|
||
[`NatsClient`](xref:NATS.Net.NatsClient) is a high-level API that wraps [`NatsConnection`](xref:NATS.Client.Core.NatsConnection) | ||
and provides a more user-friendly interface to interact with the NATS server. | ||
It is the recommended way to interact with the NATS server for beginners. | ||
However, if you need to access the underlying `NatsConnection` instance, | ||
you can do so by using the `Connection` property of `NatsClient` or by creating a new instance of `NatsConnection`. | ||
|
||
**So, What's the Difference?** | ||
|
||
`NatsClient` implements `INatsClient` and provides a high-level APIs, and also | ||
sets up the serializers to use the expected formats for message types like `int`, | ||
`string`, `byte[]`, and data classes for ad hoc JSON serialization. | ||
|
||
`NatsConnection` is the underlying class that manages the connection to the NATS server. | ||
It provides more advanced APIs and allows you to configure the connection in more detail. | ||
`NatsConnection`implements `INatsConnection` which extends `INatsClient` with advanced APIs, | ||
so you can use it as a `NatsClient` instance without any changes to your code. When you | ||
instantiate a `NatsConnection` with default options, you would only have basic serialization | ||
for `int`, `string`, and `byte[]` types, and you would need to set up the serializers for your data classes | ||
if you want to use e.g., JSON serialization. | ||
|
||
The other difference is that `NatsClient` sets `SubPendingChannelFullMode` internal channel option to | ||
`BoundedChannelFullMode.Wait` to avoid dropping messages when the subscriber's internal channel is full. | ||
This is a good default for most cases, but you can change it by setting the `SubPendingChannelFullMode` option | ||
in `NatsClient` constructor. | ||
|
||
[!code-csharp[](../../../tests/NATS.Net.DocsExamples/Advanced/IntroPage.cs#opts)] | ||
|
||
You can also use the `NatsConnection` class directly. | ||
|
||
[!code-csharp[](../../../tests/NATS.Net.DocsExamples/Advanced/IntroPage.cs#opts2)] | ||
|
||
**Which One Should I Use?** | ||
|
||
If you are new to NATS, you should use `NatsClient` as it provides a more user-friendly interface | ||
with sensible defaults especially for serialization. | ||
If you need more control over the connection options, AOT deployments, or custom serializers, | ||
you should use `NatsConnection`. | ||
|
||
See also [serialization](serialization.md) for more information on how to set up custom serializers. | ||
|
||
> [!NOTE] | ||
> Every [`NatsClient`](xref:NATS.Net.NatsClient) (and the underlying [`NatsConnection`](xref:NATS.Client.Core.NatsConnection)) | ||
> instance is a TCP connection to a NATS server. | ||
> Typically an application will only need one | ||
> connection, and many subscriptions and publishers would share that same connection. Connections are relatively | ||
> heavyweight and expensive to create while | ||
> subscriptions and publishers are lightweight internal NATS protocol handlers. | ||
> NATS.Net should be able to handle large numbers of subscriptions | ||
> and publishers per connection. | ||
## Subscriptions with Lower Level Control | ||
|
||
The | ||
[`SubscribeAsync()`](xref:NATS.Client.Core.INatsClient.SubscribeAsync``1(System.String,System.String,NATS.Client.Core.INatsDeserialize{``0},NATS.Client.Core.NatsSubOpts,System.Threading.CancellationToken)) | ||
method is a convenient way to subscribe to a subject and receive messages without much effort. | ||
If you need more control over how subscription is handled, you can use the | ||
[`SubscribeCoreAsync()`](xref:NATS.Client.Core.INatsConnection.SubscribeCoreAsync``1(System.String,System.String,NATS.Client.Core.INatsDeserialize{``0},NATS.Client.Core.NatsSubOpts,System.Threading.CancellationToken)) | ||
method instead. | ||
|
||
[!code-csharp[](../../../tests/NATS.Net.DocsExamples/Advanced/IntroPage.cs#lowlevel-sub)] | ||
|
||
## Round Trip Time (RTT) | ||
|
||
[`PingAsync()`](xref:NATS.Client.Core.INatsClient.PingAsync(System.Threading.CancellationToken)) is somewhat a | ||
special method in all NATS clients, mostly referred to as `rtt`. It is used to send a ping to the server and | ||
receive a pong back while measuring the round trip time. Since it waits for the server to respond, as a side effect, | ||
it also flushes the outgoing buffers. | ||
|
||
Remember that every [`NatsConnection`](xref:NATS.Client.Core.NatsConnection) instance is a single TCP connection | ||
and all the calls sent to the server are | ||
essentially sent back to back after they're picked up from internal queues and buffers. | ||
|
||
[!code-csharp[](../../../tests/NATS.Net.DocsExamples/Advanced/IntroPage.cs#ping)] | ||
|
||
> [!NOTE] | ||
> [`NatsConnection`](xref:NATS.Client.Core.NatsConnection) establishes the first server connection when the first call to subscribe or publish is made. | ||
> You can also call the `ConnectAsync()` method explicitly to establish the connection before any other calls are made. | ||
## What's Next | ||
|
||
- [Serialization](serialization.md) is the process of converting an object into a format that can be stored or transmitted. | ||
- [Security](security.md) is an important aspect of any distributed system. NATS provides a number of security features to help you secure your applications. | ||
- [AOT Deployment](aot.md) is a way to deploy your applications as native platform executables, which produces faster startup times and better performance in most cases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Security | ||
|
||
NATS has a lot of [security features](https://docs.nats.io/nats-concepts/security) and .NET V2 client supports them all. | ||
All you need to do is to pass your credentials to the connection. | ||
|
||
[!code-csharp[](../../../tests/NATS.Net.DocsExamples/Advanced/SecurityPage.cs#user-pass)] | ||
|
||
See also [user authentication tests](https://github.com/nats-io/nats.net.v2/blob/main/tests/NATS.Client.Core.Tests/NatsConnectionTest.Auth.cs) for more examples. | ||
|
||
## Implicit TLS Connections | ||
|
||
As of NATS server version 2.10.4 and later, the server supports implicit TLS connections. | ||
This means that the client can connect to the server using the default port of 4222 and the server will automatically upgrade the connection to TLS. | ||
This is useful for environments where TLS is required by default. | ||
|
||
[!code-csharp[](../../../tests/NATS.Net.DocsExamples/Advanced/SecurityPage.cs#tls-implicit)] | ||
|
||
## Mutual TLS Connections | ||
|
||
The [server can require TLS certificates from a client](https://docs.nats.io/running-a-nats-service/configuration/securing_nats/auth_intro/tls_mutual_auth) to validate | ||
the client certificate matches a known or trusted CA and to provide authentication. | ||
|
||
You can set the TLS options to use your client certificates when connecting to a server which requires TLS Mutual authentication. | ||
|
||
[!code-csharp[](../../../tests/NATS.Net.DocsExamples/Advanced/SecurityPage.cs#tls-mutual)] | ||
|
||
> [!TIP] | ||
> #### Intermediate CA Certificates | ||
> | ||
> When connecting using intermediate CA certificates, it might not be possible to validate the client certificate and | ||
> TLS handshake may fail. | ||
> | ||
> Unfortunately, for .NET client applications it isn't possible to pass additional intermediate certificates and the | ||
> only solution is to add the certificates to the certificate store manually. | ||
> | ||
> See also .NET documentation on [Troubleshooting SslStream authentication issues](https://learn.microsoft.com/en-us/dotnet/core/extensions/sslstream-troubleshooting#intermediate-certificates-are-not-sent) |
Oops, something went wrong.