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.
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.
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.
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'.
That seems to suggest that it cannot convert BlogSearchService to IBlogSearchService, which points to the idea thatBlogSearchService might not being inheriting IBlogSearchService
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 đŸ˜„
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.
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.
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.
Try changing this line:
To:
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 forIBlogSearchService
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 ofBlogSearchService
.If you do not specify two types in the type argument, as is in your code, it will say only return
BlogSearchService
when asked forBlogSearchService
and not the interface.As in your view component you could theoretically have:
Though that would then depend on the concrete type and not the interface.
I had
services.AddTransient<IBlogSearchService, BlogSearchService>();
originally but then got other errors.The error I get when I change it to the above is:
Can you show us BlogSearchService.cs?
That seems to suggest that it cannot convert
BlogSearchService
toIBlogSearchService
, which points to the idea thatBlogSearchService
might not being inheritingIBlogSearchService
I've updated the Gist with the file
You need your BlogSearchService to inherit IBlogSearchService.
So change:
public class BlogSearchService
topublic class BlogSearchService : IBlogSearchService
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é
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.
is working on a reply...