2017-07-12 157 views
1

调用存储方法时,我使用温莎城堡v3.4.0创建RavenDB文件会话实例,但是当我后来使用RavenDB客户端版本低于3.0.3660我得到这个错误最小的代码我可以拿出,再现错误:如何使用Castle Windsor创建客户端版本> 3.0.3660的RavenDB会话?</p> <pre><code>Castle.MicroKernel.ComponentNotFoundException: 'No component for supporting the service System.Net.Http.HttpMessageHandler was found' </code></pre> <p>这里是:

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改变了HttpMessageHandler如何在HttpJsonRequest类创建后的RavenDB客户端进行:

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

我相信这个变化,结合我在我的温莎容器使用TypedFactoryFacility的导致RavenDB请求HttpJsonRequestFactory的一个实例,并且它是来自Windsor的依赖关系,而不是使用它自己的内部实例。

如何更改我的代码以避免此问题,以便我可以使用更新版本的RavenDB客户端?

回答

3

鉴于您的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()的对象。

相关问题