Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JetStream Publish Serialization error #192

Open
alberk8 opened this issue Feb 28, 2024 · 3 comments
Open

JetStream Publish Serialization error #192

alberk8 opened this issue Feb 28, 2024 · 3 comments
Assignees

Comments

@alberk8
Copy link

alberk8 commented Feb 28, 2024

I have the following code with error Can't Serialize on the await jsContext.PublishAsync<Person>("data.person", data: person); . What am I doing wrong here?

Environment
NATS server 2.10.11 with JetStream enabled
.Net 8.0 C#
nuget: NATS.Client.JetStream 2.1.2

namespace ConsoleNatsPublisher
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            var opt = new NatsOpts
            {
                Url = "nats://127.0.0.1:4222",
                SerializerRegistry = NatsDefaultSerializerRegistry.Default,
                Name = "Nats_Connect"
            };

            var connect = new NatsConnection(opt);

            var jsContext = new NatsJSContext(connect) ;
           
            var stream = await jsContext.CreateStreamAsync(new StreamConfig("EVENTS", new[] { "data.>" }));

            var person = new Person { Name = "Nats", Address = "123 Address" };

            await jsContext.PublishAsync<Person>("data.person", data: person);

            Console.WriteLine("Done");
            Console.ReadLine();
        }

        public class Person
        {
            public string Name { get; set; } = string.Empty;
            public string Address { get; set; } = string.Empty;
        }
    }
}
@Jarema
Copy link
Contributor

Jarema commented Feb 28, 2024

@scottf might know the answer :)

@alberk8
Copy link
Author

alberk8 commented Feb 28, 2024

@Jarema Thanks for looking into this. I think I might have found the answer.
Adding more info in the Intellisense would be very helpful.

Install the nuget NATS.Client.Serializers.Json

using NATS.Client.Serializers.Json;
 var opt = new NatsOpts
 {
     Url = "nats://127.0.0.1:4222",
     SerializerRegistry = NatsJsonSerializerRegistry.Default,
     Name = "Nats_Connect"
 };

@scottf scottf assigned scottf and mtmk and unassigned mtmk Jun 15, 2024
@scottf
Copy link
Collaborator

scottf commented Jun 15, 2024

@alberk8 Can I close this or is there something that might need to be added to an example? If there is, you are welcome to contribute something and I'll help you through the PR. It also might make sense to add something directly to the client repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants