Skip to content

Instantly share code, notes, and snippets.

@flew2bits
Last active February 3, 2024 00:54
Show Gist options
  • Save flew2bits/c89bec4cd5a893f63b8f6af96c200611 to your computer and use it in GitHub Desktop.
Save flew2bits/c89bec4cd5a893f63b8f6af96c200611 to your computer and use it in GitHub Desktop.
Repro for Marten 7 beta 5 regression
using Marten;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Wolverine;
var builder = Host.CreateDefaultBuilder();
builder.UseWolverine();
builder.ConfigureServices(services =>
{
services.AddMarten(opts =>
{
opts.Connection(
"Host=localhost;Port=5436;User Id=postgres;Password=psql;Database=postgres;Persist Security Info=true");
});
services.AddHostedService<Worker>();
});
await builder.Build().RunAsync();
public class Worker : BackgroundService
{
private readonly IDocumentStore _store;
private readonly IHostApplicationLifetime _lifetime;
private readonly ILogger<Worker> _logger;
public Worker(IDocumentStore store, IHostApplicationLifetime lifetime, ILogger<Worker> logger)
{
_store = store;
_lifetime = lifetime;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var id = Guid.NewGuid();
await using (var sessionOne = _store.LightweightSession())
{
sessionOne.Store(new DocumentA(id, "Document A"));
sessionOne.Store(new DocumentB(id, id, "Document B"));
await sessionOne.SaveChangesAsync(stoppingToken);
}
await using (var sessionTwo = _store.LightweightSession())
{
try
{
DocumentB? documentB1 = null;
var documentA1 = await sessionTwo.Query<DocumentA>()
.Include<DocumentB>(a => a.Id, x => documentB1 = x)
.SingleOrDefaultAsync(a => a.Id == id, stoppingToken);
_logger.LogInformation("A1: {DocADetail} B1: {DocBDetail}", documentA1?.Detail ?? "NONE", documentB1?.OtherDetail ?? "NONE");
}
catch (Exception ex)
{
_logger.LogError(ex, "Got an exception");
}
}
await using var sessionThree = _store.LightweightSession();
DocumentA? documentA2 = null;
var documentB2 = await sessionThree.Query<DocumentB>()
.Include<DocumentA>(b => b.AId, a => documentA2 = a)
.SingleOrDefaultAsync(b => b.Id == id, stoppingToken);
_logger.LogInformation("A2: {DocADetail} B2: {DocBDetail}", documentA2?.Detail ?? "NONE", documentB2?.OtherDetail ?? "NONE");
_lifetime.StopApplication();
}
}
public record DocumentA(Guid Id, string Detail);
public record DocumentB(Guid Id, Guid AId, string OtherDetail);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment