我一直在将我的代码迁移到RavenDB 4。
我注意到RavenDB 4中的侦听器自版本3以来已更改。在v3中,您使用了IDocumentStoreListener
和RegisterListener
但是在v4中,您直接在会话实例上订阅了BeforeStore
事件。
但是,我的BeforeStore
- 事件侦听器不会在异步会话中触发(但在同步会话中会触发)。这是设计,还是我缺少的东西?
我正在使用RavenDB客户端(.NET)和服务器的版本4.0.0-rc-40025
。
谢谢!
以下是重新创建问题的示例控制台应用程序:
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.
答案 0 :(得分:1)
不,在所有情况下都会调用此方法。
实际上,实际的调用机制根本不在Store
内部,而是在SaveChanges
或SaveChangesAsync