Pages

11 June, 2021

Sitecore - Post condition failed exception

After upgrading from Sitecore 9.0.1 to Sitecore 10.0.1, there was an error reported in CD environment. "Post condition failed"

This is due to the reference of Core Database in CD server. From Sitecore 9.2, Sitecore has removed the reference of core database in Content Delivery and introduced Security database connection string which can store the aspnet membership tables. 

In my case, I had a reference of Core database in a custom configuration which modified the prefetch cacheSize. Once I removed it, site loaded fine. 
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <databases>
      <database id="core">
        <dataProviders hint="list:AddDataProvider">
          <dataProvider param1="$(id)">
            <prefetch>
              <cacheSize>200MB</cacheSize>
            </prefetch>
          </dataProvider>
        </dataProviders>
      </database>      
  </sitecore>
</configuration>

Exception:
Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 6/11/2021 10:32:01 AM 
Event time (UTC): 6/11/2021 3:32:01 PM 
Event ID: 8bb3c123c9f64b1dbf6201fddf35994b 
Event sequence: 2 
Event occurrence: 1 
Event detail code: 0 
 
Application information: 
    Application domain: /LM/W3SVC/2/ROOT-3-132678991184217924 
    Trust level: Full 
    Application Virtual Path: / 
    Application Path: C:\inetpub\wwwroot\cd\ 
    Machine name: CD 
 
Process information: 
    Process ID: 11156 
    Process name: w3wp.exe 
    Account name: IIS APPPOOL\cd 
 
Exception information: 
    Exception type: InvalidOperationException 
    Exception message: Post condition failed
   at Sitecore.Diagnostics.Assert.ResultNotNull[T](T result, String message)
   at Sitecore.Configuration.DefaultFactory.GetDatabase(String name)
   at Sitecore.Configuration.DefaultFactory.GetDatabases()
   at Sitecore.Data.Managers.DefaultItemManager.Initialize()
   at (Object , Object )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at (Object , Object )
   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.Nexus.Web.HttpModule.Application_Start()
   at Sitecore.Nexus.Web.HttpModule.Init(HttpApplication app)
   at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
   at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
   at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
   at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

 
 
Request information: 
    Request URL: https://cd:443/
    Request path: /
    User host address: 192.168.1.137 
    User:  
    Is authenticated: False 
    Authentication Type:  
    Thread account name: IIS APPPOOL\cd 
 
Thread information: 
    Thread ID: 31 
    Thread account name: IIS APPPOOL\cd 
    Is impersonating: False 
    Stack trace:    at Sitecore.Diagnostics.Assert.ResultNotNull[T](T result, String message)
   at Sitecore.Configuration.DefaultFactory.GetDatabase(String name)
   at Sitecore.Configuration.DefaultFactory.GetDatabases()
   at Sitecore.Data.Managers.DefaultItemManager.Initialize()
   at (Object , Object )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at (Object , Object )
   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.Nexus.Web.HttpModule.Application_Start()
   at Sitecore.Nexus.Web.HttpModule.Init(HttpApplication app)
   at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
   at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
   at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
   at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)
 

04 June, 2021

Sitecore RenderingContext.Current.Rendering.Item changed? - Sitecore 9 vs Sitecore 10

As part of  Sitecore 9 to Sitecore 10 upgrade, few components did not work after the upgrade and it was related to the rendering datasource.

Problem: I have a datasource on an ancestor Controller rendering. Child Controller rendering does not have a datasource. Child Controller rendering uses ancestor rendering datasource Item to render the data. After the Sitecore 10 upgrade, Child Controller rendering couldn't get the ancestor datasource item using RenderingContext.Current.Rendering.Item and instead it returned page content Item. 

There is a great blog on Sitecore MVC Rendering datasource topic by Matthew Dresser. He explains about RenderingContext.Current.Rendering.Item clearly. It helps to "get datasource of the current rendering, or the datasource of an ancestor rendering if specified, or the page item if no datasource set and no ancestor rendering with datasource. Rendering datasource takes priority". Datasource of an ancestor rendering will be returned if the setting Mvc.AllowDataSourceNesting is true. It is true by default.

After the upgrade, RenderingContext.Current.Rendering.Item does not return ancestor rendering datasource and instead it returns Page context item. 

Raised a Sitecore Support ticket whether it is an expected change or a bug. Will update once I hear from them.

As workaround, I wanted it to behave in the same fashion to reduce the impact. I added a wrapper method which gives priority to child rendering datasource if available, then ancestor rendering datasource if available and then page context item. 

References:

01 June, 2021

ReferenceError: URL is not defined

If you are using Node 8.11.2-x64, you may experience the below error when trying to fetch client packages like bootstrap or jquery from the source. 

1579 verbose stack ReferenceError: URL is not defined
1579 verbose stack     at regKeyFromURI (C:\Users\Nehem\AppData\Roaming\npm\node_modules\npm\node_modules\npm-registry-fetch\auth.js:7:18)
1579 verbose stack     at getAuth (C:\Users\Nehem\AppData\Roaming\npm\node_modules\npm\node_modules\npm-registry-fetch\auth.js:49:18)
1579 verbose stack     at regFetch (C:\Users\Nehem\AppData\Roaming\npm\node_modules\npm\node_modules\npm-registry-fetch\index.js:55:16)
1579 verbose stack     at RemoteFetcher.[pacote.Fetcher._tarballFromResolved] (C:\Users\Nehem\AppData\Roaming\npm\node_modules\npm\node_modules\pacote\lib\remote.js:40:5)
1579 verbose stack     at tryAgain (C:\Users\Nehem\AppData\Roaming\npm\node_modules\npm\node_modules\pacote\lib\fetcher.js:303:64)
1579 verbose stack     at C:\Users\Nehem\AppData\Roaming\npm\node_modules\npm\node_modules\promise-retry\index.js:29:24
1579 verbose stack     at <anonymous>

Permanent Fix: Upgrade the node to v9.8.0 which will resolve the issue. 

Workaround: In case if you can't or don't want to upgrade, you can add the var URL = require('url').URL; in the js file mentioned in the stacktrace (here: \node_modules\npm-registry-fetch\auth.js) and it should resolve the issue. Stackoverflow reference

28 May, 2021

Sitecore Upgrade - Codebase Upgrade Series - Remove unused references and nuget packages

As part of the Sitecore codebase upgrade series, in this blog, we are going to deal with unused references and packages. It is worth to cleanup the unused references and nuget packages to reduce the complexity, dependencies and effort to upgrade to be compatible with target Sitecore version. 

Remove Unused References:

Visual Studio 2019 has the inbuilt "Remove Unused References" option for .NET framework projects with .NET SDK project format and .NET core projects. If the project type is already in .NET SDK format, then right click on project or on the references and select Remove Unused References. Then select the list of references to remove and click Apply. As of Visual Studio 2019 v16.10.0, this option is turned off by default. To enable it, you can go to menu Tools > Options > Text Editor > C# > Advanced. Select the Remove Unused References command in Solution Explorer. 




Remove Unused Packages

To remove Unused packages, you may want to get ReSharper which can clean up the unused references and also remove the unused packages. If ReSharper is installed, you can refactor the entire solution or a particular project. Right Click on the Solution > Refactor > Remove Unused References. This opens up a window with the list of references and packages and you can select and remove it. This will remove the entry from Packages.config or from PackageReferences if it is used. 

There are other open source extensions in Visual Studio Gallery and few proprietary tools like CoreRush or Telerik® JustCode™ are available in the market which must be doing the same tasks. 



27 May, 2021

Sitecore Upgrade - Codebase Upgrade Series

One of the task in a Sitecore upgrade project is to upgrade the codebase to be compatible with the target Sitecore version. I have listed some of the steps which I learnt and it helped me to upgrade the solution quickly. 

  1. Remove unused references and nuget packages
  2. Framework upgrade - .NET
  3. Migrate to SDK Project Format (optional)
  4. Migrate from packages.config to PackageReference (optional)
  5. Replace Sitecore assemblies reference with nuget package
  6. Upgrade Sitecore nuget packages
  7. Upgrade .NET related nuget packages
  8. Upgrade 3rd party nuget packages
  9. Compile, replace obsolete methods and fix errors
Before upgrading the codebase, make sure to create a separate branch so that it does not affect the ongoing development tasks.