Open Closed

No service for type 'Microsoft.Extensions.Localization.IStringLocalizer`1[MyProject.Localization.MyProjectResource]' has been registered. #4146


User avatar
1
ageiter created
  • ABP Framework version: v6.0.1
  • UI type: Blazor Server
  • DB provider: EF Core
  • Exception message and stack trace: No service for type 'Microsoft.Extensions.Localization.IStringLocalizer`1[MyProject.Localization.MyProjectResource]' has been registered.

This is actually the same question as @balessi75 has already asked here: #3862

I want to use my string resources in a DTO (MyProject.Application.Contracts).

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    var results = new List<ValidationResult>();
    var l = validationContext.GetRequiredService<IStringLocalizer<MyProjectResource>>();
    var errorMessage = l["Entity:DocumentType:IsRequired:ValidationText"].Value;
    
    ...
    
    return results;
}

Unfortunately, I have not figured out how to register the IStringLocalizer correctly. I have read the mentioned documentation and searched for it, but somehow it doesn't work for me. I always get the following error message:

Unhandled exception rendering component: No service for type 'Microsoft.Extensions.Localization.IStringLocalizer`1[MyProject.Localization.MyProjectResource]' has been registered.
System.InvalidOperationException: No service for type 'Microsoft.Extensions.Localization.IStringLocalizer`1[MyProject.Localization.MyProjectResource]' has been registered.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at MyProject.Shared.DocumentDependentBase.Validate(ValidationContext validationContext) in 

Can you tell me how to register it in my module?


10 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share a project to reproduce the problem? liming.ma@volosoft.com

  • User Avatar
    0
    ageiter created

    I wanted to make an example project, but there it works. I haven't figured out where the difference is and why it doesn't work in my other project...

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    I also have no idea.

  • User Avatar
    0
    ageiter created

    I have now been able to recreate the problem in a test project. I have emailed you the OneDrive download link for the test project.

    In order for you to reproduce this, you need to create and save a new test element in my project.

    I use the ObjectGraphDataAnnotationsValidator to validate a more complex object (doesn't make sense in the example, of course, but I had to include it since it's probably part of the problem).

    Tests.razor (sorry, I had to change the HTML code so I could paste it here):

    < EditForm id="CreateTestForm" EditContext="NewTestDataContext">
                
        < ObjectGraphDataAnnotationsValidator />
    
        < ModalHeader>
            < ModalTitle>@L["NewTest"]</ModalTitle>
            < CloseButton Clicked="CloseCreateTestModalAsync" />
        < /ModalHeader>
        < ModalBody>
    
            < Validation>
                < Field>
                    < FieldLabel>@L["DocumentName"]</FieldLabel>
                    < TextEdit @bind-Text="@NewTest.Document.DocumentName">
                        < Feedback>
                            < ValidationError />
                        < /Feedback>
                    < /TextEdit>
                < /Field>
                < ValidationMessage For="() => NewTest.Document.DocumentValidation"></ValidationMessage>
            < /Validation>
    
        < /ModalBody>
        < ModalFooter>
            < Button Color="Color.Secondary" Clicked="CloseCreateTestModalAsync">
                @L["Cancel"]
            < /Button>
            < SubmitButton Form="CreateTestForm" Clicked="CreateTestAsync" />
        < /ModalFooter>
    < /EditForm>
    

    When validating DocumentDto.cs the error happens:

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var results = new List<ValidationResult>();
    
        // This is not working when using EditContext !!!
        var l = validationContext.GetRequiredService<IStringLocalizer<Abp4146Resource>>(); 
        var errorMessage = l["Test:ValidationMessage"].Value;
    
        // Do some validation
    
        results.Add(new ValidationResult(errorMessage, new[] { nameof(DocumentValidation) }));
        
        return results;
    }
    

    Exception: System.InvalidOperationException: 'No service for type 'Microsoft.Extensions.Localization.IStringLocalizer`1[Abp4146.Localization.Abp4146Resource]' has been registered.'

    What I found out: If I don't use the EditContext for the EditForm, but the model, then it works. But in my concrete project I need the EditContext...

    This is working: &lt;EditForm id=&quot;CreateTestForm&quot; Model=&quot;@NewTest&quot;&gt; This is NOT working: &lt;EditForm id=&quot;CreateTestForm&quot; EditContext=&quot;NewTestDataContext&quot;&gt;

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    For blazor

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var results = new List<ValidationResult>();
    
        // This is not working when using EditContext !!!
        var l = validationContext.GetService<IStringLocalizer<Abp4146Resource>>();
        if (l != null)
        {
            var errorMessage = l["Test:ValidationMessage"].Value;
    
            // Do some validation
    
            results.Add(new ValidationResult(errorMessage, new[] { nameof(DocumentValidation) }));
        }
    
        return results;
    }
    
  • User Avatar
    0
    ageiter created

    What does that mean exactly? It is not possible with Blazor? If yes, why? Or you have found a bug that you are going to fix?

    Because for me your "solution" means that I can't output multilingual validation messages.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    This line doesn't set the ServiceProvider of ValidationContext

    if (NewTestDataContext.Validate() == false)
    {
        return;
    }
    
  • User Avatar
    0
    ageiter created

    hi

    This line doesn't set the ServiceProvider of ValidationContext

    if (NewTestDataContext.Validate() == false) 
    { 
        return; 
    } 
    

    I know that, but it would be interesting to know how I would have to do it... setting the ServiceProvider.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can't. : ( This is designed by blazor.

  • User Avatar
    0
    ageiter created

    Ok, I have now applied a workaround to solve the problem.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on July 17, 2025, 06:22