Skip to content

NullReferenceException when using JsonSetAsync with CommandFlags.FireAndForget #26

@jordankasper11

Description

@jordankasper11

I apologize if I'm overlooking something obvious, but I'm new to RedisJSON and am running into an issue while trying to do a pretty simple task. I'm trying to generate some sample products and add them to Redis, but I get a NullReferenceException when executing the following code:

var multiplexer = await ConnectionMultiplexer.ConnectAsync("localhost");
var redis = multiplexer.GetDatabase();

var departments = new string[] { "Men", "Women", "Kids", "Travel", "Electronics" };
var brands = new string[] { "Apple", "Microsoft", "Samsung", "Sony", "Nike", "Adidas", "Reebok" };
var colors = new string[] { "Black", "Blue", "Green", "Grey", "Orange", "Pink", "Purple", "Red", "White" };
var random = new Random();

var count = 0;

while (count < 1000000)
{
    var tasks = new List<Task>();

    while (tasks.Count < 10000)
    {
        count++;

        var product = new
        {
            Id = $"product-{count}",
            Department = departments.GetValue(random.Next(0, departments.Length)),
            Brand = brands.GetValue(random.Next(0, brands.Length)),
            Color = colors.GetValue(random.Next(0, colors.Length)),
            Name = $"Product {count}",
            Description = "<p>Sed interdum aliquet augue eget placerat. Aliquam ac orci placerat, mattis arcu quis, finibus est. Aenean erat purus, dignissim in dapibus ut, eleifend nec nunc.</p>",
            Price = Math.Round((0.01 + random.NextDouble()) * 250, 2)
        };

        var task = redis.JsonSetAsync(product.Id, product, commandFlags: CommandFlags.FireAndForget);

        tasks.Add(task);
    }

    await Task.WhenAll(tasks);
}

The error is triggered by this line of code:

var task = redis.JsonSetAsync(product.Id, product, commandFlags: CommandFlags.FireAndForget);

I can eliminate the error by replacing this line with either of the following:

var task = redis.JsonSetAsync(product.Id, product);

or

var json = JsonSerializer.Serialize(product);
var task = redis.SetAddAsync(product.Id, json, CommandFlags.FireAndForget);

I want to keep CommandFlags.FireAndForget as it is much faster, but it seems that combining this with NReJSON doesn't work.

If it makes any difference, this is my SerializerProxy:

public sealed class RedisJsonSerializer : ISerializerProxy
{
    public TResult Deserialize<TResult>(RedisResult serializedValue)
    {
        return JsonSerializer.Deserialize<TResult>(serializedValue.ToString());
    }

    public string Serialize<TObjectType>(TObjectType obj)
    {
        return JsonSerializer.Serialize(obj);
    }
}

Any assistance would be greatly appreciated. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions