我一直在将我的代码迁移到 RavenDB 4。
我注意到 RavenDB 4 中的侦听器自版本 3 以来发生了变化。在 v3 中您使用过IDocumentStoreListener
,但在 v4 中您直接在会话实例上RegisterListener
订阅事件。BeforeStore
但是,我的BeforeStore
-event 侦听器不会在异步会话上触发(但会在同步会话上触发)。这是设计使然,还是我缺少的东西?
我正在使用4.0.0-rc-40025
RavenDB 客户端 (.NET) 和服务器的版本。
谢谢!
这是一个重新创建问题的示例控制台应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Raven.Client.Documents;
namespace BeforeStoreAsync
{
class Program
{
static void Main(string[] args)
{
IDocumentStore store = new DocumentStore
{
Urls = new[] { "http://localhost:8080" },
Database = "MyDatabase"
};
store.OnBeforeStore += (sender, eventArgs) =>
{
if (eventArgs.Entity is MyEntity entity)
entity.Description = DateTime.UtcNow.ToString();
};
store = store.Initialize();
var syncEntity = StoreEntitySync(store);
var asyncEntity = StoreEntityAsync(store).Result;
if (string.IsNullOrWhiteSpace(syncEntity.Description))
Console.WriteLine($"BeforeStore didn't run for {nameof(syncEntity)}.");
if (string.IsNullOrWhiteSpace(asyncEntity.Description))
Console.WriteLine($"BeforeStore didn't run for {nameof(asyncEntity)}.");
}
private static MyEntity StoreEntitySync(IDocumentStore store)
{
using(var session = store.OpenSession())
{
var entity = new MyEntity
{
Name = "Sync session"
};
session.Store(entity);
session.SaveChanges();
return entity;
}
}
private static async Task<MyEntity> StoreEntityAsync(IDocumentStore store)
{
using (var session = store.OpenAsyncSession())
{
var entity = new MyEntity
{
Name = "Async session"
};
await session.StoreAsync(entity);
await session.SaveChangesAsync();
return entity;
}
}
}
public class MyEntity
{
public string Name { get; set; }
public string Description { get; set; }
public string Id { get; set; }
}
}
上面的示例将向控制台输出以下内容:
BeforeStore didn't run for asyncEntity.