Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
<PackageVersion Include="Grpc.StatusProto" Version="2.76.0" />
<PackageVersion Include="Grpc.Tools" Version="2.76.0" />
<PackageVersion Include="HtmlTags" Version="9.0.0" />
<PackageVersion Include="JasperFx" Version="2.2.7" />
<PackageVersion Include="JasperFx.Events" Version="2.2.7" />
<PackageVersion Include="JasperFx.Events.SourceGenerator" Version="2.2.7" />
<PackageVersion Include="JasperFx" Version="2.3.0" />
<PackageVersion Include="JasperFx.Events" Version="2.3.0" />
<PackageVersion Include="JasperFx.Events.SourceGenerator" Version="2.3.0" />
<!-- RuntimeCompiler is on its own 5.x line (the Roslyn compiler package) — not the 2.1.x
family; it stays at 5.0.0. -->
<PackageVersion Include="JasperFx.RuntimeCompiler" Version="5.0.0" />
<PackageVersion Include="JasperFx.SourceGenerator" Version="2.2.7" />
<PackageVersion Include="JasperFx.SourceGenerator" Version="2.3.0" />
<PackageVersion Include="Marten" Version="9.2.0" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="6.1.3" />
<PackageVersion Include="Polecat" Version="4.2.1" />
Expand Down
22 changes: 20 additions & 2 deletions src/Http/Wolverine.Http.Tests/using_efcore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class using_efcore : IntegrationContext
public using_efcore(AppFixture fixture) : base(fixture)
{
}



[Fact]
Expand Down Expand Up @@ -65,7 +65,9 @@ public async Task using_db_context_with_outbox()
private async Task cleanItems()
{
await Host.ResetResourceState();

EfCoreEndpoints.DbContext = null;
EfCoreEndpoints.NestedDbContext = null;

var table = new Table("items");
table.AddColumn<Guid>("Id").AsPrimaryKey();
table.AddColumn<string>("Name");
Expand Down Expand Up @@ -149,4 +151,20 @@ public async Task using_db_context_with_outbox_schedule_nodb()
scheduledMessage.ScheduleDelay.ShouldNotBeNull();
scheduledMessage.Message.ShouldBeOfType<ItemCreated>();
}

[Fact]
public async Task http_handler_with_http_context_sourced_gets_the_same_service()
{
await cleanItems();

var _ = await TrackedHttpCall(x =>
{
x.Post.Json(new CreateItemCommand()).ToUrl("/ef/servicelocation");
x.StatusCodeShouldBe(204);
});

EfCoreEndpoints.DbContext.ShouldNotBeNull();
EfCoreEndpoints.NestedDbContext.ShouldNotBeNull();
EfCoreEndpoints.DbContext.ShouldBe(EfCoreEndpoints.NestedDbContext);
}
}
38 changes: 35 additions & 3 deletions src/Http/WolverineWebApi/EfCoreEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@

namespace WolverineWebApi;

public interface IServiceWithDbContextReference
{
ItemsDbContext DbContext { get; }
}

public interface IRandomService
{
double Next();
}

public class RandomService : IRandomService
{
private readonly Random _random = new();

public double Next() => _random.NextDouble();
}

public class ServiceWithDbContextReference(IRandomService randomService, ItemsDbContext dbContext) : IServiceWithDbContextReference
{
public ItemsDbContext DbContext { get; } = dbContext;
public IRandomService RandomService { get; } = randomService;
}

public class EfCoreEndpoints
{
[WolverinePost("/ef/create")]
Expand All @@ -19,13 +42,13 @@ public async Task PublishItem(CreateItemCommand command, ItemsDbContext db, IMes
db.Items.Add(item);
await bus.PublishAsync(new ItemCreated { Id = item.Id });
}

[WolverinePost("/ef/schedule")]
public async Task ScheduleItem(CreateItemCommand command, ItemsDbContext db, IMessageBus bus)
{
await bus.PublishAsync(new ItemCreated { Id = Guid.NewGuid() }, new(){ ScheduleDelay = 5.Days()});
await bus.PublishAsync(new ItemCreated { Id = Guid.NewGuid() }, new() { ScheduleDelay = 5.Days() });
}

[WolverinePost("/ef/schedule2"), EmptyResponse]
public static object ScheduleItem2(CreateItemCommand command)
{
Expand All @@ -37,4 +60,13 @@ public async Task ScheduleItem_NoDb(CreateItemCommand command, IMessageBus bus)
{
await bus.PublishAsync(new ItemCreated { Id = Guid.NewGuid() }, new() { ScheduleDelay = 5.Days() });
}

public static ItemsDbContext? DbContext { get; set; }
public static ItemsDbContext? NestedDbContext { get; set; }
[WolverinePost("/ef/servicelocation")]
public static void Handle(CreateItemCommand command, ItemsDbContext dbContext, IServiceWithDbContextReference serviceWrapper)
{
DbContext = dbContext;
NestedDbContext = serviceWrapper.DbContext;
}
}
6 changes: 6 additions & 0 deletions src/Http/WolverineWebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public static async Task<int> Main(string[] args)
builder.Services.AddKeyedSingleton<IThing, RedThing>("Red");
builder.Services.AddKeyedScoped<IThing, BlueThing>("Blue");
builder.Services.AddKeyedTransient<IThing, GreenThing>("Green");
builder.Services.AddScoped<IRandomService>(_ => new RandomService());
builder.Services.AddScoped<IServiceWithDbContextReference, ServiceWithDbContextReference>();

builder.Services.AddMarten(opts =>
{
Expand Down Expand Up @@ -140,6 +142,8 @@ public static async Task<int> Main(string[] args)
// Need this.
builder.Host.UseWolverine(opts =>
{
opts.ServiceLocationPolicy = JasperFx.CodeGeneration.Model.ServiceLocationPolicy.AllowedButWarn;

opts.Durability.MessageStorageSchemaName = "wolverine";

// I'm speeding this up a lot for faster tests
Expand Down Expand Up @@ -377,6 +381,8 @@ public static async Task<int> Main(string[] args)
opts.AddParameterHandlingStrategy<NowParameterStrategy>();

#endregion

opts.ServiceProviderSource = ServiceProviderSource.FromHttpContextRequestServices;
});

#region sample_mapwolverinehttptransportendpoints
Expand Down
Loading