【问题标题】:Sample Session-State Store Provider - Microsoft Example会话状态存储提供程序示例 - Microsoft 示例
【发布时间】:2016-04-16 13:39:34
【问题描述】:

我正在尝试使用 Microsoft 的此示例实现会话状态提供程序:

http://msdn.microsoft.com/en-us/library/ms178589.aspx

但我无法编译它,因为 .NET 会抛出此错误:

Error   1   
'Project.Session.OdbcSessionStateStore' does not implement inherited abstract member     'System.Web.SessionState.SessionStateStoreProviderBase.CreateNewStoreData(System.Web.HttpContext, int)'

Error   2   'Project.Session.OdbcSessionStateStore.CreateNewStoreData(System.Web.HttpContext, double)' is a new virtual member in sealed class 'Project.Session.OdbcSessionStateStore'

Error   3   'Project.Session.OdbcSessionStateStore.CreateNewStoreData(System.Web.HttpContext, double)': no suitable method found to override

使用 CreateNewStoreData 覆盖的确切代码:

    //
    // SessionStateProviderBase.CreateNewStoreData
    //

    public override SessionStateStoreData CreateNewStoreData(
      HttpContext context,
      double timeout)
    {
        return new SessionStateStoreData(new SessionStateItemCollection(),
          SessionStateUtility.GetSessionStaticObjects(context),
          (int)timeout);
    }

【问题讨论】:

    标签: c# .net session session-state


    【解决方案1】:

    从您的 MSDN 链接中,OdbcSessionStateStore 类继承自 SessionStateStoreProviderBase 抽象类。 Click Here了解更多信息

    由于您必须重写抽象类中的所有抽象方法(除非您创建的类也是抽象类),因此在此示例中您将需要重写抽象方法CreateNewStoreDataClick Here了解更多信息

    当覆盖方法签名时应该匹配。在您的 MSDN 链接的示例代码中,签名是

    public override SessionStateStoreData CreateNewStoreData(
          HttpContext context,
          double timeout)
    

    在实际情况下应该在哪里

    public override SessionStateStoreData CreateNewStoreData(
        HttpContext context,
        int timeout
    )
    

    注意double timeout而不是int timeout的区别

    【讨论】:

      【解决方案2】:

      示例中有错误。您需要更改 SessionStateStoreData 方法签名以使用 int 作为超时参数,因为 the method signature in the base class is defined 就是这样。 (这个类重写了基方法,所以方法签名必须相同。)

            public override SessionStateStoreData CreateNewStoreData(
                HttpContext context,
                int timeout)
      

      这意味着您还必须确保传递给此方法的参数是 int,而不是 double。我相信示例类在 GetSessionStoreItem 中只包含一个调用。您所要做的就是将值转换为 int,如下所示:

            if (actionFlags == SessionStateActions.InitializeItem)
                item = CreateNewStoreData(context, (int) pConfig.Timeout.TotalMinutes);
            else
                item = Deserialize(context, serializedItems, timeout);
      

      【讨论】:

        猜你喜欢
        • 2013-07-21
        • 2012-08-23
        • 1970-01-01
        • 2016-04-28
        • 2015-03-10
        • 2011-06-09
        • 2021-11-07
        • 2018-02-02
        • 1970-01-01
        相关资源
        最近更新 更多