0
votes

In a simple test scenario which can be setup using the following:

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

I can run a simple synchronous query against the index and get the expected results (2 rows output of type Summary):

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

However, if I try the same thing using an asynchronous query:

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

I get an 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

...

I suspect this is a bug, but as a RavenDB newbie, I first wanted to rule out the possibility I have screwed something up here. Can anyone see why this code would be failing?

(Note: this was run on Build 721 and on 701 and both produce the same results)

Thanks for any assistance you can provide.

1

1 Answers

0
votes

It looks like a bug. I don't see anything wrong in your code. I suggest you create an issue here: http://issues.hibernatingrhinos.com