private void AsyncConnectCallback(IAsyncResult ar)
        {
            string returnMessage = string.Empty;

            try
            {
                // Retrieve the delegate.
                AsyncResult          result = (AsyncResult)ar;
                AsyncConnectDelegate caller = (AsyncConnectDelegate)result.AsyncDelegate;
                // if the database already exists than it is fetched otherwise created

                // Call EndInvoke to retrieve the results.
                Db = caller.EndInvoke(ar);

                if (Db == null)
                {
                    throw new MongoServerConnectionException("Asynchronous server connection failed");
                }

                returnMessage = "Successful connection";
            }
            catch (MongoDatabaseConnectionException mdcx)
            {
                returnMessage = mdcx.Message;
            }
            catch (Exception ex)
            {
                //TODO: add additional exception handling
                returnMessage = ex.Message;
            }
            finally
            {
                _databaseConnectionResetEvent.Set(); // allow dependent process that are waiting via VerifyConnected() to proceed

                if (State == MongoServerState.Disconnected)
                {
                    if (ConnectAsyncCompleted != null)
                    {
                        ConnectAsyncCompleted(ConnectionResult.Failure, returnMessage);
                    }
                }
                else if (State == MongoServerState.Connected)
                {
                    if (ConnectAsyncCompleted != null)
                    {
                        ConnectAsyncCompleted(ConnectionResult.Success, returnMessage);
                    }
                }
                else
                {
                    if (ConnectAsyncCompleted != null)
                    {
                        ConnectAsyncCompleted(ConnectionResult.Failure, returnMessage);
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Call back method invoked after CreateMongoServerAsync() has completed. Invokes the Connected event to
        /// notify subscribers that asynchronous server connection has completed and ServerConnection is ready for use.
        /// </summary>
        /// <param name="ar"></param>
        private void AsyncConnectCallback(IAsyncResult ar)
        {
            string returnMessage = string.Empty;

            try
            {
                // Retrieve the delegate.
                AsyncResult          result = (AsyncResult)ar;
                AsyncConnectDelegate caller = (AsyncConnectDelegate)result.AsyncDelegate;

                // Call EndInvoke to retrieve the results.
                _mongoServer = caller.EndInvoke(ar);

                if (_mongoServer == null)
                {
                    returnMessage = "Failure";
                }
                else
                {
                    returnMessage = "Successful";
                }
            }
            catch (MongoDatabaseConnectionException mdcx)
            {
                returnMessage = mdcx.Message;
            }
            catch (Exception ex)
            {
                returnMessage = ex.Message;
            }
            finally
            {
                _serverConnectionResetEvent.Set(); // allow dependent process that are waiting via VerifyConnected() to proceed

                if (_mongoServer == null)
                {
                    if (ConnectAsyncCompleted != null)
                    {
                        ConnectAsyncCompleted(ConnectionResult.Failure, returnMessage);
                    }
                }
                else
                {
                    if (ConnectAsyncCompleted != null)
                    {
                        ConnectAsyncCompleted(ConnectionResult.Success, returnMessage);
                    }
                }
            }
        }