We are working on bumping our revision for NServiceBus.Persistence.Sql
from 4.5.1
to 6.5.1
with NServiceBus
version fixed to 7.2.4
.
In some of our integration tests, we peek the saga data using the code pasted below. Unfortunately, this code (we don’t know the origin, the developer who built it has left a while ago) now throws exception as follows when attempting to get a session:
System.NullReferenceException: Object reference not set to an instance of an object.
at CurrentSessionHolder.SetCurrentSession(StorageSession session) in /_/src/SqlPersistence/SynchronizedStorage/CurrentSessionHolder.cs:line 11
at SynchronizedStorage.OpenSession(ContextBag contextBag) in /_/src/SqlPersistence/SynchronizedStorage/SynchronizedStorage.cs:line 24
When debugged, we can see it as follows:
Oddly enough, in our application code (not the test code), we can still inject ISynchronizedStorage
and pipelineContext.Value
is not null (as an experiment).
Our integration test framework spins up the endpoint host and uses the same IoC, it’s really hard to tell what’s wrong.
However, the Sql.Persistence
library apparently has changed drastically. So, we are wondering perhaps there is a different way to achieve the same (peek saga data). Or even better, we are missing a now-mandatory initialization step in our implementation. Here’s the code:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Extensibility;
using NServiceBus.Persistence;
using NServiceBus.Sagas;
using NServiceBus.Transport;
namespace ND3Framework.ServiceBus.NServiceBus7.Sagas
{
public class GetSagaData : IGetSagaData
{
private readonly ISagaPersister _sagaPersister;
private readonly ISynchronizedStorage _synchronizedStorage;
public GetSagaData(ISagaPersister sagaPersister,
ISynchronizedStorage synchronizedStorage)
{
_sagaPersister = sagaPersister;
_synchronizedStorage = synchronizedStorage;
}
public async Task<T> Get<T>(Guid id)
where T : class, IContainSagaData
{
using (var session = await GetSession().ConfigureAwait(false))
{
var sagaData = await _sagaPersister.Get<T>(id, session, new ContextBag()).ConfigureAwait(false);
return sagaData;
}
}
public async Task<T> Get<T>(string property, object value) where T : class, IContainSagaData
{
using (var session = await GetSession().ConfigureAwait(false))
{
var sagaData = await _sagaPersister.Get<T>(property, value, session, new ContextBag()).ConfigureAwait(false);
return sagaData;
}
}
private async Task<CompletableSynchronizedStorageSession> GetSession()
{
var context = new ContextBag();
context.Set(new IncomingMessage(Guid.NewGuid().ToString(), new Dictionary<string, string>(), new byte[] { }));
return await _synchronizedStorage.OpenSession(context).ConfigureAwait(false);
}
}
}
Any help / clues / insights will be appreciated.