2016-03-14 42 views
1

我有以下代碼:ServiceStack 4許可

class Program 
{ 
    static void Main(string[] args) 
    { 
     var clientManager = new BasicRedisClientManager("127.0.0.1:6379"); 
     var person = new Person {Name = "Maria"}; 
     using (var redis = clientManager.GetClient()) 
     { 
      var redisPerson = redis.As<Person>(); 
      redis.StoreAsHash(redisPerson); 
     } 
    } 
} 
public class Person 
{ 
    public string Name { get; set; } 
} 

}

,並得到錯誤「關於'20 ServiceStack.Text類型的免費配額已達到極限......」 不好意思...... 20個ServiceStack.Text類型在哪裏?我錯過了什麼嗎? 感謝

回答

1

問題是redis.As<Person>()返回真實類型,如果你刪除的類型推斷redisPerson這是更清晰的Person實體entire Generic Redis Client

IRedisTypedClient<Person> redisPerson = redis.As<Person>(); 
redis.StoreAsHash(redisPerson); 

所以這個代碼試圖嘗試序列化的不當使用和使用未你想做的事無類型RedisClient保存整個類型化的Redis客戶端,而不是你應該使用類型redisPerson Redis的客戶節省Person實體,這裏是你的程序的適當例子:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var clientManager = new BasicRedisClientManager("127.0.0.1:6379"); 

     var person = new Person { Id = 1, Name = "Maria" }; 
     using (var redis = clientManager.GetClient()) 
     { 
      var redisPerson = redis.As<Person>(); 
      redisPerson.StoreAsHash(person); 

      var fromRedis = redisPerson.GetFromHash(person.Id); 
      fromRedis.PrintDump(); 
     } 

     Console.ReadLine(); 
    } 
} 

public class Person 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

,輸出:

{ 
    Id: 1, 
    Name: Maria 
} 

還要注意的是絕大多數類型的API的要求每個實體都有它用於創建實體存儲在密鑰的標識主鍵。有關complex types are stored in ServiceStack.Redis的更多信息,請參閱此答案。