Context not loaded error in custom GlassWebControl class

521 views
Skip to first unread message

Kyle Heon

unread,
Nov 27, 2013, 8:17:09 AM11/27/13
to glasssite...@googlegroups.com
I'm trying to create a GlassWebControl class similar to how the GlassPage and GlassUserControl classes are setup. I have things coded up but when the WebControl is instantiated either on page or through Sitecore presentation details I'm getting the following error and I'm not familiar enough with Glass to know what might be going on.


Context has not been loaded 

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: Glass.Sitecore.Mapper.MapperException: Context has not been loaded

Source Error:


Line 239:

Line 240:            //due to changes in the way that handlers are created we should no longer need to clone the instance context

Line 241:            return StaticContext; //.Clone() as InstanceContext;

Line 242:        }

Line 243:


Source File: c:\clientWork\Glass\Source\Glass.Sitecore.Mapper\Context.cs Line: 241

Stack Trace:


[MapperException: Context has not been loaded]

   Glass.Sitecore.Mapper.Context.GetContext() in c:\clientWork\Glass\Source\Glass.Sitecore.Mapper\Context.cs:241

   Glass.Sitecore.Mapper.SitecoreService..ctor(Database database) in c:\clientWork\Glass\Source\Glass.Sitecore.Mapper\SitecoreService.cs:56

   Glass.Sitecore.Mapper.SitecoreContext..ctor() in c:\clientWork\Glass\Source\Glass.Sitecore.Mapper\SitecoreContext.cs:27

   Glass.Sitecore.Mapper.Web.Ui.AbstractGlassWebControl.get_SitecoreContext() in c:\clientWork\Glass\Source\Glass.Sitecore.Mapper\Web\Ui\AbstractGlassWebControl.cs:19

   Glass.Sitecore.Mapper.Web.Ui.GlassWebControl`1.OnLoad(EventArgs e) in c:\clientWork\Glass\Source\Glass.Sitecore.Mapper\Web\Ui\GlassWebControl.cs:16

   System.Web.UI.Control.LoadRecursive() +71

   System.Web.UI.Control.LoadRecursive() +190

   System.Web.UI.Control.LoadRecursive() +190

   System.Web.UI.Control.LoadRecursive() +190

   System.Web.UI.Control.LoadRecursive() +190

   System.Web.UI.Control.LoadRecursive() +190

   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

This fails as soon as I try to create a SitecoreContext, accessing this property in my AbstractGlassWebControl class:

public ISitecoreContext SitecoreContext

        {

            get { return _sitecoreContext ?? (_sitecoreContext = new SitecoreContext()); }

            set { _sitecoreContext = value; }

        }

The above creation is called by the following OnLoad event setup code in my GlassWebControl class:

public abstract class GlassWebControl<T> : AbstractGlassWebControl where T : class

    {

        protected override void OnLoad(EventArgs e)

        {

            Model = SitecoreContext.CreateClass<T>(false, false, LayoutItem);

            base.OnLoad(e);

        }

 Any suggestions are greatly appreciated, thanks in advance!
Message has been deleted

Kyle Heon

unread,
Nov 27, 2013, 9:23:25 AM11/27/13
to glasssite...@googlegroups.com
What is weird is that if I don't use my custom GlassWebControl class and instead create a normal Sitecore.Web.UI.WebControl with the following setup code it works. If I replace my OnLoad code with this as well it fails with the same error details as listed in my original post.

public class TitleWebControl : Sitecore.Web.UI.WebControl

{

    protected override void DoRender(HtmlTextWriter output)

    {

        var context = new SitecoreContext();

        var model = context.GetCurrentItem<ISampleItem>();


        output.Write(model.Title);

    }

}

Michael Edwards

unread,
Nov 27, 2013, 9:27:32 AM11/27/13
to glasssite...@googlegroups.com
I will try to replicate this a bit later today.


Mike


--
You received this message because you are subscribed to the Google Groups "Glass.Sitecore.Mapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glasssitecorema...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kyle Heon

unread,
Nov 27, 2013, 9:56:55 AM11/27/13
to glasssite...@googlegroups.com, mikeed...@googlemail.com
Thanks Mike, appreciate the help. I have pushed my code up to https://github.com/Refactored/Glass.Sitecore.Mapper/tree/glasswebcontrol should you want to take a look at what I have in there so far.

Kyle Heon

unread,
Nov 27, 2013, 10:28:36 AM11/27/13
to glasssite...@googlegroups.com, mikeed...@googlemail.com
Mike,

I think I might be working off the wrong repository. I forked develop branch under the Glass-lu account but just noticed you have a 3.0 branch under your Github profile. I forked that repository and see if I can get things working. I'll branch from develop there as well.

Kyle Heon

unread,
Nov 27, 2013, 11:33:42 AM11/27/13
to glasssite...@googlegroups.com, mikeed...@googlemail.com
Wow, the Glass.Mapper project has quite the setup. Working through build issues, right now it's building but the deployment is breaking my test site. Will keep plugging away.

Kyle Heon

unread,
Nov 27, 2013, 1:40:53 PM11/27/13
to glasssite...@googlegroups.com, mikeed...@googlemail.com
Okay Mike, I had to cheat to get the solution to build (I just disabled the integration tests) and had to reset all of the Sitecore dependencies (even though they were in the Depends folder). I didn't commit those changes though, just pushed up the new files and the affected csproj file.

You can take a look in my glasswebcontrol branch available here: https://github.com/Refactored/Glass.Mapper/tree/glasswebcontrol

I'm still experiencing the SitecoreContext error. I tried to troubleshoot this but it's failing early and I'm not sure where to hook in to see why this isn't being setup.

-K

Kyle Heon

unread,
Nov 28, 2013, 11:33:37 AM11/28/13
to glasssite...@googlegroups.com, mikeed...@googlemail.com
I have made some additional updates to my glasswebcontrol branch (https://github.com/Refactored/Glass.Mapper/tree/glasswebcontrol) however there is an issue with Editable field output after changing the WebControl DataSource via Page Editor. Details below:

Value cannot be null. Parameter name: objectToSwitchTo

   at Sitecore.Common.Switcher`2.Enter(TValue objectToSwitchTo)

   at Glass.Mapper.Sc.GlassHtml.MakeEditable[T](Expression`1 field, Expression`1 standardOutput, T model, Object parameters, Context context, Database database, TextWriter writer) in c:\clientWork\Glass.Mapper\Source\Glass.Mapper.Sc\GlassHtml.cs:line 546

I have traced this to the following code:

using (new ContextItemSwitcher(scClass))

                    {

                        RenderFieldArgs renderFieldArgs = new RenderFieldArgs();

                        renderFieldArgs.Item = scClass;


The scClass variable is null causing the error. Digging further I find this line of code returning no item. What appears to be the cause is that the database being passed in at this point isn't master but instead core.

var scClass = config.ResolveItem(finalTarget, database);

Digging some more this database variable is passed in by this code:

private string MakeEditable<T>(Expression<Func<T, object>> field, Expression<Func<T, string>> standardOutput, T target, object parameters)

        {

            StringBuilder sb = new StringBuilder();

            var writer = new StringWriter(sb);

            var result = MakeEditable(field, standardOutput, target, parameters, _context, SitecoreContext.Database, writer);

            result.Dispose();

            writer.Flush();

            writer.Close();

            return sb.ToString();

        }

 For some reason the SitecoreContext.Database is core and not master. Not sure where to go next.

-K
 

Kyle Heon

unread,
Nov 28, 2013, 11:42:45 AM11/28/13
to glasssite...@googlegroups.com, mikeed...@googlemail.com

private string MakeEditable<T>(Expression<Func<T, object>> field, Expression<Func<T, string>> standardOutput, T target, object parameters)

        {

            StringBuilder sb = new StringBuilder();

            var writer = new StringWriter(sb);

            var result = MakeEditable(field, standardOutput, target, parameters, _context, SitecoreContext.Database, writer);

            result.Dispose();

            writer.Flush();

            writer.Close();

            return sb.ToString();

        }

So at this point, the Sitecore.Context.Database is master while the SitecoreContext.Database is core.

-K
Reply all
Reply to author
Forward
0 new messages