HttpSessionStateBase丢失继承类型的属性值

时间:2016-09-09 14:48:28

标签: c# asp.net inheritance httpsession

我们使用HttpSessionStateBase在类似于这个工作示例的设置中存储消息:

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;
    }
}

当我们传入QualityExplicitlySetMessage(继承自ICoreMessage,见下文)时,它会正确保存到消息中。

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

但是当我们在更改控制器后访问它时,继承的成员的属性为null,这会导致各种空引用异常。

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.

我想问以下问题:

  • 为什么HttpSessionStateBase无法捕获这些值 由继承的类型?
  • 这是保存到HttpSession还是检索?
  • 的问题
  • 这与我怀疑的继承有关吗?
  • 或者我可能正在调用一个依赖注入HttpSessionMessageDisplayFetch导致问题的新控制器?

我是第一次发布海报,所以如果我做任何失礼,请告诉我 - 超级热衷于学习!任何意见都非常欢迎。

一些可能有用的代码段:

QualityExplicitlySetMessage

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

QualityChangeMes​​sage - 工作示例

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 - 工作示例

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 - 工作示例。

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);
    }
}

0 个答案:

没有答案