如何使用Castle Windsor使用客户端版本>创建RavenDB会话。 3.0.3660? [英] How do I use Castle Windsor to create a RavenDB session with client version > 3.0.3660?

查看:52
本文介绍了如何使用Castle Windsor使用客户端版本>创建RavenDB会话。 3.0.3660?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Castle Windsor v3.4.0创建一个RavenDB文档会话实例,但是当我使用3.0.3660之后的RavenDB客户端版本时,在调用Store方法时出现此错误:

  Castle.MicroKernel.ComponentNotFoundException:'未找到支持服务System.Net.Http.HttpMessageHandler的组件'

这是我能想到的最小的片段代码,它会重现错误:

 使用Castle.Facilities.TypedFactory; 
使用Castle.MicroKernel.Registration;
使用Castle.Windsor;
使用Raven.Client;
使用Raven.Client.Document;

公共类程序
{
public static void Main()
{
var container = new WindsorContainer();
container.AddFacility< TypedFactoryFacility>();

容器.Register(
组件
.For< IDocumentStore>()
.ImplementedBy< DocumentStore>()
.DependsOn(new { http:// localhost:8081,DefaultDatabase = Test})
.OnCreate(x => x.Initialize())
.LifeStyle.Singleton,
组件
.For< IDocumentSession>()
.UsingFactoryMethod(x => x.Resolve< IDocumentStore>()。OpenSession())
.LifeStyle.Transient);

使用(var documentSession = container.Resolve< IDocumentSession>())
{
documentSession.Store(new object());
documentSession.SaveChanges();
}
}
}

这是我认为正在发生的事情。 v3.0.3660之后,对RavenDB客户端进行了更改,从而更改了在HttpJsonRequest类中创建HttpMessageHandler的方式:



https://github.com/ravendb/ravendb/commit/740ad10d42d50b1eff0fc89d1a6894fd57578984
$ b >我相信,这种变化与我在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);

或者,如果您有值,则可以将其添加到传递给<$的对象中c $ c> DependsOn()。


I am using Castle Windsor v3.4.0 to create a RavenDB document session instance but when I use a RavenDB client version later than 3.0.3660 I get this error when calling the Store method:

Castle.MicroKernel.ComponentNotFoundException: 'No component for supporting the service System.Net.Http.HttpMessageHandler was found'

Here is the smallest piece code I can come up with that reproduces the error:

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();
        }
    }    
}

Here's what I believe is happening. A change was made to the RavenDB client after v3.0.3660 that changed how the HttpMessageHandler is created in the HttpJsonRequest class:

https://github.com/ravendb/ravendb/commit/740ad10d42d50b1eff0fc89d1a6894fd57578984

I believe that this change in combination with my use of the TypedFactoryFacility in my Windsor container is causing RavenDB to request an instance of HttpJsonRequestFactory and it's dependencies from Windsor rather than using it's own internal one.

How I can change my code to avoid this problem so that I can use a more recent version of the RavenDB client?

解决方案

Given your MVCE, Windsor is set up to inject object's properties. So, when creating the DocumentStore, Castle is trying to find a value for the HttpMessageHandlerFactory property and is failing since nothing is configured for that particular type.

I was able to get your example to work (at least, it got to inserting the data into my non-existing server) by just filtering out that property:

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);

Alternatively, if you have a value for it, you could add it to the object passed to DependsOn().

这篇关于如何使用Castle Windsor使用客户端版本&gt;创建RavenDB会话。 3.0.3660?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆