示例#1
0
        void MakeRequest(
            UnsafeNativeMethods.StateProtocolVerb verb,
            String id,
            UnsafeNativeMethods.StateProtocolExclusive exclusiveAccess,
            int extraFlags,
            int timeout,
            int lockCookie,
            byte[]                                  buf,
            int cb,
            int networkTimeout,
            out UnsafeNativeMethods.SessionNDMakeRequestResults results)
        {
            int    hr;
            string uri;
            OutOfProcConnection conn = null;
            HandleRef           socketHandle;
            bool checkVersion = false;

            Debug.Assert(timeout <= SessionStateModule.MAX_CACHE_BASED_TIMEOUT_MINUTES, "item.Timeout <= SessionStateModule.MAX_CACHE_BASED_TIMEOUT_MINUTES");

            SessionIDManager.CheckIdLength(id, true /* throwOnFail */);

            if (_partitionInfo == null)
            {
                Debug.Assert(s_partitionManager != null);
                Debug.Assert(_partitionResolver != null);

                _partitionInfo = (StateServerPartitionInfo)s_partitionManager.GetPartition(_partitionResolver, id);

                // If its still null, we give up
                if (_partitionInfo == null)
                {
                    throw new HttpException(SR.GetString(SR.Bad_partition_resolver_connection_string, "PartitionManager"));
                }
            }

            // Need to make sure we dispose the connection if anything goes wrong
            try {
                conn = (OutOfProcConnection)_partitionInfo.RetrieveResource();
                if (conn != null)
                {
                    socketHandle = new HandleRef(this, conn._socketHandle.Handle);
                }
                else
                {
                    socketHandle = new HandleRef(this, INVALID_SOCKET);
                }

                if (_partitionInfo.StateServerVersion == -1)
                {
                    // We don't need locking here because it's okay to have two
                    // requests initializing s_stateServerVersion.
                    checkVersion = true;
                }

                Debug.Trace("OutOfProcSessionStateStoreMakeRequest",
                            "Calling MakeRequest, " +
                            "socket=" + (IntPtr)socketHandle.Handle +
                            "verb=" + verb +
                            " id=" + id +
                            " exclusiveAccess=" + exclusiveAccess +
                            " timeout=" + timeout +
                            " buf=" + ((buf != null) ? "non-null" : "null") +
                            " cb=" + cb +
                            " checkVersion=" + checkVersion +
                            " extraFlags=" + extraFlags);

                // Have to UrlEncode id because it may contain non-URL-safe characters
                uri = HttpUtility.UrlEncode(s_uribase + id);

                hr = UnsafeNativeMethods.SessionNDMakeRequest(
                    socketHandle, _partitionInfo.Server, _partitionInfo.Port, _partitionInfo.ServerIsIPv6NumericAddress /* forceIPv6 */, networkTimeout, verb, uri,
                    exclusiveAccess, extraFlags, timeout, lockCookie,
                    buf, cb, checkVersion, out results);

                Debug.Trace("OutOfProcSessionStateStoreMakeRequest", "MakeRequest returned: " +
                            "hr=" + hr +
                            " socket=" + (IntPtr)results.socket +
                            " httpstatus=" + results.httpStatus +
                            " timeout=" + results.timeout +
                            " contentlength=" + results.contentLength +
                            " uri=" + (IntPtr)results.content +
                            " lockCookie=" + results.lockCookie +
                            " lockDate=" + string.Format("{0:x}", results.lockDate) +
                            " lockAge=" + results.lockAge +
                            " stateServerMajVer=" + results.stateServerMajVer +
                            " actionFlags=" + results.actionFlags);

                if (conn != null)
                {
                    if (results.socket == INVALID_SOCKET)
                    {
                        conn.Detach();
                        conn = null;
                    }
                    else if (results.socket != socketHandle.Handle)
                    {
                        // The original socket is no good.  We've got a new one.
                        // Pleae note that EnsureConnected has closed the bad
                        // one already.
                        conn._socketHandle = new HandleRef(this, results.socket);
                    }
                }
                else if (results.socket != INVALID_SOCKET)
                {
                    conn = new OutOfProcConnection(results.socket);
                }

                if (conn != null)
                {
                    _partitionInfo.StoreResource(conn);
                }
            }
            catch {
                // We just need to dispose the connection if anything bad happened
                if (conn != null)
                {
                    conn.Dispose();
                }

                throw;
            }

            if (hr != 0)
            {
                HttpException e = CreateConnectionException(_partitionInfo.Server, _partitionInfo.Port, hr);

                string phase = null;

                switch (results.lastPhase)
                {
                case (int)UnsafeNativeMethods.SessionNDMakeRequestPhase.Initialization:
                    phase = SR.GetString(SR.State_Server_detailed_error_phase0);
                    break;

                case (int)UnsafeNativeMethods.SessionNDMakeRequestPhase.Connecting:
                    phase = SR.GetString(SR.State_Server_detailed_error_phase1);
                    break;

                case (int)UnsafeNativeMethods.SessionNDMakeRequestPhase.SendingRequest:
                    phase = SR.GetString(SR.State_Server_detailed_error_phase2);
                    break;

                case (int)UnsafeNativeMethods.SessionNDMakeRequestPhase.ReadingResponse:
                    phase = SR.GetString(SR.State_Server_detailed_error_phase3);
                    break;

                default:
                    Debug.Assert(false, "Unknown results.lastPhase: " + results.lastPhase);
                    break;
                }

                WebBaseEvent.RaiseSystemEvent(SR.GetString(SR.State_Server_detailed_error,
                                                           phase,
                                                           "0x" + hr.ToString("X08", CultureInfo.InvariantCulture),
                                                           cb.ToString(CultureInfo.InvariantCulture)),
                                              this, WebEventCodes.WebErrorOtherError, WebEventCodes.StateServerConnectionError, e);

                throw e;
            }

            if (results.httpStatus == 400)
            {
                if (s_usePartition)
                {
                    throw new HttpException(
                              SR.GetString(SR.Bad_state_server_request_partition_resolver,
                                           s_configPartitionResolverType, _partitionInfo.Server, _partitionInfo.Port.ToString(CultureInfo.InvariantCulture)));
                }
                else
                {
                    throw new HttpException(
                              SR.GetString(SR.Bad_state_server_request));
                }
            }

            if (checkVersion)
            {
                _partitionInfo.StateServerVersion = results.stateServerMajVer;
                if (_partitionInfo.StateServerVersion < WHIDBEY_MAJOR_VERSION)
                {
                    // We won't work with versions lower than Whidbey
                    if (s_usePartition)
                    {
                        throw new HttpException(
                                  SR.GetString(SR.Need_v2_State_Server_partition_resolver,
                                               s_configPartitionResolverType, _partitionInfo.Server, _partitionInfo.Port.ToString(CultureInfo.InvariantCulture)));
                    }
                    else
                    {
                        throw new HttpException(
                                  SR.GetString(SR.Need_v2_State_Server));
                    }
                }
            }
        }
示例#2
0
        private void MakeRequest(System.Web.UnsafeNativeMethods.StateProtocolVerb verb, string id, System.Web.UnsafeNativeMethods.StateProtocolExclusive exclusiveAccess, int extraFlags, int timeout, int lockCookie, byte[] buf, int cb, int networkTimeout, out System.Web.UnsafeNativeMethods.SessionNDMakeRequestResults results)
        {
            int num;
            OutOfProcConnection o = null;
            bool checkVersion     = false;

            SessionIDManager.CheckIdLength(id, true);
            if (this._partitionInfo == null)
            {
                this._partitionInfo = (StateServerPartitionInfo)s_partitionManager.GetPartition(this._partitionResolver, id);
                if (this._partitionInfo == null)
                {
                    throw new HttpException(System.Web.SR.GetString("Bad_partition_resolver_connection_string", new object[] { "PartitionManager" }));
                }
            }
            try
            {
                HandleRef ref2;
                o = (OutOfProcConnection)this._partitionInfo.RetrieveResource();
                if (o != null)
                {
                    ref2 = new HandleRef(this, o._socketHandle.Handle);
                }
                else
                {
                    ref2 = new HandleRef(this, INVALID_SOCKET);
                }
                if (this._partitionInfo.StateServerVersion == -1)
                {
                    checkVersion = true;
                }
                string uri = HttpUtility.UrlEncode(s_uribase + id);
                num = System.Web.UnsafeNativeMethods.SessionNDMakeRequest(ref2, this._partitionInfo.Server, this._partitionInfo.Port, networkTimeout, verb, uri, exclusiveAccess, extraFlags, timeout, lockCookie, buf, cb, checkVersion, out results);
                if (o != null)
                {
                    if (results.socket == INVALID_SOCKET)
                    {
                        o.Detach();
                        o = null;
                    }
                    else if (results.socket != ref2.Handle)
                    {
                        o._socketHandle = new HandleRef(this, results.socket);
                    }
                }
                else if (results.socket != INVALID_SOCKET)
                {
                    o = new OutOfProcConnection(results.socket);
                }
                if (o != null)
                {
                    this._partitionInfo.StoreResource(o);
                }
            }
            catch
            {
                if (o != null)
                {
                    o.Dispose();
                }
                throw;
            }
            if (num == 0)
            {
                if (results.httpStatus == 400)
                {
                    if (s_usePartition)
                    {
                        throw new HttpException(System.Web.SR.GetString("Bad_state_server_request_partition_resolver", new object[] { s_configPartitionResolverType, this._partitionInfo.Server, this._partitionInfo.Port.ToString(CultureInfo.InvariantCulture) }));
                    }
                    throw new HttpException(System.Web.SR.GetString("Bad_state_server_request"));
                }
                if (checkVersion)
                {
                    this._partitionInfo.StateServerVersion = results.stateServerMajVer;
                    if (this._partitionInfo.StateServerVersion < WHIDBEY_MAJOR_VERSION)
                    {
                        if (s_usePartition)
                        {
                            throw new HttpException(System.Web.SR.GetString("Need_v2_State_Server_partition_resolver", new object[] { s_configPartitionResolverType, this._partitionInfo.Server, this._partitionInfo.Port.ToString(CultureInfo.InvariantCulture) }));
                        }
                        throw new HttpException(System.Web.SR.GetString("Need_v2_State_Server"));
                    }
                }
            }
            else
            {
                HttpException exception = CreateConnectionException(this._partitionInfo.Server, this._partitionInfo.Port, num);
                string        str2      = null;
                switch (results.lastPhase)
                {
                case 0:
                    str2 = System.Web.SR.GetString("State_Server_detailed_error_phase0");
                    break;

                case 1:
                    str2 = System.Web.SR.GetString("State_Server_detailed_error_phase1");
                    break;

                case 2:
                    str2 = System.Web.SR.GetString("State_Server_detailed_error_phase2");
                    break;

                case 3:
                    str2 = System.Web.SR.GetString("State_Server_detailed_error_phase3");
                    break;
                }
                WebBaseEvent.RaiseSystemEvent(System.Web.SR.GetString("State_Server_detailed_error", new object[] { str2, "0x" + num.ToString("X08", CultureInfo.InvariantCulture), cb.ToString(CultureInfo.InvariantCulture) }), this, 0xbc1, 0xc360, exception);
                throw exception;
            }
        }