Skip to content

Commit

Permalink
Encoded connections are no longer supported (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Jul 10, 2024
1 parent e5aaea9 commit ec8fe87
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 124 deletions.
123 changes: 0 additions & 123 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,129 +287,6 @@ instead of `NATS.RX.Ops`.

See the full example here: [RxSample](src/Samples/RxSample/RxSample.cs)

## Basic Encoded Usage
The .NET NATS client mirrors go encoding through serialization and
deserialization. Simply create an encoded connection and publish
objects, and receive objects through an asynchronous subscription using
the encoded message event handler. The .NET 4.6 client has a default formatter
serializing objects using the BinaryFormatter, but methods used to serialize and deserialize
objects can be overridden. The NATS core version does not have serialization
defaults and they must be specified.

```c#
using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
{
EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
{
// Here, obj is an instance of the object published to
// this subscriber. Retrieve it through the
// ReceivedObject property of the arguments.
MyObject obj = (MyObject)args.ReceivedObject;

System.Console.WriteLine("Company: " + obj.Company);
};

// Subscribe using the encoded message event handler
IAsyncSubscription s = c.SubscribeAsync("foo", eh);

MyObject obj = new MyObject();
obj.Company = "MyCompany";

// To publish an instance of your object, simply
// call the IEncodedConnection publish API and pass
// your object.
c.Publish("foo", obj);
c.Flush();
}
```

### Other Types of Serialization
Optionally, one can override serialization. Depending on the level of support or
third party packages used, objects can be serialized to JSON, SOAP, or a custom
scheme. XML was chosen as the example here as it is natively supported by .NET 4.6.

```c#
// Example XML serialization.
byte[] serializeToXML(Object obj)
{
MemoryStream ms = new MemoryStream();
XmlSerializer x = new XmlSerializer(((SerializationTestObj)obj).GetType());

x.Serialize(ms, obj);

byte[] content = new byte[ms.Position];
Array.Copy(ms.GetBuffer(), content, ms.Position);

return content;
}

Object deserializeFromXML(byte[] data)
{
XmlSerializer x = new XmlSerializer(new SerializationTestObj().GetType());
MemoryStream ms = new MemoryStream(data);
return x.Deserialize(ms);
}

<...>

// Create an encoded connection and override the OnSerialize and
// OnDeserialize delegates.
IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection();
c.OnDeserialize = deserializeFromXML;
c.OnSerialize = serializeToXML;

// From here on, the connection will use the custom delegates
// for serialization.
```

One can also use `Data Contract` to serialize objects. Below are simple example
overrides that work with .NET core:

```c#
[DataContract]
public class JsonObject
{
[DataMember]
public string Value = "";
}

internal object jsonDeserializer(byte[] buffer)
{
using (MemoryStream stream = new MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof(JsonObject));
stream.Write(buffer, 0, buffer.Length);
stream.Position = 0;
return serializer.ReadObject(stream);
}
}

internal byte[] jsonSerializer(object obj)
{
if (obj == null)
return null;

var serializer = new DataContractJsonSerializer(typeof(JsonObject));

using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, obj);
return stream.ToArray();
}
}

<...>

// Create an encoded connection and override the OnSerialize and
// OnDeserialize delegates.
IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection();
c.OnDeserialize = jsonDeserializer;
c.OnSerialize = jsonSerializer;

// From here on, the connection will use the custom delegates
// for serialization.
```

## Wildcard Subscriptions

The `*` wildcard matches any token, at any level of the subject:
Expand Down
3 changes: 3 additions & 0 deletions src/NATS.Client/ConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public IConnection CreateConnection(Options opts, bool reconnectOnConnect = fals
}

/// <summary>
/// ENCODED CONNECTIONS, WHILE STILL FUNCTIONAL, WILL NO LONGER BE SUPPORTED
/// Attempt to connect to the NATS server, with an encoded connection, using the default options.
/// </summary>
/// <param name="reconnectOnConnect">if true, the connection will treat the initial connection as any other and attempt reconnects on failure</param>
Expand All @@ -241,6 +242,7 @@ public IEncodedConnection CreateEncodedConnection(bool reconnectOnConnect = fals
}

/// <summary>
/// ENCODED CONNECTIONS, WHILE STILL FUNCTIONAL, WILL NO LONGER BE SUPPORTED
/// Attempt to connect to the NATS server, with an encoded connection, referenced by <paramref name="url"/>.
/// </summary>
/// <remarks>
Expand All @@ -262,6 +264,7 @@ public IEncodedConnection CreateEncodedConnection(string url, bool reconnectOnCo
}

/// <summary>
/// ENCODED CONNECTIONS, WHILE STILL FUNCTIONAL, WILL NO LONGER BE SUPPORTED
/// Attempt to connect to the NATS server, with an encoded connection, using the given options.
/// </summary>
/// <param name="opts">The NATS client options to use for this connection.</param>
Expand Down
4 changes: 3 additions & 1 deletion src/NATS.Client/EncodedConn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace NATS.Client
public delegate Object Deserializer(byte[] data);

/// <summary>
/// ENCODED CONNECTIONS, WHILE STILL FUNCTIONAL, WILL NO LONGER BE SUPPORTED
/// Provides decoded messages received by subscriptions or requests.
/// </summary>
public class EncodedMessageEventArgs : EventArgs
Expand Down Expand Up @@ -89,7 +90,8 @@ public Msg Message
}

/// <summary>
/// Represents an <see cref="Connection"/> which uses a client specified
/// ENCODED CONNECTIONS, WHILE STILL FUNCTIONAL, WILL NO LONGER BE SUPPORTED
/// Represents a <see cref="Connection"/> which uses a client specified
/// encoding scheme.
/// </summary>
public class EncodedConnection : Connection, IEncodedConnection
Expand Down
1 change: 1 addition & 0 deletions src/NATS.Client/IEncodedConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace NATS.Client
{
/// <summary>
/// ENCODED CONNECTIONS, WHILE STILL FUNCTIONAL, WILL NO LONGER BE SUPPORTED
/// Represents a connection to a NATS Server which uses a client specified
/// encoding scheme.
/// </summary>
Expand Down

0 comments on commit ec8fe87

Please sign in to comment.