Exemplo n.º 1
0
 /// <summary>
 /// Execute the asynchronous action.
 /// </summary>
 /// <param name="action">The action handler.</param>
 /// <param name="actionName">The unique action name.</param>
 public virtual void Execute(System.Action <Nequeo.DataAccess.NequeoCompany.Data.Extension.Assets> action, string actionName)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     if (String.IsNullOrEmpty(actionName))
     {
         throw new ArgumentNullException("actionName");
     }
     _asyncExecuteAssets.Execute(action, actionName);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Execute the asynchronous action.
 /// </summary>
 /// <param name="action">The action handler.</param>
 /// <param name="actionName">The unique action name.</param>
 public virtual void Execute(System.Action <Nequeo.Wpf.NequeoCompany.Controls.Account> action, string actionName)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     if (String.IsNullOrEmpty(actionName))
     {
         throw new ArgumentNullException("actionName");
     }
     _asyncExecuteAccount.Execute(action, actionName);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Execute the asynchronous action for the channel.
        /// </summary>
        /// <param name="action">The action handler</param>
        public virtual async Task <Boolean> Execute(Action <T> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            // Return the result.
            return(await _asyncExecute.Execute(action));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Upload a file to the remote server.
        /// </summary>
        /// <param name="localSourcePath">The local source file and path to read data from.</param>
        /// <param name="position">The position to start writing from.</param>
        /// <param name="asyncOperation">Should the download be execute asynchronously.</param>
        /// <returns>True if the operation was successful; else false.</returns>
        public bool UploadFile(string localSourcePath, long position = 0, bool asyncOperation = false)
        {
            try
            {
                // If no async operation is requested.
                if (!asyncOperation)
                {
                    // Open the local source file where
                    // the data will be read from.
                    _localSource = new FileStream(localSourcePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

                    // If an upload position exists.
                    if (position > 0)
                    {
                        _localSource.Position = position;
                    }

                    // Set the temp source file name.
                    _tempStructedFile = localSourcePath + ".temp";

                    // Create a new source file that will contain the structured data at the top of the file
                    // and all the data of the origin source file information.
                    _localSourceTemp = new FileStream(_tempStructedFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

                    // Get the source file name
                    string fileName = Path.GetFileName(localSourcePath);

                    // Create the request structed data.
                    TextWriter textWriter = new StreamWriter(_localSourceTemp);
                    textWriter.Write(
                        "UP".PadRight(OPERATION_STRUCTURE_BUFFER_SIZE) +
                        fileName.PadRight(FILENAME_STRUCTURE_BUFFER_SIZE) +
                        _localSourceTemp.Length.ToString().PadRight(FILE_SIZE_STRUCTURE_BUFFER_SIZE) +
                        position.ToString().PadRight(FILE_POSITION_STRUCTURE_BUFFER_SIZE));
                    textWriter.Flush();

                    // Copy the local source stream to the local
                    // temp source file created.
                    _localSource.CopyTo(_localSourceTemp);
                    _localSource.Close();

                    // Flush the new data written to the temp file
                    // and set the read seek point back to the begining
                    // of the file.
                    _localSourceTemp.Flush();
                    _localSourceTemp.Seek(0, SeekOrigin.Begin);

                    // Assign the current loacl source temp instance to the local
                    // source upload stream.
                    _localSourceUpload = _localSourceTemp;

                    // Create a new request
                    _requestUpload               = System.Net.WebRequest.Create(new Uri(_remoteUri));
                    _requestUpload.Method        = "POST";
                    _requestUpload.ContentLength = _localSourceTemp.Length;
                    _requestUpload.Credentials   = _networkCredential;

                    // Transfer the data from the server to the loacl source.
                    // At this point the transfer is initiated.
                    // Initiate the request, create the communication connection
                    TransferData(_localSourceTemp, _requestUpload.GetRequestStream(), _requestUpload.ContentLength, _timeout);

                    // Initiate the request, create the communication connection.
                    _responseUpload = _requestUpload.GetResponse();
                }
                else
                {
                    // Start a new async upload.
                    _asyncExecute.Execute <bool>(u => u.UploadFile(localSourcePath), "UploadFile");
                }

                // return true.
                return(true);
            }
            catch (Exception ex)
            {
                // Clean-up
                if (_localSourceTemp != null)
                {
                    _localSourceTemp.Close();
                }

                // Clean-up
                if (_localSource != null)
                {
                    _localSource.Close();
                }

                // Clean-up
                if (_localSourceUpload != null)
                {
                    _localSourceUpload.Close();
                }

                // Clean-up
                if (_responseUpload != null)
                {
                    _responseUpload.Close();
                }

                // Clean-up
                if (_requestUpload != null)
                {
                    if (_requestUpload.GetRequestStream() != null)
                    {
                        _requestUpload.GetRequestStream().Close();
                    }
                }

                try
                {
                    // Clean-up delete the temp structured file.
                    if (!String.IsNullOrEmpty(_tempStructedFile))
                    {
                        File.Delete(_tempStructedFile);
                    }
                }
                catch { }

                if (_asyncExecute.Exception != null)
                {
                    throw new Exception(ex.Message, new Exception(_asyncExecute.Exception.Message));
                }
                else
                {
                    throw ex;
                }
            }
            finally
            {
                // Clean-up
                if (!asyncOperation)
                {
                    if (_localSourceTemp != null)
                    {
                        _localSourceTemp.Close();
                    }
                }

                // Clean-up
                if (!asyncOperation)
                {
                    if (_localSource != null)
                    {
                        _localSource.Close();
                    }
                }

                // Clean-up
                if (!asyncOperation)
                {
                    if (_localSourceUpload != null)
                    {
                        _localSourceUpload.Close();
                    }
                }

                // Clean-up
                if (!asyncOperation)
                {
                    if (_requestUpload != null)
                    {
                        if (_requestUpload.GetRequestStream() != null)
                        {
                            _requestUpload.GetRequestStream().Close();
                        }
                    }
                }

                try
                {
                    // Clean-up delete the temp structured file.
                    if (!asyncOperation)
                    {
                        if (!String.IsNullOrEmpty(_tempStructedFile))
                        {
                            File.Delete(_tempStructedFile);
                        }
                    }
                }
                catch { }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Upload a file to the remote server.
        /// </summary>
        /// <param name="localSourcePath">The local source file and path to read data from.</param>
        /// <param name="position">The position to start writing from.</param>
        /// <param name="asyncOperation">Should the download be execute asynchronously.</param>
        /// <returns>True if the operation was successful; else false.</returns>
        public bool UploadFile(string localSourcePath, long position = 0, bool asyncOperation = false)
        {
            try
            {
                // If no async operation is requested.
                if (!asyncOperation)
                {
                    // Open the local source file where
                    // the data will be read from.
                    _localSource = new FileStream(localSourcePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

                    // If an upload position exists.
                    if (position > 0)
                    {
                        _localSource.Position = position;
                    }

                    // Get the source file name
                    string fileName    = Path.GetFileName(localSourcePath);
                    string iniPosition = (position > 0 ? "&P=" + position.ToString() : "");

                    // Create a new request
                    _requestUpload               = System.Net.WebRequest.Create(new Uri(_remoteUri + "?O=UP&F=" + fileName + "&S=" + _localSource.Length.ToString() + iniPosition));
                    _requestUpload.Method        = "POST";
                    _requestUpload.ContentLength = _localSource.Length;
                    _requestUpload.Credentials   = _networkCredential;

                    // Transfer the data from the server to the loacl source.
                    // At this point the transfer is initiated.
                    // Initiate the request, create the communication connection
                    TransferData(_localSource, _requestUpload.GetRequestStream(), _requestUpload.ContentLength, _timeout);

                    // Initiate the request, create the communication connection.
                    _responseUpload = _requestUpload.GetResponse();
                }
                else
                {
                    // Start a new async upload.
                    _asyncExecute.Execute <bool>(u => u.UploadFile(localSourcePath), "UploadFile");
                }

                // return true.
                return(true);
            }
            catch (Exception ex)
            {
                // Clean-up
                if (_localSource != null)
                {
                    _localSource.Close();
                }

                // Clean-up
                if (_localSourceUpload != null)
                {
                    _localSourceUpload.Close();
                }

                // Clean-up
                if (_responseUpload != null)
                {
                    _responseUpload.Close();
                }

                if (_asyncExecute.Exception != null)
                {
                    throw new Exception(ex.Message, new Exception(_asyncExecute.Exception.Message));
                }
                else
                {
                    throw ex;
                }
            }
            finally
            {
                // Clean-up
                if (!asyncOperation)
                {
                    if (_localSource != null)
                    {
                        _localSource.Close();
                    }
                }

                // Clean-up
                if (!asyncOperation)
                {
                    if (_localSourceUpload != null)
                    {
                        _localSourceUpload.Close();
                    }
                }
            }
        }