Example #1
0
        /// <summary>
        /// Uploads a file to the service and sends signal for service to send the file to a different client.
        /// </summary>
        public void UploadFile()
        {
            if (_receiver.Name!=null&&_viewModel.Name == _receiver.Name)
            {
                MessageBoxResult result = MessageBox.Show("Select a file receiver.");
                return;
            }
            _viewModel.FileWindowModel.DoneLabel = "Uploading to service";
            Thread thread = new Thread(() =>
            {
                string[] address = _viewModel.ConnectionIP.Split(':');
                string fileServicePath = "/WPFHost/http";
                var fileEndAdd = new EndpointAddress("http://" + address[0] + ":" + (int.Parse(address[1]) + 1111) + fileServicePath);
                var fileBinding = new BasicHttpBinding("httpBinding");
                _fileChannel = new FileTransferWcfClient(fileBinding, fileEndAdd);
                try
                {
                    _fileChannel.Open();
                    _fileChannel.SetProxy(_fileChannel.ChannelFactory.CreateChannel());
                    // get some info about the input file
                    FileInfo fileInfo = new FileInfo(_viewModel.FileWindowModel.SelectedFile);
                    _viewModel.FileWindowModel.ProgressMax = fileInfo.Length;
                    // open input stream
                    using (FileStream stream = new FileStream(_viewModel.FileWindowModel.SelectedFile, FileMode.Open, FileAccess.Read))
                    {
                        using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream))
                        {
                            uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged;

                            // upload file
                            _fileChannel.UploadFile(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress);
                        }
                    }
                    // close service client
                    _fileChannel.Close();
                    _channel.SendFileToClientAsync(_localClient,_receiver, fileInfo.Name);
                    _viewModel.FileWindowModel.DoneLabel = "Sending to client";
                }
                catch (Exception ex)
                {
                    _viewModel.Name = ex.ToString();
                }
            });
            thread.Start();
        }