2017-07-12 158 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()的對象。

相關問題