1

ASP.NET Core 2.2.0

I built a RCL with some (Razor) pages, interfaces, repositories and models and I want to share that RCL using a DLL reference with my other projects. That works fine (using this) and now I want to use the View Components inside the RCL, but it gives me the error:

InvalidOperationException: Unable to resolve service for type 'Shared.ModelInterfaces.IMyRepository' while attempting to activate 'Shared.Components.ItemViewComponent'.

Diving deeper in the error, I found this:

method may only be called on a type for which type.is generic parameter is true

And it looks like this is causing the main error.

My ViewComponent has no Generic Type:

namespace Shared.Components
{
    public class ItemViewComponent : ViewComponent
    {
        private readonly IMyRepository _myRepository;


        public ItemViewComponent(IMyRepository myRepository)
        {
            _myRepository = myRepository;
        }

        public IViewComponentResult Invoke(string ViewType, string Category = "", string Organization = "", string ItemID = "")
        {
            // some code / some calls to my _myRepository / some returns
        }
    }
}

How can I fix this? I need the IMyRepository...

Side note

I know that RCLs usually are referenced by project or as a NuGet Package, but these methods have some disadvantages for me, that's why I reference my RCL by DLL.

CribAd
  • 452
  • 6
  • 19
  • I think maybe the error is something in your implementation of `IMyRepository` – Tim May 06 '19 at 12:00
  • No, it has to do with the 'isgeneric' type parameter. I tested it with multiple Vie Components and they all don't work, but when using the IMyRopsitory in a Razor Page it works fine. – CribAd May 06 '19 at 12:16

1 Answers1

0

You have to register your IMyRepository service in the project using the view component:

services.AddScoped<IMyRespository, MyRepository>();
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Yes I did that. As written: the RCL with the repository in it works fine on (Razor) Pages. In my Startup,cs I call `services.AddTransient();` and it works fine. – CribAd May 06 '19 at 14:14
  • I don't understand. Are you saying that you were *already* doing this? Frankly, that cannot have been the case, because that exception is explicitly a result of failing to register the service you're attempting to inject. Nothing else will raise that exception. – Chris Pratt May 06 '19 at 16:20
  • Yes I'm doing this and the error is still there. It looks like it has to do with my construction maybe; the viewcomponents are in the DLL-referenced RCL. When I use project reference it also works, but with DLL reference it will not. – CribAd May 07 '19 at 06:07