1

我在 Redis 上使用事务并使用 StackExchange.Redis 提供程序。

我有大量用于事务的 StringSetAsync 操作。

我在使用 StringSetAsync 时遇到了错误:

RuntimeBinderException 被捕获

“StackExchange.Redis.ITransaction”不包含“StringSetAsync”的定义

堆栈跟踪:

在 CallSite.Target(Closure, CallSite, ITransaction, String, Object) 在 Repository.RedisDatabaseContextBase.SetRecord(IBasicRedisEntity redisEntity, Boolean isNewRecord)

====

添加:

这是反映问题的代码示例。Marc 是对的,一切都与动态有关。

try
{
  ConnectionMultiplexer cm = ConnectionMultiplexer.Connect("localhost:6380,allowAdmin=true");
  var db = cm.GetDatabase();

  ITransaction transaction = db.CreateTransaction();

  dynamic pp = new byte[5] {1, 2, 3, 4, 5};

  transaction.StringSetAsync("test", pp);

  if (transaction.Execute())
  {
    Console.Write("Committed");
  }
  else
  {
    Console.Write("UnCommitted");
  }

  Console.ReadLine();
}
catch (Exception e)
{
  Console.WriteLine(e);
}
4

1 回答 1

0

在某些方面它是正确的:不存在需要byte[]. 有一个需要RedisValue,但那不是一回事dynamic方法解析可能会出现故障 - 对于显式接口实现和转换运算符:两者都适用于这里!

我会建议:

object pp = new byte[5] {1, 2, 3, 4, 5};

if(pp is byte[])
    transaction.StringSetAsync("test", (byte[])pp);
else if (pp is string)
    transaction.StringSetAsync("test", (string)pp);
else if (pp is RedisValue)
    transaction.StringSetAsync("test", (RedisValue)pp);
else
    throw new NotSupportedException(
        "Value is not usable: " + pp.GetType().FullName);

另一种选择可能是:

dynamic pp = new byte[5] {1, 2, 3, 4, 5};
transaction.StringSetAsync("test", (RedisValue)pp);

在理论上应该可行,但在 IMO 中仍然有点不必要。

于 2015-01-22T08:13:19.563 回答