Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug using injectable properties from ExecuteSectionAsync #73

Merged
merged 1 commit into from
Dec 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{
if (name == "lorem-header")
{
<p class="text-info">This page renders lorem ipsum content from a service that's injected vie the <code>@@inject</code> directive.</p>
<p class="text-info">This page renders lorem ipsum content from a service (@LoremService.GetType().Name) that's injected via the <code>@@inject</code> directive.</p>
}

return Task.CompletedTask;
Expand Down
6 changes: 5 additions & 1 deletion src/RazorSlices/RazorLayoutSlice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Html;
using System.Diagnostics;
using Microsoft.AspNetCore.Html;

namespace RazorSlices;

Expand Down Expand Up @@ -37,7 +38,10 @@ protected ValueTask<HtmlString> RenderSectionAsync(string sectionName)

if (ContentSlice is not null)
{
Debug.WriteLine($"Rendering section '{sectionName}' on child slice of type '{ContentSlice.GetType().Name}' from layout slice of type '{GetType().Name}'");

var renderTask = ContentSlice.ExecuteSectionAsync(sectionName);

if (!renderTask.HandleSynchronousCompletion())
{
return AwaitRenderTask(renderTask);
Expand Down
6 changes: 5 additions & 1 deletion src/RazorSlices/RazorLayoutSliceOfTModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Html;
using System.Diagnostics;
using Microsoft.AspNetCore.Html;

namespace RazorSlices;

Expand Down Expand Up @@ -41,7 +42,10 @@ protected ValueTask<HtmlString> RenderSectionAsync(string sectionName)

if (ContentSlice is not null)
{
Debug.WriteLine($"Rendering section '{sectionName}' on child slice of type '{ContentSlice.GetType().Name}' from layout slice of type '{GetType().Name}'");

var renderTask = ContentSlice.ExecuteSectionAsync(sectionName);

if (!renderTask.HandleSynchronousCompletion())
{
return AwaitRenderTask(renderTask);
Expand Down
17 changes: 13 additions & 4 deletions src/RazorSlices/RazorSlice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract partial class RazorSlice : IDisposable
private HtmlEncoder _htmlEncoder = HtmlEncoder.Default;
private PipeWriter? _pipeWriter;
private TextWriter? _textWriter;
private bool _initialized;
private bool _disposed;

/// <summary>
Expand Down Expand Up @@ -72,10 +73,7 @@ public IServiceProvider? ServiceProvider

internal Task ExecuteAsyncImpl()
{
if (Initialize is not null)
{
Initialize(this, _serviceProvider, HttpContext);
}
EnsureInitialized();

return ExecuteAsync();
}
Expand Down Expand Up @@ -248,6 +246,7 @@ private ValueTask RenderViaLayout<TWriter>(Func<RazorSlice, TWriter, HtmlEncoder

if (layoutSlice is IRazorLayoutSlice razorLayoutSlice and RazorSlice)
{
EnsureInitialized();
razorLayoutSlice.ContentSlice = this;
CopySliceState(this, (RazorSlice)razorLayoutSlice);

Expand All @@ -271,6 +270,16 @@ internal static async ValueTask<HtmlString> AwaitRenderTask(Task renderTask)
return HtmlString.Empty;
}

private void EnsureInitialized()
{
if (!_initialized && Initialize is not null)
{
Debug.WriteLine($"Initializing slice of type '{GetType().Name}'. {nameof(_serviceProvider)} is currently {(_serviceProvider is null ? "null" : "not null")}.");
Initialize(this, _serviceProvider, HttpContext);
_initialized = true;
}
}

private ValueTask<FlushResult> AutoFlush()
{
Debug.Assert(_pipeWriter is null || _pipeWriter.CanGetUnflushedBytes, "PipeWriter must support unflushed bytes to auto-flush.");
Expand Down
Loading