2

My goal is to set up a custom rendering. But I'm running into this error after publishing. I'm not sure where to look for the problem as I'm quite new.

Is this a controller error or am I registering something wrong?

Could not instantiate event handler. Type: Sitecore.XA.Foundation.RenderingVariants.EventHandlers.RenderingVariantCacheClearer. Method: OnItemCopied (method: Sitecore.Events.Event.BuildConfigSubscribersFromConfiguration()).

Here is my code.

Model

using Sitecore.XA.Foundation.RenderingVariants.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SXAArticle.Models
{
  public class ArticleModel : VariantListsRenderingModel {
  }

}

Controller

using Sitecore.XA.Foundation.Mvc.Controllers;
using SXAArticle.Repositories;
using Sitecore.XA.Foundation.RenderingVariants.Repositories;


namespace SXAArticle.Controllers
{
public class ArticleController : StandardController
{

    protected IArticleRepository _articleRespository
    { get; set; }

    protected IVariantsRepository _variantsRespository
    { get; set; }

    public ArticleController(IArticleRepository articleRepository, 
     IVariantsRepository variantsRepository)
    {
        _variantsRespository = variantsRepository;
        _articleRespository = articleRepository;
    }


    protected object GetVariantsModel()
    {
        return _variantsRespository.GetModel();
    }
}
}

Repository

using Sitecore.XA.Foundation.Mvc.Repositories.Base;
using Sitecore.XA.Foundation.RenderingVariants.Repositories;
using SXAArticle.Models;

namespace SXAArticle.Repositories
{
   public class ArticleRepository : VariantsRepository, IArticleRepository
   {
       public override IRenderingModelBase GetModel()
       {
            ArticleModel model = new ArticleModel();
            FillBaseProperties(model);
            return model;
       }
   }
}

IRepository

using Sitecore.XA.Foundation.RenderingVariants.Repositories;

namespace SXAArticle.Repositories
{
   public interface IArticleRepository : IVariantsRepository
   {

   }
}

Dependency Registration

using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
using SXAArticle.Controllers;
using SXAArticle.Repositories;

namespace SXAArticle.RegisterDependencies
{
    public class ArticleDependency : IServicesConfigurator
    {
        public void Configure(IServiceCollection serviceCollection)
    {
        serviceCollection.AddTransient<IArticleRepository, ArticleRepository>();
        serviceCollection.AddTransient<ArticleController>();
    }
    }
}

Dependency Injection

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <sitecore>
        <services>
          <register serviceType="SXAArticle.Repositories.IArticleRepository, SXAArticle"
                implementationType="SXAArticle.Repositories.ArticleRepository, SXAArticle"
                lifetime="Singleton"/>
        </services>
      </sitecore>
    </configuration>



<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <services>
      <configurator type="SXAArticle.RegisterDependencies.ArticleDependency, SXAArticle" />
    </services>
  </sitecore>
</configuration>

My View

@using Sitecore.XA.Foundation.SitecoreExtensions.Extensions
@using Sitecore.XA.Foundation.RenderingVariants.Extensions
@using Sitecore.XA.Foundation.SitecoreExtensions.Extensions
@using Sitecore.XA.Foundation.SitecoreExtensions.Extensions
@using Sitecore.XA.Foundation.Variants.Abstractions.Fields

@model SXAArticle.Models.ArticleModel

@if (Model.DataSourceItem != null || Html.Sxa().IsEdit)
{
    <div class="component article @Model.CssClasses.Aggregate()" )>
        <div class="component-content">
            @if (Model.DataSourceItem == null)
            {
                @Model.MessageIsEmpty
            }
            else
            {
                foreach (BaseVariantField variantField in Model.VariantFields)
                {
                    @Html.RenderingVariants().RenderVariant(variantField, Model.Item, Model.RenderingWebEditingParams)
                }
            }
        </div>
    </div>
}

I know it's a huge post. But hopefully, it can help out someone else in the future too.

1

First of all, if you are using StandardController in order to provide a model to the view you should override GetModel() method instead of using your own GetVariantsModel().

protected override object GetModel()
    {
        return Repository.GetModel();
    }

This base GetModel() method is used in default base Index() action.

Secondly, you are mixing the dependency injection techniques:

enter image description here

Regarding the issue, you are having. Double-check if you don't have any issues in your custom config files which might broke existing configs after merge. I assume you might have as I already noticed that you are trying to register some dependencies twice.

One more thing to check - do not override SXA assemblies while publishing your code. Set Copy Local to False in the dependant SXA assembly properties.

Update: If you are using any custom library, you need to manually copy it to the bin directory of your website. Or set the following property on your dependency in Visual Studio:

enter image description here

  • I had a back up before i published, so i haven't merged anything. I went and got the back up. And did the changes as you described above. Now i'm getting a totally different error message. "Could not load file or assembly 'PdfSharp' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)" What does this mean? – Tobias Heide Oct 15 '19 at 11:23
  • the error means the "pdfsharp" dll file is not present in the bin folder – Abhay Dhar Oct 15 '19 at 12:11
  • Looks like you are using custom PdfSharp library which is not copied to the BIN. You need to copy all custom libraries you are using in your code. – Dawid Rutkowski Oct 15 '19 at 12:13
  • Thanks input guys. I've deleted pdfSharp, as im not using it. So now i'm on to the next error. Which is "No parameterless constructor defined for this object.". I've seen this error on another post. So i'll see what i can do about it. :-) – Tobias Heide Oct 16 '19 at 7:31

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.