如何使用 Castle Windsor 创建客户端版本 > 3.0.3660 的 RavenDB 会话?
How do I use Castle Windsor to create a RavenDB session with client version > 3.0.3660?
我正在使用 Castle Windsor v3.4.0 创建 RavenDB 文档会话实例,但是当我使用高于 3.0.3660 的 RavenDB 客户端版本时,我在调用 Store 方法时遇到此错误:
Castle.MicroKernel.ComponentNotFoundException: 'No component for supporting the service System.Net.Http.HttpMessageHandler was found'
这是我能想出的最小的重现错误的代码:
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Raven.Client;
using Raven.Client.Document;
public class Program
{
public static void Main()
{
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component
.For<IDocumentStore>()
.ImplementedBy<DocumentStore>()
.DependsOn(new { Url = "http://localhost:8081", DefaultDatabase = "Test" })
.OnCreate(x => x.Initialize())
.LifeStyle.Singleton,
Component
.For<IDocumentSession>()
.UsingFactoryMethod(x => x.Resolve<IDocumentStore>().OpenSession())
.LifeStyle.Transient);
using (var documentSession = container.Resolve<IDocumentSession>())
{
documentSession.Store(new object());
documentSession.SaveChanges();
}
}
}
这是我认为正在发生的事情。 v3.0.3660 之后对 RavenDB 客户端进行了更改,更改了在 HttpJsonRequest 中创建 HttpMessageHandler 的方式 class:
https://github.com/ravendb/ravendb/commit/740ad10d42d50b1eff0fc89d1a6894fd57578984
我相信这一变化与我在 Windsor 容器中使用 TypedFactoryFacility 相结合导致 RavenDB 请求 HttpJsonRequestFactory 的一个实例,它是 Windsor 的依赖项,而不是使用它自己的内部实例。
如何更改我的代码以避免此问题,以便我可以使用更新版本的 RavenDB 客户端?
鉴于您的 MVCE,Windsor 已设置为注入对象的属性。因此,在创建 DocumentStore
时,Castle 试图找到 HttpMessageHandlerFactory
属性 的值但失败了,因为没有为该特定类型配置任何内容。
我能够通过过滤掉 属性:
让你的例子工作(至少,它必须将数据插入我不存在的服务器)
container.Register(
Component.For<IDocumentStore>()
.ImplementedBy<DocumentStore>()
.DependsOn(new { Url = "http://localhost:8081", DefaultDatabase = "Test" })
.OnCreate(x => x.Initialize())
.PropertiesIgnore(p => p.Name == nameof(DocumentStore.HttpMessageHandlerFactory))
.LifeStyle.Singleton);
或者,如果您有它的值,您可以将它添加到传递给 DependsOn()
的对象中。
我正在使用 Castle Windsor v3.4.0 创建 RavenDB 文档会话实例,但是当我使用高于 3.0.3660 的 RavenDB 客户端版本时,我在调用 Store 方法时遇到此错误:
Castle.MicroKernel.ComponentNotFoundException: 'No component for supporting the service System.Net.Http.HttpMessageHandler was found'
这是我能想出的最小的重现错误的代码:
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Raven.Client;
using Raven.Client.Document;
public class Program
{
public static void Main()
{
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component
.For<IDocumentStore>()
.ImplementedBy<DocumentStore>()
.DependsOn(new { Url = "http://localhost:8081", DefaultDatabase = "Test" })
.OnCreate(x => x.Initialize())
.LifeStyle.Singleton,
Component
.For<IDocumentSession>()
.UsingFactoryMethod(x => x.Resolve<IDocumentStore>().OpenSession())
.LifeStyle.Transient);
using (var documentSession = container.Resolve<IDocumentSession>())
{
documentSession.Store(new object());
documentSession.SaveChanges();
}
}
}
这是我认为正在发生的事情。 v3.0.3660 之后对 RavenDB 客户端进行了更改,更改了在 HttpJsonRequest 中创建 HttpMessageHandler 的方式 class:
https://github.com/ravendb/ravendb/commit/740ad10d42d50b1eff0fc89d1a6894fd57578984
我相信这一变化与我在 Windsor 容器中使用 TypedFactoryFacility 相结合导致 RavenDB 请求 HttpJsonRequestFactory 的一个实例,它是 Windsor 的依赖项,而不是使用它自己的内部实例。
如何更改我的代码以避免此问题,以便我可以使用更新版本的 RavenDB 客户端?
鉴于您的 MVCE,Windsor 已设置为注入对象的属性。因此,在创建 DocumentStore
时,Castle 试图找到 HttpMessageHandlerFactory
属性 的值但失败了,因为没有为该特定类型配置任何内容。
我能够通过过滤掉 属性:
让你的例子工作(至少,它必须将数据插入我不存在的服务器)container.Register(
Component.For<IDocumentStore>()
.ImplementedBy<DocumentStore>()
.DependsOn(new { Url = "http://localhost:8081", DefaultDatabase = "Test" })
.OnCreate(x => x.Initialize())
.PropertiesIgnore(p => p.Name == nameof(DocumentStore.HttpMessageHandlerFactory))
.LifeStyle.Singleton);
或者,如果您有它的值,您可以将它添加到传递给 DependsOn()
的对象中。