我正在使用Sider C#Redis客户端连接到在我的Windows 7机器上运行的Redis服务器。 https://github.com/chakrit/sider
我可以从我的C#应用程序中激活set / get / select
我现在想要使用发布/订阅功能,以便以的方式(传递代理人)通知我的C#应用程序有关redis客户端“密钥”的任何更改/ strong>
我无法编写代码,因为没有关于如何使用sider客户端页面的示例。
我能写的就是这个:
var client = new RedisClient(address, 6379);
string[] keys = new string[1];
keys[0] = "key1ToMonitor";
IObservable<Message<string>> obb = client.Subscribe(keys);
我知道这看起来很蹩脚,但我不知道如何以lambda方式编写它,如果任何客户端在redis服务器上更改了所需的密钥,我的函数将被调用。
PS:我是新手,如果我的方法存在缺陷,请纠正我。修改:在添加建议的更改时,我收到以下错误。
Error 7 Cannot convert lambda expression to type 'System.IObserver<Sider.Message<string>>' because it is not a delegate type D:\_Work\TestApp\Program.cs 90 27 TestApp
obb.subscribe签名看起来像这样
namespace System
{
// Summary:
// Defines a provider for push-based notification.
//
// Type parameters:
// T:
// The object that provides notification information.This type parameter is
// covariant. That is, you can use either the type you specified or any type
// that is more derived. For more information about covariance and contravariance,
// see Covariance and Contravariance in Generics.
public interface IObservable<out T>
{
// Summary:
// Notifies the provider that an observer is to receive notifications.
//
// Parameters:
// observer:
// The object that is to receive notifications.
//
// Returns:
// The observer's interface that enables resources to be disposed.
IDisposable Subscribe(IObserver<T> observer);
}
}
代码:
var client = new RedisClient(address, 6379);
string[] keys = new string[1];
keys[0] = "key1ToMonitor";
IObservable<Message<string>> obb = client.Subscribe(keys);
obb.Subscribe(x => Debug.WriteLine(x.ToString()) ); // error : doesn't let me compile
答案 0 :(得分:0)
您需要订阅生成的实际可观察量。像这样:
obb.Subscribe(x => Debug.WriteLine(x.ToString()));
不要忘记添加using System.Reactive.Linq;
以获取将lambda转换为观察者所需的扩展名。