Skip to content

Commit

Permalink
[.NET 6] Backport WinForms & WPF CSS Hot Reload Fixes (#9787) Fixes #…
Browse files Browse the repository at this point in the history
…7479

* AutoCloseOnReadCompleteStream

(cherry picked from commit 8dd1316)

* Ensure old response content is closed out for hot reload

(cherry picked from commit ebdfdf4)

* PR Feedback

(cherry picked from commit 122426c)
  • Loading branch information
TanayParikh authored Aug 31, 2022
1 parent f097f92 commit a8e4d78
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions Microsoft.Maui.sln
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MauiBlazorWebView.DeviceTes
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SharedSource", "SharedSource", "{4F2926C8-43AB-4328-A735-D9EAD699F81D}"
ProjectSection(SolutionItems) = preProject
src\BlazorWebView\src\SharedSource\AutoCloseOnReadCompleteStream.cs = src\BlazorWebView\src\SharedSource\AutoCloseOnReadCompleteStream.cs
src\BlazorWebView\src\SharedSource\QueryStringHelper.cs = src\BlazorWebView\src\SharedSource\QueryStringHelper.cs
src\BlazorWebView\src\SharedSource\UrlLoadingEventArgs.cs = src\BlazorWebView\src\SharedSource\UrlLoadingEventArgs.cs
src\BlazorWebView\src\SharedSource\UrlLoadingStrategy.cs = src\BlazorWebView\src\SharedSource\UrlLoadingStrategy.cs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#if WEBVIEW2_WINFORMS || WEBVIEW2_WPF

using System.IO;

namespace Microsoft.AspNetCore.Components.WebView.WebView2
{
internal class AutoCloseOnReadCompleteStream : Stream
{
private readonly Stream _baseStream;

public AutoCloseOnReadCompleteStream(Stream baseStream)
{
_baseStream = baseStream;
}

public override bool CanRead => _baseStream.CanRead;

public override bool CanSeek => _baseStream.CanSeek;

public override bool CanWrite => _baseStream.CanWrite;

public override long Length => _baseStream.Length;

public override long Position { get => _baseStream.Position; set => _baseStream.Position = value; }

public override void Flush() => _baseStream.Flush();

public override int Read(byte[] buffer, int offset, int count)
{
var bytesRead = _baseStream.Read(buffer, offset, count);

// Stream.Read only returns 0 when it has reached the end of stream
// and no further bytes are expected. Otherwise it blocks until
// one or more (and at most count) bytes can be read.
if (bytesRead == 0)
{
_baseStream.Close();
}

return bytesRead;
}

public override long Seek(long offset, SeekOrigin origin) => _baseStream.Seek(offset, origin);

public override void SetLength(long value) => _baseStream.SetLength(value);

public override void Write(byte[] buffer, int offset, int count) => _baseStream.Write(buffer, offset, count);
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static bool TryReplaceResponseContent(string contentRootRelativePath, str
if (_updatedContent.TryGetValue((assemblyName, relativePath), out var values))
{
responseStatusCode = 200;
responseContent.Close();
responseContent = new MemoryStream(values.Content);
if (!string.IsNullOrEmpty(values.ContentType))
{
Expand Down
5 changes: 4 additions & 1 deletion src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,10 @@ protected virtual Task HandleWebResourceRequest(CoreWebView2WebResourceRequested
StaticContentHotReloadManager.TryReplaceResponseContent(_contentRootRelativeToAppRoot, requestUri, ref statusCode, ref content, headers);

var headerString = GetHeaderString(headers);
eventArgs.Response = _coreWebView2Environment!.CreateWebResourceResponse(content, statusCode, statusMessage, headerString);

var autoCloseStream = new AutoCloseOnReadCompleteStream(content);

eventArgs.Response = _coreWebView2Environment!.CreateWebResourceResponse(autoCloseStream, statusCode, statusMessage, headerString);
}
#elif WEBVIEW2_MAUI
// No-op here because all the work is done in the derived WinUIWebViewManager
Expand Down

0 comments on commit a8e4d78

Please sign in to comment.