0

I'm working on the Sitecore upgrade from 8.2 to 10.4. Here we have custom link provider and we have changed the code based the new methods but we are getting the error.

Server Error in '/' Application.
Unexpected provider type: Project.Common.Providers.SwitchingLinkProvider. Expected: Sitecore.Links.LinkProvider
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Unexpected provider type: Project.Common.Providers.SwitchingLinkProvider. Expected: Sitecore.Links.LinkProvider

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: Unexpected provider type: Project.Common.Providers.SwitchingLinkProvider. Expected: Sitecore.Links.LinkProvider]
   Sitecore.Configuration.DefaultFactory.GetProviders(List`1 nodes) +932
   Sitecore.Configuration.DefaultFactory.GetProviders(String rootPath, TProvider& defaultProvider) +323
   Sitecore.Configuration.ProviderHelper`2.ReadProviders() +75
   Sitecore.Configuration.ProviderHelper`2.get_Provider() +116
   Sitecore.Links.DefaultLinkManager.ParseRequestUrl(HttpRequest request) +42
   Sitecore.Web.RequestUrl.Parse(HttpRequestBase request) +123
   Sitecore.Pipelines.HttpRequest.HttpRequestArgs.Initialize() +31
   Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +118
   Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists) +236
   Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain) +22
   Sitecore.Web.RequestEventsHandler.OnBeginRequest(HttpContextBase context) +172
   Sitecore.Nexus.Web.HttpModule.HttpApplication_BeginRequest(Object sender, EventArgs e) +121
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +218
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +194
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +100

patch config file

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <linkmanager>
      <patch:attribute name="defaultProvider" value="switcher" />
      <providers>
        <add name="switcher" fallback="sitecore" type="Project.Common.Providers.SwitchingLinkProvider, project.Common" resolve="true" />
      </providers>
    </linkmanager>
  </sitecore>
</configuration>

Class file

public class SwitchingLinkProvider : BaseLinkManager
 {
     private readonly ProviderHelper<LinkProvider, LinkProviderCollection> _providerHelper;
     private const string FALLBACK_ATTRIBUTE_KEY = "fallback";
     private const string LINKPROVIDER_ATTRIBUTE_KEY = "linkProvider";

     private string Fallback { get; set; }

     public SwitchingLinkProvider(ProviderHelper<LinkProvider, LinkProviderCollection> providerHelper)
     {
         this._providerHelper = providerHelper;
     }

     protected virtual LinkProvider Provider
     {
         get
         {
             var siteLinkProvider = (Sitecore.Context.Site != null)
                                     ? Sitecore.Context.Site.Properties[LINKPROVIDER_ATTRIBUTE_KEY] : string.Empty;
             if (!string.IsNullOrEmpty(siteLinkProvider))
             {
                 return this._providerHelper.Provider;
             }

             return this._providerHelper.Providers[siteLinkProvider] ?? this._providerHelper.Provider;
         }
     }

     public override bool AddAspxExtension => this.Provider.AddAspxExtension;
     public override bool AlwaysIncludeServerUrl => this.Provider.AlwaysIncludeServerUrl;
     public override LanguageEmbedding LanguageEmbedding => this.Provider.LanguageEmbedding;
     public override LanguageLocation LanguageLocation => this.Provider.LanguageLocation;
     public override bool LowercaseUrls => this.Provider.LowercaseUrls;
     public override bool ShortenUrls => this.Provider.ShortenUrls;
     public override bool UseDisplayName => this.Provider.UseDisplayName;

     public override bool AlwaysExcludeVirtualFolder => throw new System.NotImplementedException();

     public override string ExpandDynamicLinks(string text)
     {
         Assert.ArgumentNotNull(text, nameof(text));
         return this.ExpandDynamicLinks(text, false);
     }

     public override string ExpandDynamicLinks(string text, bool resolveSites)
     {
         Assert.ArgumentNotNull(text, nameof(text));
         return Assert.ResultNotNull<string>(this.Provider.ExpandDynamicLinks(text, resolveSites));
     }

     //public override ItemUrlBuilderOptions GetDefaultUrlOptions()
     //{
     //    return Assert.ResultNotNull<ItemUrlBuilderOptions>(this.Provider.GetDefaultUrlOptions());
     //}

     public override string GetDynamicUrl(Item item)
     {
         return this.GetDynamicUrl(item, LinkUrlOptions.Empty);
     }

     public override string GetDynamicUrl(Item item, LinkUrlOptions options)
     {
         return this.Provider.GetDynamicUrl(item, options);
     }

     public override string GetItemUrl(Item item)
     {
         return this.Provider.GetItemUrl(item, this.GetDefaultUrlBuilderOptions());
     }

     public override string GetItemUrl(Item item, ItemUrlBuilderOptions options)
     {
         return this.Provider.GetItemUrl(item, options);
     }

     public override bool IsDynamicLink(string linkText)
     {
         return this.Provider.IsDynamicLink(linkText);
     }

     public override DynamicLink ParseDynamicLink(string linkText)
     {
         return this.Provider.ParseDynamicLink(linkText);
     }

     public override SiteInfo ResolveTargetSite(Item item)
     {
         Assert.ArgumentNotNull(item, nameof(item));
         return this.Provider.ResolveTargetSite(item);
     }

     public override SiteContext GetPreviewSiteContext(Item item)
     {
         Assert.ArgumentNotNull(item, nameof(item));
         return this.Provider.GetPreviewSiteContext(item);
     }

     public override ItemUrlBuilderOptions GetDefaultUrlBuilderOptions()
     {
         throw new System.NotImplementedException();
     }      

     public override RequestUrl ParseRequestUrl(HttpRequest request)
     {
         throw new System.NotImplementedException();
     }
 }
2
  • It seems the provider name is different in your patch file and provider class file, please check it again Commented Nov 25 at 11:30
  • config is updated. Commented Nov 25 at 11:33

2 Answers 2

0

You will need to extend your custom link provider with Sitecore.Links.LinkProvider provider class since you are adding this in the link provider collection.

using Sitecore.Links;
public class SwitchingLinkProvider: LinkProvider
{
}

Note: In the Sitecore latest version 10.4, item url is build using the links/itemUrlBuilder' configuration, therefore you can remove obsolete methods from the implementation such as ResolveTargetSite.

You can read more here on the provider collection.

Hope this helps!!! Let me know in case you face any issues.

1
  • 1
    Wish I had thought of that... Commented Nov 25 at 14:17
0

It looks like in the latest version of Sitecore, what ever it is that is consuming your custom class is expecting either a LinkProvider or something derived from it.

I copied your custom class into a solution and it doesnt complile against the latest sitecore binaries (lots of abstract members no implemented). Whereas, if I switch the base class to Sitecore.Links.LinkProvider (and removed unrequired overrides) it compiles.

Therefore my suggestion is to change your base class:

public class SwitchingLinkProvider : LinkProvider
{
    private readonly ProviderHelper<LinkProvider, LinkProviderCollection> _providerHelper;
    private const string FALLBACK_ATTRIBUTE_KEY = "fallback";

Then remove any obsolete i.e ResolveTargetSite(), any that cant be overwritten anymore i.e. GetPreviewSiteContext() and change any methods with updated return types i.e. GetDefaultUrlBuilderOptions().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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