繁体   English   中英

如何在dll引用的Razor类库中共享视图组件?

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

ASP.NET Core 2.2.0

我用一些(Razor)页面,界面,存储库和模型构建了一个RCL,并且我想使用DLL参考与其他项目共享该RCL。 正常工作(使用this ),现在我想在RCL内使用View组件,但这给了我错误:

InvalidOperationException:尝试激活“ Shared.Components.ItemViewComponent”时,无法解析“ Shared.ModelInterfaces.IMyRepository”类型的服务。

在错误中深入研究,我发现了这一点:

该方法只能在type.is通用参数为true的类型上调用

看起来这是造成主要错误的原因。

我的ViewComponent没有通用类型:

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
        }
    }
}

我怎样才能解决这个问题? 我需要IMyRepository ...

边注

我知道RCL通常是由项目或作为NuGet包引用的,但是这些方法对我来说有一些缺点,这就是为什么我通过DLL引用我的RCL。

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

services.AddScoped<IMyRespository, MyRepository>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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