奥尔良粮食客户与粮食之间的双向沟通

时间:2016-07-18 17:27:17

标签: orleans

我想在grain方法Eg中实现对grain客户端的回调。在评估Grain.Method1时,需要调用客户端方法来获取一些数据。

我已尝试使用流,但是当我在客户端上订阅流时,该方法不会触发。

谷物:

var config = ClusterConfiguration.LocalhostPrimarySilo();
config.AddMemoryStorageProvider();
config.Globals.RegisterStorageProvider<MemoryStorage>("PubSubStore");
config.Globals.RegisterStreamProvider<SimpleMessageStreamProvider>("MySMSProvider");
...
public override async Task OnActivateAsync() {
var streamProvider  = GetStreamProvider("MySMSProvider");
var stream = streamProvider.GetStream<MyTypeMessage>(myGuid, "MyStream");

RegisterTimer(s => {
   return stream.OnNextAsync(new MyTypeMessage());
}, null, TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(1000));
...

客户:

var clientConfiguration = ClientConfiguration.LocalhostSilo();
clientConfiguration.RegisterStreamProvider<SimpleMessageStreamProvider>("MySMSProvider");
GrainClient.Initialize(clientConfiguration);
...
var streamProvider = GrainClient.GetStreamProvider("MySMSProvider");
var stream = requestStreamProvider.GetStream<MyTypeMessage>(myGuid, "MyStream");
await stream.SubscribeAsync(
            async (message, token) => { process message that does not fire }); 

1 个答案:

答案 0 :(得分:3)

您似乎没有保留对订阅

返回的 var subscriptionHandle = await _factCalculationRequestStream .SubscribeAsync(async (message, token) => { process message that does not fire }); 的引用

尝试这样的事情: subscriptionHandle 并保持IGrainObserver不被垃圾收集

在我的情况下,我最终在我的课程上实现public class MySocketService: WebSocketBehavior, IGrainObserver, IDisposable { private StreamSubscriptionHandle<MyStreamEvent> ... //inside some method invoked externally _streamSubscriberHandle = await requestedStream .SubscribeAsync(onStreamMessage, onStreamError, onStreamComplete); } (这是WebAPI项目中的websocketBehaviour,这个订阅效果很好:

split