我正在使用Sider客户端连接到运行在我的windows 7机器上的C#服务器。https://github.com/chakrit/sider
我能够从我的C#应用程序中触发set/get/select
现在我想使用Publish/Subscribe特性,以便我的C#应用程序可以以偶发的方式(传递委托)的方式被告知redis客户端的“键”的任何更改。
我无法为此编写代码,因为没有关于如何使用内幕客户端页面的示例。
我所能写的就是:
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 TestAppobb.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 发布于 2016-05-03 14:30:20
您需要订阅实际可观察到的产品。就像这样:
obb.Subscribe(x => Debug.WriteLine(x.ToString()));不要忘记添加using System.Reactive.Linq;以获得将lambda转换为观察者所需的扩展。
https://stackoverflow.com/questions/36958559
复制相似问题