[英]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. 我用一些(Razor)页面,界面,存储库和模型构建了一个RCL,并且我想使用DLL参考与其他项目共享该RCL。 That works fine (using this ) and now I want to use the View Components inside the RCL, but it gives me the error:
正常工作(使用this ),现在我想在RCL内使用View组件,但这给了我错误:
InvalidOperationException: Unable to resolve service for type 'Shared.ModelInterfaces.IMyRepository' while attempting to activate 'Shared.Components.ItemViewComponent'.
InvalidOperationException:尝试激活“ Shared.Components.ItemViewComponent”时,无法解析“ Shared.ModelInterfaces.IMyRepository”类型的服务。
Diving deeper in the error, I found this: 在错误中深入研究,我发现了这一点:
method may only be called on a type for which type.is generic parameter is true
该方法只能在type.is通用参数为true的类型上调用
And it looks like this is causing the main error. 看起来这是造成主要错误的原因。
My ViewComponent has no Generic Type: 我的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
}
}
}
How can I fix this? 我怎样才能解决这个问题? I need the IMyRepository...
我需要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. 我知道RCL通常是由项目或作为NuGet包引用的,但是这些方法对我来说有一些缺点,这就是为什么我通过DLL引用我的RCL。
您必须使用视图组件在项目中注册IMyRepository
服务:
services.AddScoped<IMyRespository, MyRepository>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.