Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Owain Williams 479 posts 1410 karma points MVP 6x c-trib
    Dec 23, 2021 @ 13:42
    Owain Williams
    0

    Migrating from Umbraco 8 to 9 and hit problems

    Hey everyone, Thought I'd see if anyone has any thoughts on what is causing this. I just don't understand what the error is telling me.

    InvalidOperationException: Unable to resolve service for type 'owaincodes.Interfaces.IBlogSearchService' while attempting to activate 'OwainCodes.Core.HomepageBlogViewComponent'.
    

    I've put all my code on to a Gist in the hope it makes more sense.

    https://gist.github.com/OwainWilliams/abd72f6f6f58db9f0ef484ea810162fc

    I've been following an example from https://github.com/Adolfi/UmbracoNineDemoSite to try and get Examine setup and working on U9.

    O.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Dec 23, 2021 @ 13:49
    Nik
    1

    You've not registered your service against the interface. I'm not sure what the right code is, but I'm pretty sure it's similar to V8 where on the left you have the interface and on the right the concrete type.

  • louisjrdev 11 posts 84 karma points
    Dec 23, 2021 @ 14:15
    louisjrdev
    0

    Try changing this line:

    services.AddTransient<BlogSearchService>();
    

    To:

    services.AddTransient<IBlogSearchService, BlogSearchService>();
    

    The error is the Dependency Injection, saying it cant find any service that has been registered for IBlogSearchService in the DI container. Which in this case is because you have registered just the class, without specifying that you want to register the class to be returned for any requests for IBlogSearchService too. Hence why you need to add the interface to the type paramater.

    DI can be thought of as, when asked for IBlogSearchService give me an instance of BlogSearchService.

    If you do not specify two types in the type argument, as is in your code, it will say only return BlogSearchService when asked for BlogSearchService and not the interface.

    As in your view component you could theoretically have:

     public class HomepageBlogViewComponent : ViewComponent
    
    
        {
               private BlogSearchService blogSearchService;
    
               public HomepageBlogViewComponent(BlogSearchService blogSearchService)
           {
               this.blogSearchService = blogSearchService ?? throw new ArgumentException(nameof(blogSearchService));
           }
           public IViewComponentResult Invoke(PaginationDetails model)
           {
               var returnModel = blogSearchService.GetPagedBlogFeed(model);
    
               return View();
               }
    
           }
    

    Though that would then depend on the concrete type and not the interface.

  • Owain Williams 479 posts 1410 karma points MVP 6x c-trib
    Dec 23, 2021 @ 14:23
    Owain Williams
    0

    I had services.AddTransient<IBlogSearchService, BlogSearchService>(); originally but then got other errors.

    The error I get when I change it to the above is:

    error CS0311: The type 'owaincodes.Services.BlogSearchService' cannot be used as type parameter 'TImplementation' in the generic type or method 'ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. There is no implicit reference conversion from 'owaincodes.Services.BlogSearchService' to 'owaincodes.Interfaces.IBlogSearchService'.
    
  • louisjrdev 11 posts 84 karma points
    Dec 23, 2021 @ 14:24
    louisjrdev
    0

    Can you show us BlogSearchService.cs?

    That seems to suggest that it cannot convert BlogSearchService to IBlogSearchService, which points to the idea thatBlogSearchService might not being inheriting IBlogSearchService

  • Owain Williams 479 posts 1410 karma points MVP 6x c-trib
    Dec 23, 2021 @ 14:27
    Owain Williams
    0

    I've updated the Gist with the file

  • louisjrdev 11 posts 84 karma points
    Dec 23, 2021 @ 14:28
    louisjrdev
    1

    You need your BlogSearchService to inherit IBlogSearchService.

    So change: public class BlogSearchService to public class BlogSearchService : IBlogSearchService

  • Corné Hoskam 80 posts 587 karma points MVP 2x c-trib
    Dec 23, 2021 @ 14:28
    Corné Hoskam
    1

    Hi Owain,

    Did you make sure to actually Implement the IBlogSearchService in your BlogSearchService class? Just making sure, due to the "There is no implicit reference conversion from 'owaincodes.Services.BlogSearchService' to 'owaincodes.Interfaces.IBlogSearchService'." part đŸ˜„

    Kind regards,

    Corné

  • Owain Williams 479 posts 1410 karma points MVP 6x c-trib
    Dec 23, 2021 @ 14:31
    Owain Williams
    0

    Thanks all! That has got me past that error. Now on to the next one but I think it's due to null references so should be able to sort that out.

Please Sign in or register to post replies

Write your reply to:

Draft