2

我想知道是否可以通过创建订阅Mongo服务Get Event Store?也许我的措辞不正确,但让我解释一下。我目前有一个将事件写入Mongo Databaseusing的过程NEventStore。我想做的是有一个订阅服务,它订阅一个Streamin Mongo

无法在互联网上找到任何关于此的信息,但这可能吗?简而言之,我的问题也许是您可以将两者混合并匹配在一起,还是为了做到这一点,我必须将我的事件写到eventstore而不是Mongo?也许,我正在解决这个错误,还有其他选择吗?

我可以看到我的事件正在写入,但是它无法触发EventAppeared。目前所有这些都在我的机器上本地完成。

我尝试创建一个精简的应用程序来执行此操作:

  1. 使用以下命令创建订阅

        using (var connection = EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, 1113)))
        {
            connection.SubscribeToStreamAsync(@"mongodb://localhost:27017/Test", false, EventAppeared, SubscriptionDropped);
    
            var repository = new NEventStoreRepository();
            repository.Write(new SomethingHasHappened("Hello"));
    
            Console.ReadLine();
        }
    
    private static void SubscriptionDropped(EventStoreSubscription arg1, SubscriptionDropReason arg2, Exception arg3)
    {
    }
    
    private static void EventAppeared(EventStoreSubscription arg1, ResolvedEvent arg2)
    {
    }
    
  2. 我通过 NEventStore 将事件写入我的 mongo 数据库

    public void Write(object @event)
    {
        var id = Guid.NewGuid();
    
        using (var scope = new TransactionScope())
        {
            using (var store = WireupEventStore())
            {
                using (var stream = store.OpenStream(id.ToString(), 0, int.MaxValue))
                {
                    stream.Add(new EventMessage { Body = @event });
                    stream.CommitChanges(Guid.NewGuid());
                    scope.Complete();
                }
            }
        }
    
        Console.ReadKey();
    }
    
    private static IStoreEvents WireupEventStore()
    {
        return Wireup
            .Init()
            .LogToOutputWindow()
            .UsingMongoPersistence("NEventStore.MongoDB", new DocumentObjectSerializer())
            .InitializeStorageEngine()
            .UsingJsonSerialization()
            .Build();
    }
    
4

1 回答 1

2

正常的事件流程如下:

(鉴于一切都已安装并正在运行......)

  1. 在应用程序代码的 GetEventStore 中注册流的订阅者
  2. 将事件保存到流
  3. 事件出现在您的订阅者中

我认为您要么混淆了事物的流程,要么试图做一些完全不受支持的事情(比如让 MongoDb 订阅 GetEventStore)。我认为您的代码正在做的是:

  1. 设置 NEventStore 以保存到 MongoDb
  2. 订阅 GetEventStore 中名为“mongodb://localhost:27017/Test”的流
  3. 将事件保存到 MongoDb

据我所知,您永远不会将任何事件保存到 GetEventStore,因此为什么 EventAppeared 方法中没有出现任何事件。您正在保存到 MongoDb。

[更新]

我想订阅一个 Mongodb 流并填充 GetEventStore,我相信从你的回答中收集到的内容是不可能的。

MongoDb 没有流,它有集合——它是一个文档数据库。流是 GetEventStore 中的一个概念。但是,看起来 NEventStore 允许您连接消息调度程序,这大概意味着您可以注册处理程序以侦听事件。在这些处理程序中,您可以保存到 GetEventStore。

于 2016-08-05T13:24:15.897 回答