2

我们使用 StateServer 来处理 Session 以获得已知的好处(网络场、IIS 回收)。

但是我试图弄清楚如何使这个容错。我们存储在 Session 中的任何内容都不是关键的,它只是用于性能。因此,如果 StateServer 不可用,我们很乐意从磁盘重新加载。

但是似乎无法检测 StateServer 是否在线,因此即使 StateServer 关闭,以下代码也可以正常运行

try
{
    //It is not NULL as it has been configured
    if (HttpContext.Current.Session != null)
        Session["Test"] = "value";
}
// No exception is thrown
catch (Exception)
{
    throw new Exception();
}

现在对我来说,没有抛出异常是有道理的。如果必须在每次写入时检查状态,会话处理将不会非常高效。所以我猜测会发生什么,它会在写入响应时写入所有会话变量。

问题就在于此,当它尝试写入 Session 时,它会失败并出现 500 错误,而且我无论如何也不知道要拦截这个错误并处理它。

无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State 服务已启动并且客户端和服务器端口相同。

我想要发生的是写入只是静默失败(或记录错误)并且客户端不受影响。正如现在所写的那样,由于这个单点故障,整个站点都崩溃了。

任何想法 - 我是否遗漏了一些明显的东西?

4

2 回答 2

1

好吧,这可能很难。asp.net 使用 session 紧密,所以如果 session 存储失败,那么 asp.net 在 session 模块的初始化过程中也会失败。您可以编写自己的会话状态提供程序,它将包装现有的,如果失败,它将返回空的会话项,但很难使用它,因为会话行为可能是不可预测的。

您可以查看内置的SQL 会话状态提供程序,如果您的 SQL 服务器具有复制功能,该提供程序具有故障转移功能。

更新1

这是默认会话提供程序的包装器示例

public class SessionProviderWrapper : SessionStateStoreProviderBase
{
    private readonly SessionStateStoreProviderBase _provider;

    private static Func<SessionStateStoreProviderBase> _createProvider;

    static SessionProvider()
    {
        _createProvider = InitializerProvider();
    }

    private static Func<SessionStateStoreProviderBase> InitializerProvider()
    {
        if (_createProvider != null)
            return _createProvider;

        var sessionType = "stateserver"; // you can switch to another session provider

        Type type;
        switch (sessionType)
        {
            case "inproc":
                type = Type.GetType("System.Web.SessionState.InProcSessionStateStore, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
                break;

            case "sql":
                type = Type.GetType("System.Web.SessionState.SqlSessionStateStore, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
                break;

            case "stateserver":
                type = Type.GetType("System.Web.SessionState.OutOfProcSessionStateStore, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
                break;

            default:
                throw new ConfigurationErrorsException("Unknow session type: " + sessionType);
        }

        if (type == null)
        {
            throw new InvalidOperationException("Failed to find session provider for " + sessionType);
        }

        _createProvider = GenerateConstructorCall(type);

        return _createProvider;
    }

    private static Func<SessionStateStoreProviderBase> GenerateConstructorCall(Type type)
    {
        // we are searching for public constructor
        var constructor = type.GetConstructors().FirstOrDefault(c => c.GetParameters().Length == 0);
        if (constructor == null)
        {
            // otherwise for internal. SQL session provider has internal constructor, but we don't care
            constructor = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(c => c.GetParameters().Length == 0);
        }

        var node = Expression.New(constructor);
        var lambda = Expression.Lambda<Func<SessionStateStoreProviderBase>>(node, null);
        var func = lambda.Compile();
        return func;
    }

    public SessionProvider()
    {
        var createProvider = InitializerProvider();

        _provider = createProvider();
    }

    public override void Initialize(string name, NameValueCollection config)
    {
        _provider.Initialize(name, config);
    }

    public override string Name
    {
        get { return _provider.Name; }
    }

    public override string Description
    {
        get { return _provider.Description; }
    }

    public override void Dispose()
    {
        _provider.Dispose();
    }

    public override bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback)
    {
        return _provider.SetItemExpireCallback(expireCallback);
    }

    public override void InitializeRequest(HttpContext context)
    {
        _provider.InitializeRequest(context);
    }

    public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId,
                                         out SessionStateActions actions)
    {
        try
        {
            return _provider.GetItem(context, id, out locked, out lockAge, out lockId, out actions);
        }
        catch (Exception ex)
        {
            locked = false;
            lockAge = TimeSpan.Zero;
            lockId = null;
            actions = SessionStateActions.None;
            // log ex
            return new SessionStateStoreData(new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 10);
        }
    }

    public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId,
                                                  out SessionStateActions actions)
    {
        return _provider.GetItemExclusive(context, id, out locked, out lockAge, out lockId, out actions);
    }

    public override void ReleaseItemExclusive(HttpContext context, string id, object lockId)
    {
        _provider.ReleaseItemExclusive(context, id, lockId);
    }

    public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
    {
        _provider.SetAndReleaseItemExclusive(context, id, item, lockId, newItem);
    }

    public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
    {
        _provider.RemoveItem(context, id, lockId, item);
    }

    public override void ResetItemTimeout(HttpContext context, string id)
    {
        _provider.ResetItemTimeout(context, id);
    }

    public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
    {
        return _provider.CreateNewStoreData(context, timeout);
    }

    public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
    {
        _provider.CreateUninitializedItem(context, id, timeout);
    }

    public override void EndRequest(HttpContext context)
    {
        _provider.EndRequest(context);
    }
}

基本上,您可以像 GetItem 方法一样对每个方法进行 try\catch,如果出现错误,您可以返回空会话对象。如果它在 try\catch 应用程序中失败,它仍然会存在。但是性能会降低,因为每个请求都会在 Get\Release 上抛出几个异常,这些异常将在 catch 部分处理。但无论如何,这些异常会稍微降低性能

于 2013-12-24T19:27:49.787 回答
0

我想接受 tgolisch 答案作为对我有用的解决方案。

  • 在 Global.asax 中,我们将在 Application_Error 事件中查找缺少的 StateServer 错误
  • 如果我们找到它,我们将使用 Server.ClearError() 并记录错误
  • 我们还将使用它来记录错误并可能发出警报

谢谢大家!

于 2013-12-24T19:51:29.777 回答