我正在尝试使用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);
}
答案 0 :(得分:2)
样本中存在错误。您需要更改 SessionStateStoreData 方法签名以使用int作为timeout参数,因为这是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);
答案 1 :(得分:2)
从您的MSDN链接,类OdbcSessionStateStore
继承自SessionStateStoreProviderBase
抽象类。 Click Here了解更多信息
因为您必须覆盖抽象类中的所有抽象方法(除非您创建的类也是抽象类),在此示例中,您将需要覆盖抽象方法CreateNewStoreData
。 Click Here了解更多信息
覆盖方法时,签名应该匹配。在MSDN链接的示例代码中,签名是
public override SessionStateStoreData CreateNewStoreData(
HttpContext context,
double timeout)
在实际情况中应该是
public override SessionStateStoreData CreateNewStoreData(
HttpContext context,
int timeout
)
请注意差异double timeout
而不是int timeout