依赖项注入服务返回的类型或命名空间不存在命名空间错误

时间:2019-06-24 17:17:31

标签: c# dependency-injection sitecore

依赖注入将为我们正在创建的两个服务返回null。错误CS0234的错误:名称空间“ CCO.Feature.Global.Areas.CCO”中不存在类型或命名空间名称“ Feature”(您是否缺少程序集引用?)。是什么原因造成的?

我们正在从Sitecore 8.2网站转换为Sitecore 9.1网站。这要求使用Sitecore和Microsoft的依赖项注入框架使用服务配置文件。我们根据Sitecore支持说明创建了配置文件。

namespace CCO.Feature.Global.Areas.CCO.Controllers
{
    public class MetadataController : CcoController
    {
        [AggregateService]

        public interface IDependencies
        {
            IMetadataService MetadataService { get; set; }
            ISitecoreConfigurationManager SitecoreConfigurationManager { get; set; }
        }

        private readonly MetadataController.IDependencies _dependencies;

        public MetadataController(IDependencies dependencies) : base(dependencies?.SitecoreConfigurationManager)
        {
            _dependencies = dependencies;
        }

        public virtual ActionResult HtmlPageTitle()
        {
            var mvcContext = new MvcContext();
            var dataSource = mvcContext.GetContextItem<IGlassBase>();
            var title = _dependencies.MetadataService.GetHtmlPageTitle(dataSource);

            return View(new MvcHtmlString(title));
        }

namespace CCO.Feature.Global.Services
{
    public class GlobalConfigurator : IServicesConfigurator
    {
        public void Configure(IServiceCollection serviceCollection)
        {
            serviceCollection.AddScoped(typeof(MetadataController.IDependencies), typeof(MetadataControllerDependencies));
            serviceCollection.AddTransient<MetadataController>(); 
        }
    }
}

namespace CCO.Feature.Global.Services
{
    public class MetadataControllerDependencies : MetadataController.IDependencies
    {
        public IMetadataService MetadataService { get; set; }
        public ISitecoreConfigurationManager SitecoreConfigurationManager { get; set; }
    }
}

namespace CCO.Feature.Global.Services
{
    public interface IMetadataService
    {
        string GetHtmlPageTitle(IGlassBase pageItem);
        IPageMetadata GetPageMetadata(IGlassBase pageItem);
        string GetCustomHeadHtml(IGlassBase pageItem);
        string GetOpenGraphImage(IGlassBase pageItem);
    }
}

我们希望该服务返回页面标题,但会收到错误CS0234:名称空间“ CCO.Feature.Global.Areas.CCO”中不存在类型或命名空间名称“ Feature”(您是否缺少程序集引用?),然后在尝试返回视图时抛出空引用异常。

添加了堆栈跟踪6/25/2019

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 38:             var mvcContext = new MvcContext();
Line 39:            var dataSource = mvcContext.GetContextItem<IGlassBase>();
Line 40:             var title = _dependencies.MetadataService.GetHtmlPageTitle(dataSource);
Line 41: 
Line 42:             return View(new MvcHtmlString(title));

Source File: D:\Source\cco_website\src\Feature\Global\code\Areas\CCO\Controllers\MetadataController.cs    Line: 40 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   CCO.Feature.Global.Areas.CCO.Controllers.MetadataController.HtmlPageTitle() in D:\Source\cco_website\src\Feature\Global\code\Areas\CCO\Controllers\MetadataController.cs:40
   lambda_method(Closure , ControllerBase , Object[] ) +92
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +34
   System.Web.Mvc.<>c__DisplayClass24_0.<InvokeActionMethodWithFilters>b__0() +79
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +454
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +454
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +524

0 个答案:

没有答案