NServiceBus/NServiceBus — Issue #175: InMemorySagaPersister causes InvalidCast exception
Author: bmatson
Created: 2012-01-06 12:55:43+00:00
State: open

In issue #77 a bug was fixed in the in-memory saga persister lookup by property to verify that the looked up value can actually be cast over to the required type.

I believe a related fix needs to be made to the lookup by sagaId GUID as well. In the app I’m working on I’ve got two sagas, both of which use timeouts. When the timeout message arrives it uses the GUID based version of the Get function, which just blindly casts the found saga data over to T. Looking at the code both in the 2.5 branch and the develop branch, this issue appears to exist in both places.

Changing the lookup to something along the lines of this should fix the problem:

```C#
T ISagaPersister.Get(Guid sagaId)
{
ISagaEntity result;
data.TryGetValue(sagaId, out result);

if (result != null && result is T)
return (T)result;

return default(T);
}