<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sitecore Xperiences &#187; WFFM</title>
	<atom:link href="https://blog.peplau.com.br/category/wffm/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.peplau.com.br</link>
	<description>The things I&#039;ve seen as a Sitecore Professional</description>
	<lastBuildDate>Sun, 09 Mar 2025 21:54:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>WFFM - Sitecore Context Item NULL inside a Save Action</title>
		<link>https://blog.peplau.com.br/wffm-sitecore-context-item-null-inside-a-save-action/</link>
		<comments>https://blog.peplau.com.br/wffm-sitecore-context-item-null-inside-a-save-action/#comments</comments>
		<pubDate>Sun, 06 Aug 2017 14:10:26 +0000</pubDate>
		<dc:creator><![CDATA[Rodrigo Peplau]]></dc:creator>
				<category><![CDATA[WFFM]]></category>

		<guid isPermaLink="false">http://blog.peplau.com.br/?p=588</guid>
		<description><![CDATA[<div class="lr_horizontal_share" data-share-url="https://blog.peplau.com.br/wffm-sitecore-context-item-null-inside-a-save-action/"></div>Business requirements There is a use case in one of our clients at Nish Tech, where a WFFM form, with a Submit E-mail Save Action, is supposed to be reused at several different parts of the website. It should have the same fields and look &#38; feel at every place we want to reuse it, [&#8230;]]]></description>
				<content:encoded><![CDATA[<h1>Business requirements</h1>
<p><span style="font-weight: 400;">There is a use case in one of our clients at Nish Tech, where a WFFM form, with a Submit E-mail Save Action, is supposed to be reused at several different parts of the website. It should have the same fields and look &amp; feel at every place we want to reuse it, the only difference would be that the e-mail would be submitted to different addresses depending on what page the form is located.</span></p>
<p><span style="font-weight: 400;">The pages where this form will be allowed are what we call “Communities”. Each of them has a couple of fields with e-mails (Eg: Sales E-mail, Human Resources E-mail) from which we wanted to have WFFM looking for, instead of using the one hardcoded at the WFFM itself.</span></p>
<p><b>One may ask: </b><span style="font-weight: 400;">why not duplicate the WFFM form and hardcode a different e-mail for each page we need? The answer is plenty clear: we will have more than 80 communities, and growing. That would mean more than 80 duplications of the WFFM form, which sound quite abusive. If we happen to need some kind of maintenance at these forms, this would be a nightmare.</span></p>
<blockquote><p><b>The resulting implementation will be subject of a future blog post, this one in particular will focus in a specific challenge: how to access the Sitecore.Context.Item inside a WFFM Save Action.</b></p></blockquote>
<h1><span style="font-weight: 400;">Technical challenge</span></h1>
<p><span style="font-weight: 400;">So the idea is that WFFM should look into a field at the item where it is located, grabbing the destiny e-mail from there instead of the hardcoded e-mail at the WFFM Form itself. To achieve this we will need to access the current context item (Sitecore.Context.Item), which surprisingly is not available inside a Save Action, as confirmed by Sitecore in a Helpdesk ticket we created.</span></p>
<p><span style="font-weight: 400;">To be fair, Sitecore.Context.Item is available in Preview Mode because Sitecore explicitly sets the current item ID at the querystring. However, in publish mode there are no such parameter at the querystring, which happens to let Sitecore.Context.Item with value NULL when you are inside a Save Action. This happens because the Item Resolver only runs AFTER the Save Action, thus Context.Item will only be filled after the Save Action is executed.</span></p>
<h1><span style="font-weight: 400;">Implementing a Solution</span></h1>
<p><span style="font-weight: 400;">So here is the solution we ended up arranging, with best possible reusing of same WFFM and not jeopardizing WFFM’s or Sitecore’s normal pipelines and flows.</span></p>
<h3><span style="font-weight: 400;">Step 1 &#8211; Create a new WFFM field for Sitecore.Context.Item</span></h3>
<p><span style="font-weight: 400;">This would be a hidden field at the form, responsible for carry on the Sitecore.Context.Item.ID, available at the time when the form is rendering, to the Save Action where that is not available. </span></p>
<ol>
<li style="font-weight: 400;"><span style="font-weight: 400;">At /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Simple Types start by duplicating the “Single-Line Text” field to serve as a basis for our new field;</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">Rename it to “Sitecore Context Item ID”;</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">Fill the field “MVC Type” with the assembly we are going to create to our new field:</span><span style="font-weight: 400;"><br />
</span><i><span style="font-weight: 400;">ClientName.Website.Core.WFFM.Fields.SitecoreContextItemId, ClientName.Website.Core</span></i></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">Create a view called “SitecoreContextItemId.cshtml” for this field under /Views/Form/EditorTemplates and add the following content. Notice the </span><i><span style="font-weight: 400;">&lt;span style=&#8221;display:none;&#8221;&gt;</span></i><span style="font-weight: 400;"> surrounding the field, which will make it hidden when the form renders</span></li>
</ol>
<pre><span style="font-weight: 400;">@using Sitecore.Forms.Mvc.Html
</span>@model Sitecore.Forms.Mvc.ViewModels.FieldViewModel
&lt;span style="display:none;"&gt;
@using (Html.BeginField())
{      
    @Html.BootstrapEditor("Value")    
}
&lt;/span&gt;
</pre>
<p>&nbsp;</p>
<h3><span style="font-weight: 400;">Step 2 &#8211; Make that field comes auto-populated with Context.Item</span></h3>
<p><span style="font-weight: 400;">This class, which is tied to the field at the bullet 3 of Step 1, implements the logic that makes the field renders with Sitecore.Context.Item.ID auto-populated. Notice that this is simply inheriting from SingleLineTextField, adding the ID to the field Value, and nothing else.</span></p>
<pre><span style="font-weight: 400;">using Sitecore.Forms.Mvc.ViewModels.Fields;
</span>namespace ClientName.Website.Core.WFFM.Fields
{
    public class SitecoreContextItemId : SingleLineTextField
    {
        public override void Initialize()
        {
            if (this.MaxLength != 0)
                return;
            this.MaxLength = 256;
            this.Value = Sitecore.Context.Item.ID.ToString();
        }
    }
}</pre>
<h3></h3>
<h3><span style="font-weight: 400;">Step 3 &#8211; Make Context.Item be automatically filled from that field</span></h3>
<p><span style="font-weight: 400;">We could very well now just go ahead and read from that field, couldn’t we? But to make things pretty, instead of that we are going to implement a pipeline processor to populate Sitecore.Context.Item.</span></p>
<ul>
<li style="font-weight: 400;"><span style="font-weight: 400;">Create an include file “Sitecore.WFFM.SetContextItem.config” with following content:</span></li>
</ul>
<pre><span style="font-weight: 400;">&lt;?xml version="1.0" encoding="utf-8"?&gt;
</span>&lt;configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"&gt;
  &lt;sitecore&gt;
    &lt;pipelines&gt;
      &lt;wffm.submit&gt;
        &lt;processor type="ClientName.Website.Core.WFFM.Pipelines.SetContextItem,ClientName.Website.Core" patch:after="processor[@type='Sitecore.Forms.Mvc.Pipelines.Submit.RegisterFormSubmitEvent,Sitecore.Forms.Mvc']"/&gt;
      &lt;/wffm.submit&gt;
    &lt;/pipelines&gt;
  &lt;/sitecore&gt;
&lt;/configuration&gt;</pre>
<ul>
<li style="font-weight: 400;"><span style="font-weight: 400;">Then let’s implement this class to make Sitecore.Context.Item be populated. </span></li>
</ul>
<pre><i><span style="font-weight: 400;">using System.Linq;
</span></i><i><span style="font-weight: 400;">using Sitecore.Data;
</span></i><i><span style="font-weight: 400;">using Sitecore.Diagnostics;
</span></i><i><span style="font-weight: 400;">using Sitecore.Forms.Mvc.Interfaces;
</span></i><i><span style="font-weight: 400;">using Sitecore.Forms.Mvc.Models;
</span></i><i><span style="font-weight: 400;">using Sitecore.Forms.Mvc.Pipelines;
</span></i><i><span style="font-weight: 400;">namespace ClientName.Website.Core.WFFM.Pipelines
</span></i><i><span style="font-weight: 400;">{
</span></i><i><span style="font-weight: 400;">    public class SetContextItem : FormProcessorBase&lt;IFormModel&gt;
</span></i><i><span style="font-weight: 400;">    {
</span></i><i><span style="font-weight: 400;">        public override void Process(FormProcessorArgs&lt;IFormModel&gt; args)
</span></i><i><span style="font-weight: 400;">        {
</span></i><i><span style="font-weight: 400;">            Assert.ArgumentNotNull((object)args, "args");
</span></i><i><span style="font-weight: 400;">            var model = args.Model as FormModel;
</span></i><i><span style="font-weight: 400;">            if (model == null || Sitecore.Context.Item!=null)
</span></i><i><span style="font-weight: 400;">                return;
</span></i><i><span style="font-weight: 400;">            var contextField = model.Item.Fields.FirstOrDefault(p =&gt; p.MVCClass.Contains(typeof(Fields.SitecoreContextItemId).FullName));
</span></i><i><span style="font-weight: 400;">            if (contextField == null)
</span></i><i><span style="font-weight: 400;">                return;
</span></i><i><span style="font-weight: 400;">            var contextFieldResult = model.Results.FirstOrDefault(p =&gt; p.FieldName == contextField.Name);
</span></i><i><span style="font-weight: 400;">            if (contextFieldResult == null)
</span></i><i><span style="font-weight: 400;">                return;
</span></i><i><span style="font-weight: 400;">            var contextItem = Sitecore.Context.Database.GetItem(new ID(contextFieldResult.Value.ToString()));
</span></i><i><span style="font-weight: 400;">            Sitecore.Context.Item = contextItem;
</span></i><i><span style="font-weight: 400;">        }
</span></i><i><span style="font-weight: 400;">    }
</span></i><i><span style="font-weight: 400;">}</span></i></pre>
<p><span style="font-weight: 400;">Pay special attention to the following lines:</span></p>
<pre><span style="font-weight: 400;">var contextField = model.Item.Fields.FirstOrDefault(p =&gt; p.MVCClass.Contains(typeof(Fields.SitecoreContextItemId).FullName));</span></pre>
<p>Where the assembly name we created at Step 2 is used to find our field and next:</p>
<pre><span style="font-weight: 400;">var contextFieldResult = model.Results.FirstOrDefault(p =&gt; p.FieldName == contextField.Name);</span></pre>
<p><strong><strong> </strong></strong>Where the ID value is taken to later populate Sitecore.Context.Item:</p>
<pre><span style="font-weight: 400;">var contextItem = Sitecore.Context.Database.GetItem(new ID(contextFieldResult.Value.ToString()));
</span>Sitecore.Context.Item = contextItem;</pre>
<p>&nbsp;</p>
<h3><span style="font-weight: 400;">Step 4 &#8211; Add the field to your WFFM Form</span></h3>
<p><span style="font-weight: 400;">Easy part, but if forgotten nothing will work. Make sure your new field is present at your WFFM Form:</span></p>
<p><a href="http://blog.peplau.com.br/wp-content/uploads/WFFM-Field-Sitecore-Context-Item.png"><img class="alignnone size-full wp-image-598" src="http://blog.peplau.com.br/wp-content/uploads/WFFM-Field-Sitecore-Context-Item.png" alt="WFFM Field Sitecore Context Item" width="929" height="94" /></a></p>
]]></content:encoded>
			<wfw:commentRss>https://blog.peplau.com.br/wffm-sitecore-context-item-null-inside-a-save-action/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Corey Peplau wrote this - WFFM conflict with Unity DI and a lesson on how Sitecore community is so amazing</title>
		<link>https://blog.peplau.com.br/corey-peplau-wrote-this-wffm-conflict-with-unity-di-and-a-lesson-on-how-sitecore-community-is-so-amazing/</link>
		<comments>https://blog.peplau.com.br/corey-peplau-wrote-this-wffm-conflict-with-unity-di-and-a-lesson-on-how-sitecore-community-is-so-amazing/#comments</comments>
		<pubDate>Mon, 24 Jul 2017 13:35:09 +0000</pubDate>
		<dc:creator><![CDATA[Rodrigo Peplau]]></dc:creator>
				<category><![CDATA[Bug fixing]]></category>
		<category><![CDATA[Sitecore Community]]></category>
		<category><![CDATA[WFFM]]></category>

		<guid isPermaLink="false">http://blog.peplau.com.br/?p=585</guid>
		<description><![CDATA[<div class="lr_horizontal_share" data-share-url="https://blog.peplau.com.br/corey-peplau-wrote-this-wffm-conflict-with-unity-di-and-a-lesson-on-how-sitecore-community-is-so-amazing/"></div>This blog was since the beginning, a space to share my own experiences with Sitecore development, always trying to bring that something new, not easily seen, or showing my interpretation of common problems. But this time I need to break my own rule and bring a story starred by another person. I’m talking about my [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-weight: 400;">This blog was since the beginning, a space to share my own experiences with Sitecore development, always trying to bring that something new, not easily seen, or showing my interpretation of common problems. But this time I need to break my own rule and bring a story starred by another person. I’m talking about my good friend </span><a href="https://twitter.com/sitecorey" target="_blank"><b>Corey Smith</b></a><span style="font-weight: 400;">, the most willful person I ever met. </span></p>
<h2><span style="font-weight: 400;">First a small parable </span></h2>
<p><span style="font-weight: 400;">An engineer was hired to fix a complex machine, which he did in less than an hour by simply twisting a single screw.</span></p>
<ul>
<li style="font-weight: 400;"><span style="font-weight: 400;">How much it cost? Asks the client.</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">10 thousand dollars, sir&#8230;</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">But that is a lot of money just to twist a screw! I need you to do an invoice with details of your job to justify this price.</span></li>
</ul>
<p><span style="font-weight: 400;">And the invoice listed:</span></p>
<ul>
<li style="font-weight: 400;"><span style="font-weight: 400;">Twist a screw: $ 5.00</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">Know what screw to twist: $ 9,995.00</span></li>
</ul>
<p><span style="font-weight: 400;">Corey is a step ahead of this story and managed to not only twist the right screw, but made that remotely, and for free.</span></p>
<h2><span style="font-weight: 400;">The WFFM issue</span></h2>
<p><span style="font-weight: 400;">Everything started when, at my first project as </span><a href="http://www.nishtechinc.com/Blog/2017/July/nish-tech-hires-3rd-SitecoreMVP"><span style="font-weight: 400;">new member of Nish Tech Inc</span></a><span style="font-weight: 400;">, I had to install WFFM 8.2 rev. 170413 (Update-3) up above an existent solution. Starting from there, any MVC WFFM form were dying with the following error:</span></p>
<blockquote><p><span style="font-weight: 400;">Error Rendering Controller: Sitecore.Forms.Mvc.Controllers.FormController, Sitecore.Forms.Mvc. Action: Index: Could not create controller: &#8216;Sitecore.Forms.Mvc.Controllers.FormController, Sitecore.Forms.Mvc&#8217;. The item being rendered is: &#8216;/sitecore/content/Website/Home/Community/Cincinnati&#8217;. The context item is: &#8216;/sitecore/content/Website/Home/Community/Cincinnati&#8217;. The current route url is: &#8216;{*pathInfo}&#8217;. This is the default Sitecore route which is set up in the &#8216;InitializeRoutes&#8217; processor of the &#8216;initialize&#8217; pipeline.</span></p>
<p><span style="font-weight: 400;">  at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Sitecore.Mvc.Controllers.ControllerRunner.GetController()</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Sitecore.Mvc.Controllers.ControllerRunner.Execute()</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Sitecore.Mvc.Presentation.ControllerRenderer.Render(TextWriter writer)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)</span></p>
<p><span style="font-weight: 400;">Inner Exception: Resolution of the dependency failed, type = &#8220;Sitecore.Forms.Mvc.Controllers.FormController&#8221;, name = &#8220;(none)&#8221;. Exception occurred while: while resolving. Exception is: InvalidOperationException &#8211; The current type, Sitecore.Forms.Mvc.Interfaces.IRepository`1[Sitecore.Forms.Mvc.Models.FormModel], is an interface and cannot be constructed. Are you missing a type mapping? &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; At the time of the exception, the container was: Resolving Sitecore.Forms.Mvc.Controllers.FormController,(none) Resolving parameter &#8220;repository&#8221; of constructor Sitecore.Forms.Mvc.Controllers.FormController(Sitecore.Forms.Mvc.Interfaces.IRepository`1[[Sitecore.Forms.Mvc.Models.FormModel, Sitecore.Forms.Mvc, Version=8.2.0.0, Culture=neutral, PublicKeyToken=null]] repository, Sitecore.Forms.Mvc.Interfaces.IAutoMapper`2[[Sitecore.Forms.Mvc.Interfaces.IFormModel, Sitecore.Forms.Mvc, Version=8.2.0.0, Culture=neutral, PublicKeyToken=null],[Sitecore.Forms.Mvc.ViewModels.FormViewModel, Sitecore.Forms.Mvc, Version=8.2.0.0, Culture=neutral, PublicKeyToken=null]] mapper, Sitecore.Forms.Mvc.Interfaces.IFormProcessor`1[[Sitecore.Forms.Mvc.Models.FormModel, Sitecore.Forms.Mvc, Version=8.2.0.0, Culture=neutral, PublicKeyToken=null]] processor, Sitecore.WFFM.Abstractions.Shared.IAnalyticsTracker analyticsTracker) Resolving Sitecore.Forms.Mvc.Interfaces.IRepository`1[Sitecore.Forms.Mvc.Models.FormModel],(none)</span></p>
<p><span style="font-weight: 400;">  at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateControllerInstance(RequestContext requestContext, String controllerName)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName)</span></p>
<p><span style="font-weight: 400;">Inner Exception: The current type, Sitecore.Forms.Mvc.Interfaces.IRepository`1[Sitecore.Forms.Mvc.Models.FormModel], is an interface and cannot be constructed. Are you missing a type mapping?</span></p>
<p><span style="font-weight: 400;">  at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at lambda_method(Closure , IBuilderContext )</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.&lt;&gt;c__DisplayClass1.b__0(IBuilderContext context)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at lambda_method(Closure , IBuilderContext )</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.&lt;&gt;c__DisplayClass1.b__0(IBuilderContext context)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)</span><span style="font-weight: 400;"><br />
</span><span style="font-weight: 400;">   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)</span></p></blockquote>
<h2><span style="font-weight: 400;">The greatness of Sitecore community</span></h2>
<p><span style="font-weight: 400;">After some googling I found <a href="http://blogs.perficient.com/microsoft/2016/05/sitecore-wffm-controller-not-found-after-upgrade-to-8-1/" target="_blank">this article</a></span><span style="font-weight: 400;"><a href="http://blogs.perficient.com/microsoft/2016/05/sitecore-wffm-controller-not-found-after-upgrade-to-8-1/" target="_blank"> from Corey Smith</a>, where he describes a similar issue after an upgrade, which he solved by moving his Dependency Injection declaration before the InitializeControllerFactory. I know that recent versions of WFFM are using Dependency Injection, so it sounded like a good clue to start my investigation.</span></p>
<p><span style="font-weight: 400;">But in my case we were using <a href="https://msdn.microsoft.com/en-us/library/dn223671(v=pandp.30).aspx" target="_blank">Unity Dependency Injection</a></span><span style="font-weight: 400;">, nothing registered at Sitecore Pipeline, so I thought &#8211; why not asking Corey himself for an idea? He is a great guy, as per what we’ve seen during his presentation at the Sitecore Usergroup Brazil (<a href="https://www.youtube.com/watch?v=MAwy2DwPPjo&amp;list=PLiIDLKfmzW5izK8HmPcFOHQq3YMl369-n&amp;index=1&amp;t=2874s" target="_blank">video can be seen here</a>).</span></p>
<p>What followed was a real lesson for me, breaking my paradigms of balance between personal and professional life, and showing to my amazed eyes how strong, powerful and great is the Sitecore community in general, and Corey Smith in particular.</p>
<h2><span style="font-weight: 400;">Our investigation</span></h2>
<p><span style="font-weight: 400;">It was 3rd July, I was stuck at the hotel in Cincinnati with my family and this issue to solve. Corey were safe at his home in Atlanta, at the convenient distance of a single click to interrupt the chat. We were texting like crazy thru Twitter private messages. Fortunately, we both had beers at our side, would you do without some?</span></p>
<p>We started focusing at the Unity Dependency Injection setup. Under my App_Start folder I had a class called UnityMvcActivator with following decorations above the namespace:</p>
<blockquote><p><b>[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(UnityWebActivator), &#8220;Start&#8221;)]</b></p>
<p><span style="font-weight: 400;">[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(UnityWebActivator), &#8220;Shutdown&#8221;)]</span></p>
<p><span style="font-weight: 400;">namespace MyClient.Website.Core.App_Start</span></p>
<p><span style="font-weight: 400;">{</span></p>
<p><span style="font-weight: 400;">    public static class UnityWebActivator</span></p>
<p><span style="font-weight: 400;">    {</span></p>
<p><span style="font-weight: 400;">        public static void Start()</span></p>
<p><span style="font-weight: 400;">        {</span></p></blockquote>
<p><span style="font-weight: 400;">Line in bold is specifically setting the start-up of Dependency Injection. That would be automatically invoking (at some point </span><b>in the Application_Start event</b><span style="font-weight: 400;">) the method Start(). The exact point, however, was a mystery for us, so we managed to increase a little of our control over this. Perhaps changing the order here would make any difference? </span></p>
<p><span style="font-weight: 400;">So we removed the line in bold, then registered the method Start as an Initialize Pipeline </span>processor<span style="font-weight: 400;">. For that, the Start signature had to change into a valid Process method, as seem below:</span></p>
<blockquote><p>public void Process(PipelineArgs args)</p></blockquote>
<p>That way we would be able to control the timing when the DI start-up would occur. Promising? I thought so! However unfortunately, no matter where the registration is made, before or after everything, still the same error was showing.</p>
<h2><span style="font-weight: 400;">Root cause </span></h2>
<p><span style="font-weight: 400;">After digging a bit more, Corey came up with the root cause, and a possible solution. Happens that the controller for WFFM forms (FormController), which is the affected class at the error message, has actually two constructors: one without any parameter</span></p>
<blockquote><p>public FormController()</p></blockquote>
<p>And a second one with more parameters</p>
<blockquote><p>public FormController(IRepository&lt;FormModel&gt; repository, IAutoMapper&lt;IFormModel, FormViewModel&gt; mapper, IFormProcessor&lt;FormModel&gt; processor, IAnalyticsTracker analyticsTracker)</p></blockquote>
<p>When Unity tries to instantiate this class it is automatically choosing the controller with most parameters. And that is making the whole thing blow up to the sky.</p>
<h2><span style="font-weight: 400;">Temporary (quick) solution</span></h2>
<p><span style="font-weight: 400;">The temporary solution we ended up doing was:</span></p>
<ol>
<li style="font-weight: 400;"><span style="font-weight: 400;">Roll back the pipeline modification and allow Unity to registry itself again</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">Add the following new line to UnityConfig, RegisterTypes method:</span><span style="font-weight: 400;"><br />
</span></li>
</ol>
<blockquote><p><span style="font-weight: 400;">public class UnityConfig</span></p>
<p><span style="font-weight: 400;">{</span></p>
<p><span style="font-weight: 400;">        public static void RegisterTypes(IUnityContainer container)</span></p>
<p><span style="font-weight: 400;">        {</span></p>
<p><b>            // Workaround to solve WFFM issues</b></p>
<p><b>            container.RegisterType&lt;FormController&gt;(new InjectionConstructor());</b></p></blockquote>
<p><span style="font-weight: 400;">What the line above does is to tell Unity to register, only at the FormController class, the constructor with no parameters. This, however, is an ad-hoc solution which brings a very important indagation: What else are we breaking in Sitecore? What other controllers and dependencies do we need to register? </span></p>
<p>“You will probably find out in production, I suppose” says Corey.</p>
<h2><span style="font-weight: 400;">Appropriate (hard) solution</span></h2>
<p><span style="font-weight: 400;">Best thing to do is to abandon Unity Dependency Injection at this project and have <a href="https://doc.sitecore.net/sitecore_experience_platform/developing/developing_with_sitecore/dependency_injectio" target="_blank">Sitecore’s built-in Dependency Injection</a> (<a href="https://github.com/aspnet/DependencyInjection">Microsoft.Extensions.DependencyInjection</a>) </span><span style="font-weight: 400;">to handle all injections at our system. We are not gaining anything using Unity at this project, but actually we are creating problems. </span></p>
<p><span style="font-weight: 400;">Sitecore’s OOTB Dependency Injection </span><span style="font-weight: 400;">is shipped from factory with all proper Dependency Injection configurations. That means that all Sitecore modules play very nice with it, so the whole issue we are having with Unity, for instance, simply wouldn’t even happen at first place.  </span></p>
<p>Along with that, in Sitecore 8.2, we can do Dependency Injection into pipeline processors, which has always been a requirement at Nish Tech, and is also totally awesome!</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.peplau.com.br/corey-peplau-wrote-this-wffm-conflict-with-unity-di-and-a-lesson-on-how-sitecore-community-is-so-amazing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
