简体   繁体   中英

How to share a View Component in a dll-referenced Razor Class Library?

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.

您必须使用视图组件在项目中注册IMyRepository服务:

services.AddScoped<IMyRespository, MyRepository>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM