0

We are using HttpSessionStateBase to store messages in a set up similar to this working example:

public class HttpSessionMessageDisplayFetch : IMessageDisplayFetch
{
    protected HttpSessionStateBase _session;

    private IList<ICoreMessage> messages
    {
        get
        {
            if (_session[EchoCoreConstants.MESSAGE_KEY] == null)
                _session[EchoCoreConstants.MESSAGE_KEY] = new List<ICoreMessage>();

            return _session[EchoCoreConstants.MESSAGE_KEY] as IList<ICoreMessage>;
        }
    }

    public HttpSessionMessageDisplayFetch()
    {
        if (HttpContext.Current != null)
            _session = new HttpSessionStateWrapper(HttpContext.Current.Session);
    }

    public void AddMessage(ICoreMessage message)
    {
        if (message != null)
            messages.Add(message);
    }

    public IEnumerable<IResultPresentation> FlushMessagesAsPresentations(IResultFormatter formatter)
    {
        var mToReturn = messages.Select(m => m.GetPresentation(formatter)).ToList();

        messages.Clear();

        return mToReturn;
    }
}

When we pass in a QualityExplicitlySetMessage (which inherits from ICoreMessage, see below) it is saved correctly to messages.

This is how the object looks after being inserted into the messages list, at the end of AddMessage(ICoreMessage message) above.

But when we come to access it after changing controllers the inherited member's properties are null, which causes a variety of null reference exceptions.

This is how the object now looks after we call FlushMessagesAsPresentations. I've commented out var mToReturn... as this tries to access one of these null ref properties.

I'd like to ask the following:

  • Why is the HttpSessionStateBase failing to capture these values taken by the inherited type?
  • Is this an issue in saving to the HttpSession or in retrieving?
  • Is this anything to do with, as I suspect, inheritance?
  • Or is the fact I'm potentially calling a new controller that dependency injects the HttpSessionMessageDisplayFetch causing an issue?

I'm a first-time poster so please let me know if I'm making any kind of faux pas - Super keen to learn! Any input is very welcome.

Some potentially useful code snippets:

QualityExplicitlySetMessage

public class QualityExplicitlySetMessage : QualityChangeMessage
{
    public QualityExplicitlySetMessage(IQPossession before, IQPossession after, IQEffect qEffect)
        : base(before, after, qEffect)
    {
        IsSetToExactly = true;
    }
}

QualityChangeMessage - Working example

public abstract class QualityChangeMessage : CoreMessage, IQualityChangeMessage
{
    protected PossessionChange Change;

    public PossessionChange GetPossessionChange()
    {
        return Change;
    }

    protected QualityChangeMessage(IQPossession before, IQPossession after, IQEffect qEffect)
    {
        Change = new PossessionChange(before, after, qEffect);
        StoreQualityInfo(qEffect.AssociatedQuality);
    }

    public override IResultPresentation GetPresentation(IResultFormatter formatter)
    {
        return formatter.GetQualityResult(this);
    }

    #region IQualityChangeMessage implementation

    public int LevelBefore
    {
        get { return Change.Before.Level; }
    }

    //... And so on with values dependent on the Change property.

}

CoreMessage - Working example

public abstract class CoreMessage : ICoreMessage
    {

    public string MessageType
    {
        get { return GetType().ToString(); }
    }

    public string ImageTooltip
    {
        get { return _imagetooltip; }
        set { _imagetooltip = value; }
    }

    public string Image
    {
        get { return _image; }
        set { _image = value; }
    }

    public int? RelevantQualityId { get; set; }


    protected void StoreQualityInfo(Quality q)
    {
        PyramidNumberIncreaseLimit = q.PyramidNumberIncreaseLimit;
        RelevantQualityId = q.Id;
        RelevantQualityName = q.Name;
        ImageTooltip = "<strong>" + q.Name + "</strong><br/>" + q.Description + "<br>" +
                       q.EnhancementsDescription;
        Image = q.Image;
    }

    public virtual IResultPresentation GetPresentation(IResultFormatter formatter)
    {
        return formatter.GetResult(this);
    }
}

UserController - Working example.

public partial class UserController : Controller
{
    private readonly IMessageDisplayFetch _messageDisplayFetch;

    public UserController(IMessageDisplayFetch messageDisplayFetch)
    {
        _messageDisplayFetch = messageDisplayFetch;
    }

    public virtual ActionResult MessagesForStoryletWindow()
    {
        var activeChar = _us.CurrentCharacter();
        IEnumerable<IResultPresentation> messages;

        messages = _messageDisplayFetch.FlushMessagesAsPresentations(_storyFormatter);

        var vd = new MessagesViewData(messages)
        {
            Character = new CharacterViewData(activeChar),
        };
        return View(Views.Messages, vd);
    }
}
  • R.I.P. minimal example :/ – deW1 Sep 09 '16 at 14:53
  • Hi deW1. This is a problem that could quite a number of causes and is likely the result of confused inheritance, so an MWE would not be conducive. That being said, I'll have a read over and reduce the size where I can. – user2212498 Sep 09 '16 at 15:03
  • Can you elaborate what does "after changing controllers" mean? – Andrei Sep 09 '16 at 15:14
  • Hi @Andrei - Of course, sorry if this isn't clear enough. Saving the message and adding the message are called at two different times in execution, possibly not by the same instance of the UserController. – user2212498 Sep 09 '16 at 15:25

0 Answers0