2012-03-31 74 views
0

拋出異常在一個簡單的測試方案,可以使用以下設置:AsProjection()用於異步查詢

public class TestObj 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
} 

public class Summary 
{ 
    public string MyId { get; set; } 
    public string MyName { get; set; } 
} 

public class TestObjs_Summary : AbstractIndexCreationTask<TestObj, Summary> 
{ 
    public TestObjs_Summary() 
    { 
     Map = docs => docs.Select(d => new { MyId = d.Id, MyName = d.Name }); 

     Store(x => x.MyId, FieldStorage.Yes); 
     Store(x => x.MyName, FieldStorage.Yes); 
    } 
} 

static IDocumentStore Setup() 
{ 
    var store = new DocumentStore() { Url="http://localhost:8080" }; 
    store.Initialize(); 
    IndexCreation.CreateIndexes(MethodInfo.GetCurrentMethod().DeclaringType.Assembly, store); 

    using (var session = store.OpenSession()) 
    { 
     session.Store(new TestObj { Name = "Doc1" }); 
     session.Store(new TestObj { Name = "Doc2" }); 
     session.SaveChanges(); 
    } 
    return store; 
} 

我可以對指數運行一個簡單的同步查詢,並得到預期的結果(2行輸出類型的摘要):

using (var session = store.OpenSession()) 
{ 
    var q = session.Query<Summary>("TestObjs/Summary").AsProjection<Summary>(); 
    Dump("Sync:", q.ToList()); 
} 

但是,如果我嘗試同樣的事情,使用異步查詢:

using (var session = store.OpenAsyncSession()) 
{ 
    var q = session.Query<Summary>("TestObjs/Summary").AsProjection<Summary>(); 
    q.ToListAsync().ContinueWith(t => Dump("Async:", t.Result)); 
} 

我得到一個InvalidCastException:

InnerException: System.InvalidCastException 
Message=Unable to cast object of type 'TestObj' to type 'Summary'. 
Source=Raven.Client.Lightweight 
StackTrace: 
at Raven.Client.Document.InMemoryDocumentSessionOperations.ConvertToEntity[T](String id, RavenJObject documentFound, RavenJObject metadata) in c:\Builds\RavenDB-Unstable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:line 416 
at Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity[T](String key, RavenJObject document, RavenJObject metadata) in c:\Builds\RavenDB-Unstable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:line 340 
at Raven.Client.Document.SessionOperations.QueryOperation.Deserialize[T](RavenJObject result) in c:\Builds\RavenDB-Unstable\Raven.Client.Lightweight\Document\SessionOperations\QueryOperation.cs:line 130 

... 

我懷疑這是一個錯誤,但作爲一個新手RavenDB,我首先要排除我搞砸了的東西在這裏的可能性。任何人都可以看到爲什麼這段代碼會失敗嗎?

(注:這是對構建721和701上運行,都產生了相同的結果)

感謝您可以提供任何幫助。

回答

0

它看起來像一個錯誤。我的代碼中沒有看到任何錯誤。我建議你在這裏創建一個問題:http://issues.hibernatingrhinos.com

+0

謝謝丹尼爾。記錄的問題http://issues.hibernatingrhinos.com/issue/RavenDB-191 – 2012-03-31 10:32:26