Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'Volo.Abp.AspNetCore.Components.Web.AbpBlazorMessageLocalizerHelper`1...
Autofac.Core.Registration.ComponentNotRegisteredException
The requested service 'Volo.Abp.AspNetCore.Components.Web.AbpBlazorMessageLocalizerHelper`1[[DummyPlatform.Localization.DummyPlatformResource, DummyPlatform.Domain.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
See https://autofac.rtfd.io/help/service-not-registered for more info.
at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType)
at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Volo.Abp.AbpTestBaseWithServiceProvider.GetRequiredService[T]()
at DummyPlatform.Blazor.Tests.Items.ItemPage_IntegrationShould.Load_Items() in C:\Users\user\source\repos\DummyPlatform\test\DummyPlatform.Blazor.Tests\Items\ItemPage_IntegrationShould.cs:line 22
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Item.razor
@page "/items"
@using DummyPlatform.Items
@using DummyPlatform.Localization
@using Volo.Abp.AspNetCore.Components.Web
@inherits DummyPlatformComponentBase
@inject IItemAppService ItemAppService
@inject AbpBlazorMessageLocalizerHelper<DummyPlatformResource> LH
<Card>
<CardHeader>
<Row Class="justify-content-between">
<Column ColumnSize="ColumnSize.IsAuto">
<h2>@L["Items"]</h2>
</Column>
</Row>
</CardHeader>
<CardBody>
<DataGrid TItem="ItemDto"
Data="ItemList"
ReadData="OnDataGridReadAsync"
TotalItems="TotalCount"
ShowPager="true"
PageSize="PageSize">
<DataGridColumns>
<DataGridColumn TItem="ItemDto"
Field="@nameof(ItemDto.ItemNr)"
Caption="@L["ItemNr"]"></DataGridColumn>
<DataGridColumn TItem="ItemDto"
Field="@nameof(ItemDto.VariantCode)"
Caption="@L["VariantCode"]"></DataGridColumn>
<DataGridColumn TItem="ItemDto"
Field="@nameof(ItemDto.Description)"
Caption="@L["Description"]"></DataGridColumn>
</DataGridColumns>
</DataGrid>
</CardBody>
</Card>
<Modal @ref="ItemDetailModal">
<_ModalBackdrop></_ModalBackdrop>
<ModalContent IsCentered="true">
<Form>
<ModalHeader>
<ModalTitle>@OpenedItem.ItemNr</ModalTitle>
<CloseButton Clicked="CloseItemDetailModal"></CloseButton>
</ModalHeader>
<ModalBody>
<Field>
<FieldLabel>@L["ItemNr"]</FieldLabel>
<TextEdit @bind-Text="@OpenedItem.ItemNr"></TextEdit>
</Field>
<Field>
<FieldLabel>@L["VariantCode"]</FieldLabel>
<TextEdit @bind-Text="@OpenedItem.ItemNr"></TextEdit>
</Field>
<Field>
<FieldLabel>@L["Description"]</FieldLabel>
<TextEdit @bind-Text="@OpenedItem.Description"></TextEdit>
</Field>
</ModalBody>
<ModalFooter>
<Button Color="Color.Secondary"
Clicked="CloseItemDetailModal">
@L["Cancel"]
</Button>
</ModalFooter>
</Form>
</ModalContent>
</Modal>
Unit Test
using DummyPlatform.Items;
using DummyPlatform.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using NSubstitute;
using Volo.Abp.AspNetCore.Components.Web;
using Xunit;
namespace DummyPlatform.Blazor.Tests.Items;
public class ItemPage_IntegrationShould : DummyPlatformBlazorTestBase
{
private IItemAppService _itemAppService;
private AbpBlazorMessageLocalizerHelper<DummyPlatformResource> _lh;
[Fact]
public void Load_Items()
{
// Arrange
var ctx = CreateTestContext();
_itemAppService = Substitute.For<IItemAppService>();
_lh = GetRequiredService<AbpBlazorMessageLocalizerHelper<DummyPlatformResource>>();
ctx.Services.AddSingleton(_itemAppService);
ctx.Services.AddSingleton(_lh);
// Act
ctx.RenderComponent<DummyPlatform.Blazor.Pages.Items>();
// Assert
_itemAppService.Received(1).GetListAsync(Arg.Any<GetItemListDto>());
}
}
I kept the Testbase the same as in the blogpost. As you can see, I kept very close to the Tutorial from the ABP documentation, so someone else could encounter the same problem. Do I maybe have a missing dependency here?
When I call GuidGenerator.Create() in my DomainService, that I instantiated in a test, I get a NullReferenceException. I think I could solve this by implementing the IDomainService instead and then mock the IGuidGenerator interface. However, there really is no need to do that here aside from testing concerns.
Do you have any suggestions on how to workaround this issue?
(ItemManager.cs)
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;
namespace TestPlatform.Items;
public class ItemManager : DomainService
{
private readonly IItemRepository _itemRepository;
public ItemManager(IItemRepository itemRepository)
{
_itemRepository = itemRepository;
}
public async Task<Item> CreateAsync(
string itemNr,
int variantCode,
string description,
)
{
var existingItem = await _itemRepository.FindByItemNrAndVariantAsync(itemNr, variantCode);
if (existingItem != null)
{
throw new ItemAlreadyExistsException(itemNr, variantCode);
}
var newGuid = GuidGenerator.Create();
return new Item(
newGuid,
itemNr,
variantCode,
description,
);
}
}
(ItemManagerTest.cs)
using System.Threading.Tasks;
using TestPlatform.Items.Shared;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using Shouldly;
using Xunit;
namespace TestPlatform.Items;
public class ItemManager_CreateAsyncShould
{
private IItemRepository itemRepository = Substitute.For<IItemRepository>();
[Fact]
public async Task Return_Item_If_No_Item_With_Same_ItemNr_Exists()
{
itemRepository.FindByItemNrAndVariantAsync(
ValidItemData.ItemNr,
ValidItemData.VariantCode
).ReturnsNull();
var itemManager = new ItemManager(itemRepository);
var item = await itemManager.CreateAsync(
ValidItemData.ItemNr,
ValidItemData.VariantCode,
ValidItemData.Description,
);
item.ShouldBeOfType<Item>();
item.ShouldNotBeNull();
}
}
Edit: For some reason, markdown removes everything inside the <> arrows. However, the content is not important for the problem at hand anyway.