0

I have created an ItemNotFoundResolver and injected it after ItemResolver pipeline as follows :

public class ItemNotFoundResolver : HttpRequestProcessor {
private readonly ISitecoreEntityService _entityService;
public ItemNotFoundResolver(ISitecoreEntityService entityService)
{
   _entityService = entityService;
}
//code logic here to call a method from the SitecoreEntityService

The Patch file is as follows :

<httpRequestBegin>
<processor patch:after="*[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" type="TestProject.Feature.ErrorHandling.Pipelines.HttpRequest.ItemNotFoundResolver, TestProject.Feature.ErrorHandling" resolve="true"/>

In Project TestProject.Foundation.ORM Have created an entity service and registered the same

public class SitecoreEntityService : ISitecoreEntityService
{
    protected readonly ISitecoreService _service;
    protected readonly IMvcContext _mvcContext;
    protected readonly IRequestContext _requestContext;
    public SitecoreEntityService(ISitecoreService service, IMvcContext mvcContext, IRequestContext requestContext)
    {
        Condition.Requires<ISitecoreService>(service).IsNotNull("ISitecoreService could not be null");
        Condition.Requires<IMvcContext>(mvcContext).IsNotNull("IMvcContext could not be null");
        Condition.Requires<IRequestContext>(requestContext).IsNotNull("IRequestContext could not be null");

        _service = service;
        _mvcContext = mvcContext;
        _requestContext = requestContext;
    }

In RegisterContainer.cs

public void Configure(IServiceCollection serviceCollection)
    {
        serviceCollection.AddTransient<ISitecoreService>(provider =>
            Sitecore.Context.Database.Name.ToLower() == "core" ? new SitecoreService("master") : new SitecoreService(Sitecore.Context.Database));
        serviceCollection.AddScoped<IRequestContext, RequestContext>();
        serviceCollection.AddScoped<IMvcContext, MvcContext>();
        serviceCollection.AddScoped<IGlassHtml, GlassHtml>();
    }

While I am trying to install a package through UpdateInstallationWizard, I am getting the following error in logs, while the last step (installation) is getting executed. and then it fails :

3052 20:30:51 ERROR Application error. Exception: Sitecore.Exceptions.ConfigurationException Message: Could not create object from service provider . Config XML: < processor type="TestProject.Feature.ErrorHandling.Pipelines.HttpRequest.ItemNotFoundResolver, TestProject.Feature.ErrorHandling" resolve="true" patch:source="TestProject.Feature.ErrorHandling.config" xmlns:patch="http://www.sitecore.net/xmlconfig/" /> Source: Sitecore.Kernel at Sitecore.Configuration.DefaultFactory.CreateFromServiceProvider(XmlNode configNode, Boolean assert) at Sitecore.Configuration.DefaultFactory.CreateObject(XmlNode configNode, String[] parameters, Boolean assert, IFactoryHelper helper) at Sitecore.Configuration.DefaultFactory.CreateObject(XmlNode configNode, Boolean assert) at Sitecore.Pipelines.CorePipelineFactory.GetObjectFromType(XmlNode processorNode) at Sitecore.Pipelines.CorePipelineFactory.GetProcessorObject(XmlNode processorNode) at Sitecore.Pipelines.CoreProcessor.GetMethod(Object[] parameters) at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists) at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain) at Sitecore.Web.RequestEventsHandler.OnPostAuthenticateRequest(HttpContextBase context) at Sitecore.Nexus.Web.HttpModule.(Object , EventArgs ) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

Exception: System.NullReferenceException Message: Object reference not set to an instance of an object. Source: TestProject.Foundation.ORM at TestProject.Foundation.ORM.DI.RegisterContainer.<>c.b__0_0(IServiceProvider provider) in C:\Projects\TestProject\src\Foundation\ORM\code\DI\RegisterContainer.cs:line 16 at lambda_method(Closure , ServiceProvider ) at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.Internal.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Sitecore.Configuration.DefaultFactory.CreateFromServiceProvider(XmlNode configNode, Boolean assert)

Anyone have idea on why this error is shown. Am I missing something. Please help me solve this issue.

1

Sitecore.Context.Database is null at the time when the ISitecoreSevice type is being injected. A simple null-check should probably solve this.

public void Configure(IServiceCollection serviceCollection)
{
    serviceCollection.AddTransient<ISitecoreService>(provider =>
    {
        var databaseName = Sitecore.Context.Database?.Name.ToLower();
        if (databaseName == null || databaseName == "core")
            return new SitecoreService("master");
        return new SitecoreService(Sitecore.Context.Database);
    });
    serviceCollection.AddScoped<IRequestContext, RequestContext>();
    serviceCollection.AddScoped<IMvcContext, MvcContext>();
    serviceCollection.AddScoped<IGlassHtml, GlassHtml>();
}
0

Did you register the configurator?

Follow the "Service registration" section at https://doc.sitecore.net/sitecore_experience_platform/developing/developing_with_sitecore/dependency_injection

enter image description here

  • Yes I have registered it <services> <configurator type="TestProject.Foundation.ORM.DI.RegisterContainer, TestProject.Foundation.ORM" /> </services> – Sreekrishnan Sep 19 '18 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.