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

NullReferenceException when using JsonSetAsync with CommandFlags.FireAndForget #26

Open
jordankasper11 opened this issue Mar 12, 2023 · 0 comments

Comments

@jordankasper11
Copy link

jordankasper11 commented Mar 12, 2023

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!

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

1 participant