在一个简单的测试场景中,可以使用以下方法进行设置:
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;
}
我可以对索引运行一个简单的同步查询并获得预期的结果(类型为Summary的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
...
我怀疑这是一个bug,但作为一个RavenDB新手,我首先要排除我在这里搞砸了一些东西的可能性。任何人都可以看到为什么这段代码会失败?
(注意:这是在Build 721和701上运行,两者都产生相同的结果)
感谢您提供的任何帮助。