Browse Source

Merge pull request #26372 from abpframework/maliming/rc-static-asset-compression

Fix Blazor startup and prerendering failures on .NET 11
pull/26382/head
İsmail ÇAĞDAŞ 2 weeks ago
committed by GitHub
parent
commit
f75bca2db4
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 5
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs
  2. 5
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AspNetCoreUnitOfWorkTransactionBehaviourProvider.cs
  3. 6
      framework/src/Volo.Abp.AspNetCore/buildTransitive/Volo.Abp.AspNetCore.targets
  4. 90
      framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/Uow/AspNetCoreUnitOfWorkTransactionBehaviourProvider_Tests.cs

5
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs

@ -2,6 +2,7 @@
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -30,7 +31,9 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe
return;
}
if (httpContext.WebSockets.IsWebSocketRequest)
// HttpContext.WebSockets writes to the feature collection on every read when the request has no
// websocket feature, which breaks other threads reading the same HttpContext (e.g. Blazor prerendering).
if (httpContext.Features.Get<IHttpWebSocketFeature>()?.IsWebSocketRequest == true)
{
return;
}

5
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AspNetCoreUnitOfWorkTransactionBehaviourProvider.cs

@ -1,5 +1,6 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http;
@ -20,7 +21,9 @@ public class AspNetCoreUnitOfWorkTransactionBehaviourProvider : IUnitOfWorkTrans
return null;
}
if (httpContext.WebSockets.IsWebSocketRequest)
// HttpContext.WebSockets writes to the feature collection on every read when the request has no
// websocket feature, which breaks other threads reading the same HttpContext (e.g. Blazor prerendering).
if (httpContext.Features.Get<IHttpWebSocketFeature>()?.IsWebSocketRequest == true)
{
return null;
}

6
framework/src/Volo.Abp.AspNetCore/buildTransitive/Volo.Abp.AspNetCore.targets

@ -5,4 +5,10 @@
<PropertyGroup Condition="$(NETCoreSdkVersion.StartsWith('11.0.100-rc.')) and '$(Configuration)' == 'Debug' and '$(UsingMicrosoftNETSdkWeb)' == 'true' and '$(OutputType)' == 'Library' and '$(EnableHotReloadInRuntimeConfigDevFile)' == ''">
<EnableHotReloadInRuntimeConfigDevFile>false</EnableHotReloadInRuntimeConfigDevFile>
</PropertyGroup>
<!-- SDK 11.0.100 RC builds give the compressed copies of net11.0 package assets duplicate labels, so a Blazor host fails
on startup when two packages have a static asset with the same path. Remove this once the SDK fixes it. -->
<PropertyGroup Condition="$(NETCoreSdkVersion.StartsWith('11.0.100-rc.')) and '$(UsingMicrosoftNETSdkWeb)' == 'true' and '$(CompressionEnabled)' == ''">
<CompressionEnabled>false</CompressionEnabled>
</PropertyGroup>
</Project>

90
framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/Uow/AspNetCoreUnitOfWorkTransactionBehaviourProvider_Tests.cs

@ -1,4 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Options;
using Shouldly;
using Xunit;
@ -12,6 +19,11 @@ public class AspNetCoreUnitOfWorkTransactionBehaviourProvider_Tests
var httpContext = new DefaultHttpContext();
httpContext.Request.Method = method;
return CreateProvider(httpContext);
}
private static AspNetCoreUnitOfWorkTransactionBehaviourProvider CreateProvider(HttpContext httpContext)
{
return new AspNetCoreUnitOfWorkTransactionBehaviourProvider(
new HttpContextAccessor { HttpContext = httpContext },
Microsoft.Extensions.Options.Options.Create(new AspNetCoreUnitOfWorkTransactionBehaviourProviderOptions()));
@ -29,4 +41,82 @@ public class AspNetCoreUnitOfWorkTransactionBehaviourProvider_Tests
{
CreateProvider(method).IsTransactional.ShouldBe(expected);
}
[Fact]
public void IsTransactional_Should_Not_Change_The_Feature_Collection()
{
var features = new KestrelLikeFeatureCollection();
features.Set<IHttpRequestFeature>(new HttpRequestFeature { Method = "GET", Path = "/" });
features.Set<IHttpResponseFeature>(new HttpResponseFeature());
features.Set<IHttpResponseBodyFeature>(new StreamResponseBodyFeature(Stream.Null));
var httpContext = new DefaultHttpContext(features);
var provider = CreateProvider(httpContext);
var revision = httpContext.Features.Revision;
provider.IsTransactional.ShouldBe(false);
provider.IsTransactional.ShouldBe(false);
httpContext.Features.Revision.ShouldBe(revision);
}
[Fact]
public void IsTransactional_Should_Return_Null_For_WebSocket_Requests()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Method = "GET";
httpContext.Features.Set<IHttpWebSocketFeature>(new TestWebSocketFeature());
CreateProvider(httpContext).IsTransactional.ShouldBeNull();
}
// Kestrel's feature collection increments the revision on every Set, even when the value is null.
private sealed class KestrelLikeFeatureCollection : IFeatureCollection
{
private readonly FeatureCollection _inner = new();
public int Revision { get; private set; }
public bool IsReadOnly => false;
public object? this[Type key]
{
get => _inner[key];
set
{
Revision++;
_inner[key] = value;
}
}
public TFeature? Get<TFeature>()
{
return _inner.Get<TFeature>();
}
public void Set<TFeature>(TFeature? instance)
{
Revision++;
_inner.Set(instance);
}
public IEnumerator<KeyValuePair<Type, object>> GetEnumerator()
{
return _inner.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
private sealed class TestWebSocketFeature : IHttpWebSocketFeature
{
public bool IsWebSocketRequest => true;
public Task<WebSocket> AcceptAsync(WebSocketAcceptContext context)
{
throw new System.NotSupportedException();
}
}
}

Loading…
Cancel
Save