Altijd als eerste onze nieuwste blogs lezen? Laat je email adres achter en je ontvangt een bericht als wij een nieuw blog plaatsen.

When multiple websites are running within one Sitecore instance, it is important that they do not conflict with each other. This applies especially for Pipelines, Events, Save Processors, etc.

Onze technische blogs zijn in het Engels. Dit doen wij omdat wij menen dat technische kennis van Sitecore en Sitefinity grensoverschrijdend moet zijn. Wij leren veel van buitenlandse developers, wij delen onze kennis ook graag met hen. Mochten er in de code fouten zitten of punten ter verbetering, dan gaan we graag het gesprek aan.

When multiple websites are running within one Sitecore instance, it is important that they do not conflict with each other. This applies especially for Pipelines, Events, Save Processors, etc.

For example a pipeline written for a particular website called A can cause serious problems for site B. To find out what the problems are of the crashing website B, that will be a complicated task.  Especially when website A was built by Partner X and website B was built by Partner Y.

What you really would like is that you can make pipelines or events conditional, so that the pipeline or events only triggers when a certain condition is met.

Now there are a number of types within Sitecore pipelines and events. In this blog I will confine myself to the HttpRequestProcessor.

Now there are two types of conditional pipelines, events to consider. These include;

  • You build one from scratch (custom) and thus have complete control.
  • You want to use a third-party pipeline and here you do not have complete control.

For both types there must be a sort of checking mechanism that checks if the pipeline must be triggered or not. This mechanism will be used by both types and it is a good idea to extract them to a separate helper class (ConditionalSiteHelper).

This helper class must able to check on two settings. Sitename and Hostname . If the pipeline is triggered after the SiteResolver pipeline you can use the Sitename check. If not than the hostname check must be used. Also very useful is an option to switch on or off debug information if necessary.

The checking mechanism helper class is just straight C# code I don’t dive into that code.

Building your own custom conditional pipeline.


All custom HttpRequestProcessors must be derived from the abstract class ConditionalSiteHttpRequestProcessor. See the image on the left. This abstract class contains three properties and an abstract method called “ConditionalProcess” that must be implemented at the derived class.

The method ConditionalProcess will only be executed when the Filled in Condition (Type and Value) are met. Otherwise this method will not be executed.

The parameter “Type” can contain two values “sitenames” or “hostnames”.







Sitenames

When sitenames is filled in to the “Type” parameter then the method “ConditionalProcess” will only trigger when the current sitename  occurs in the “Value” parameter. The “Value” parameter can contains multiple sitenames. The sitenames must be separated by a the | character.

Hostnames  

When the “Type” parameter contains the “hostnames” then the “Value” parameter must contains all url’s that will trigger this pipeline. In hostnames it is possible to use a wildcard. When the parameter is filled with *.coworker.nl. The pipeline will trigger all the url’s that ends with coworker.nl. Also the parameter “Value” can contains more than 1 url. The url’s must be separated by a the | character.

So below is the code of the abstract class 

  
public abstract class ConditionalSiteHttpRequestProcessor : System.Object
    {
        public string Type { get; set; }
        public string Value { get; set; }
        public bool Debug { get; set; }
 
        public void Process(HttpRequestArgs args)
        {
            if (!ConditionalSiteHelper.Check(Type, Value, Debug)) return;
            Write2Log(" Triggerd.");
            ConditionalProcess(args);
        }
 
        public abstract void ConditionalProcess(HttpRequestArgs args);
 
 
        /// 
        /// Write a message to the Sitecore logfile.
        /// 
        /// The message.
        private void Write2Log(string message)
        {
            if (Debug)
            {
                Logging.Debug(message,this);
            }
        }
     
}

Now to get this working you have to make an include file for the custom pipeline see below an example;


sitenames
coworker
True


Now building a container for third-party pipelines that must have a conditional trigger:
Now you have a third party pipelines that only may executed for one website on a Sitecore installation. You can’t derive from  the ConditionalSiteContainerHttpRequestProcessor abstract class. We have to build some sort of container where we can define the pipelines that will be executed when de condition is met.

Is this possible in Sitecore ?????

YES

And it is not that difficult.

See the code below


 public class ConditionalSiteContainerHttpRequestProcessor : HttpRequestProcessor
    {
        public string Type { get; set; }
        public string Value { get; set; }
        public bool Debug { get; set; }
 
        private List Container { get; set; }
 
        /// 
        /// Initializes a new instance of the  class.
        /// 
        public ConditionalSiteContainerHttpRequestProcessor()
        {
            Container = new List();
        }
 
        public void AddProcessor(XmlNode node)
        {
            var coreProcessor = new CoreProcessor();
            coreProcessor.Initialize(node);
            Container.Add(coreProcessor);
        }
 
        /// 
        /// Processes the specified args.
        /// 
        /// The args.
        public override void Process(HttpRequestArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
          
            if (ConditionalSiteHelper.Check(Type, Value, Debug))
            {
                var argsArray = new object[] { args };
 
                foreach (var c in Container)
                {
                    if (!args.Aborted || c.RunIfAborted)
                    {
                        c.Invoke(argsArray);
                        PipelineCounters.ProcessorsExecuted.Increment();
                    }
                }
                PipelineCounters.PipelinesExecuted.Increment();
                if (args.Aborted)
                {
                    PipelineCounters.PipelinesAborted.Increment();
                } 
            }
 
        }       
 
    }


Add the container tag you see a attribute called hint. The content of that attribute is the name of the method that will be execute to Add the declared processor to the list. 

In the process method there is first the conditional check to see if de declared processor may be executed. If this is the case then we will looping through list of declared processors and execute them. 

That’s All !!! 

So when using this mechanism on a foundation level, and partners are building websites and using these conditional pipelines then you have one less thing to worry about as a maintenance team.

Meer achtergrond nodig? Neem contact met ons op

Deel deze pagina