嵌入模式下不支持RavenDB IsOperationAllowedOnDocument

san*_*ovm 5 ravendb

使用嵌入模式调用InvalidOperationExceptionIsOperationAllowedOnDocument会抛出RavenDB .

我可以在IsOperationAllowedOnDocument实现中看到一个子句检查嵌入模式下的调用.

namespace Raven.Client.Authorization
{
    public static class AuthorizationClientExtensions
    {
        public static OperationAllowedResult[] IsOperationAllowedOnDocument(this ISyncAdvancedSessionOperation session, string userId, string operation, params string[] documentIds)
        {
            var serverClient = session.DatabaseCommands as ServerClient;
            if (serverClient == null)
                throw new InvalidOperationException("Cannot get whatever operation is allowed on document in embedded mode.");
Run Code Online (Sandbox Code Playgroud)

除了不使用嵌入模式之外,还有其他解决方法吗?

谢谢你的时间.

Dov*_*ars 4

我在编写一些单元测试时遇到了同样的情况。詹姆斯提供的解决方案奏效了;然而,这导致了一个用于单元测试的代码路径和另一个用于生产代码的路径,这违背了单元测试的目的。我们能够创建第二个文档存储并将其连接到第一个文档存储,这使我们能够成功访问授权扩展方法。虽然此解决方案可能不适合生产代码(因为创建文档存储的成本很高),但它非常适合单元测试。这是一个代码示例:

using (var documentStore = new EmbeddableDocumentStore
        { RunInMemory = true,
          UseEmbeddedHttpServer = true,
          Configuration = {Port = EmbeddedModePort} })
{
    documentStore.Initialize();
    var url = documentStore.Configuration.ServerUrl;

    using (var docStoreHttp = new DocumentStore {Url = url})
    {
        docStoreHttp.Initialize();

        using (var session = docStoreHttp.OpenSession())
        {
            // now you can run code like:
            // session.GetAuthorizationFor(),
            // session.SetAuthorizationFor(),
            // session.Advanced.IsOperationAllowedOnDocument(),
            // etc...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

还有其他几项值得一提:

  1. 第一个文档存储需要在 UseEmbeddedHttpServer 设置为 true 的情况下运行,以便第二个文档存储可以访问它。
  2. 我为端口创建了一个常量,以便一致地使用它并确保使用非保留端口。