Skip to content
Draft
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
17 changes: 14 additions & 3 deletions src/Components/Shared/src/ResourceCollectionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,19 @@ private async Task<ResourceAssetCollection> LoadResourceCollection()
return ResourceAssetCollection.Empty;
}

var module = await _jsRuntime.InvokeAsync<IJSObjectReference>("import", _url);
var result = await module.InvokeAsync<ResourceAsset[]>("get");
return result == null ? ResourceAssetCollection.Empty : new ResourceAssetCollection(result);
try
{
var module = await _jsRuntime.InvokeAsync<IJSObjectReference>("import", _url);
var result = await module.InvokeAsync<ResourceAsset[]>("get");
return result == null ? ResourceAssetCollection.Empty : new ResourceAssetCollection(result);
}
catch (Exception ex)
{
throw new InvalidOperationException(
$"Failed to load the Blazor resource collection from '{_url}'. " +
"This is likely caused by a mismatch in the file integrity check, which can happen when files are modified after they are published. " +
"Ensure that all published files are deployed correctly and that none have been modified after publish.",
ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task StopAsync(CancellationToken token)
// Throw an aggregate exception if there were any exceptions
if (exceptions is not null)
{
var aggregateException = new AggregateException(exceptions);
var aggregateException = new AggregateException("One or more hosted services failed to stop.", exceptions);
try
{
Log.ErrorStoppingHostedServices(_logger, aggregateException);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Abstractions;

namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting;

public class HostedServiceExecutorTest
{
[Fact]
public async Task StopAsync_WithFailingServices_ThrowsAggregateExceptionWithDescriptiveMessage()
{
// Arrange
var services = new[]
{
new FaultyHostedService("Service 1 error"),
new FaultyHostedService("Service 2 error"),
};
var executor = new HostedServiceExecutor(services, NullLogger<HostedServiceExecutor>.Instance);

// Act
var ex = await Assert.ThrowsAsync<AggregateException>(() => executor.StopAsync(CancellationToken.None));

// Assert
Assert.StartsWith("One or more hosted services failed to stop.", ex.Message);
Assert.Equal(2, ex.InnerExceptions.Count);
}

[Fact]
public async Task StopAsync_WithNoFailingServices_DoesNotThrow()
{
// Arrange
var services = new[]
{
new SuccessfulHostedService(),
new SuccessfulHostedService(),
};
var executor = new HostedServiceExecutor(services, NullLogger<HostedServiceExecutor>.Instance);

// Act (should not throw)
await executor.StopAsync(CancellationToken.None);
}

private class FaultyHostedService(string errorMessage) : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;

public Task StopAsync(CancellationToken cancellationToken) =>
Task.FromException(new InvalidOperationException(errorMessage));
}

private class SuccessfulHostedService : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.JSInterop;
using Moq;

namespace Microsoft.AspNetCore.Components;

public class ResourceCollectionProviderTest
{
[Fact]
public async Task GetResourceCollection_WhenUrlIsNull_ReturnsEmpty()
{
// Arrange
var jsRuntime = Mock.Of<IJSRuntime>();
var provider = new ResourceCollectionProvider(jsRuntime);

// Act
var result = await provider.GetResourceCollection();

// Assert
Assert.Same(ResourceAssetCollection.Empty, result);
}

[Fact]
public async Task GetResourceCollection_WhenImportFails_ThrowsDescriptiveError()
{
// Arrange
var url = "/_framework/resource-collection.abc123.js";
var jsRuntime = new Mock<IJSRuntime>();
jsRuntime
.Setup(r => r.InvokeAsync<IJSObjectReference>("import", It.IsAny<object[]>()))
.ThrowsAsync(new JSException("Failed to fetch dynamically imported module"));

var provider = new ResourceCollectionProvider(jsRuntime.Object);
provider.ResourceCollectionUrl = url;

// Act
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
() => provider.GetResourceCollection());

// Assert
Assert.Contains($"Failed to load the Blazor resource collection from '{url}'", ex.Message);
Assert.Contains("integrity", ex.Message, StringComparison.OrdinalIgnoreCase);
Assert.NotNull(ex.InnerException);
Assert.IsType<JSException>(ex.InnerException);
}

[Fact]
public async Task GetResourceCollection_WhenGetFails_ThrowsDescriptiveError()
{
// Arrange
var url = "/_framework/resource-collection.abc123.js";
var jsRuntime = new Mock<IJSRuntime>();
var moduleReference = new Mock<IJSObjectReference>();

jsRuntime
.Setup(r => r.InvokeAsync<IJSObjectReference>("import", It.IsAny<object[]>()))
.ReturnsAsync(moduleReference.Object);

moduleReference
.Setup(m => m.InvokeAsync<ResourceAsset[]>("get", It.IsAny<object[]>()))
.ThrowsAsync(new JSException("An error occurred"));

var provider = new ResourceCollectionProvider(jsRuntime.Object);
provider.ResourceCollectionUrl = url;

// Act
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
() => provider.GetResourceCollection());

// Assert
Assert.Contains($"Failed to load the Blazor resource collection from '{url}'", ex.Message);
Assert.Contains("integrity", ex.Message, StringComparison.OrdinalIgnoreCase);
Assert.NotNull(ex.InnerException);
Assert.IsType<JSException>(ex.InnerException);
}
}
Loading