Ejemplo n.º 1
0
        /// <summary>
        /// Прием файла с сервера
        /// </summary>
        public void LoadFileFromServer(string settingName)
        {
            Logger logger = LogManager.GetCurrentClassLogger();

            logger.Debug(settingName + ": Начало приема файла с сервера");

            SetCurrentStatus(settingName, Job.GetFileFromServer, JobStatus.Process);
            OnExchangeProcess(GetCurrentStatus(settingName));

            var baseConfig = GetBaseConfig(settingName);
            ChannelFactory <IFileTransfer> factory;

            Status remoteStatus = RemoteGetCurrentStatus(settingName);

            if (remoteStatus.jobStatus == JobStatus.Error && remoteStatus.description.Contains("Не найдена настройка"))
            {
                throw new Exception(remoteStatus.description);
            }

            //попробуем загрузить из файла конфигурации
            try{
                factory = new ChannelFactory <IFileTransfer>("IFileTransfer");
            }
            catch {
                //если нет то создаем вручную

                NetTcpBinding streamBinding = new NetTcpBinding(SecurityMode.None);
                streamBinding.CloseTimeout           = TimeSpan.FromHours(0.5);
                streamBinding.OpenTimeout            = TimeSpan.FromHours(0.5);
                streamBinding.SendTimeout            = TimeSpan.FromHours(13);
                streamBinding.TransferMode           = TransferMode.Buffered;
                streamBinding.MaxReceivedMessageSize = 429496729;

                EndpointAddress endpointAdress = new EndpointAddress(config.HostSetting.Protocol + @"://" + baseConfig.ServerAdress + "/FileTransfer");
                factory = new ChannelFactory <IFileTransfer>(streamBinding, endpointAdress);
            }


            //string patchToFile = Path.GetTempPath() + @"SA1C\In\Message" + settingName + ".xml.zip";

            IFileTransfer channel = factory.CreateChannel();

            long   offset = 0;
            Status status = GetCurrentStatus(settingName);

            if (status.currentPosInFile > 0)
            {
                offset = status.currentPosInFile;
            }

            using (Stream fileData = channel.LoadFile(settingName, offset))
            {
                FileData sendData = new FileData();
                sendData.settingName    = settingName;
                sendData.fileByteStream = fileData;

                UploadFile(sendData);
                //using (FileStream tempFile = File.Create(patchToFile))
                //{
                //
                //	fileData.CopyTo(tempFile, 4096);
                //}
            }

            factory.Close();

            //разархивируем принятый файл
            //FileInfo fi = new FileInfo(patchToFile);
            //ZipFile.Decompress(fi);
            //TODO: удаляем архив
            //fi.Delete();

            logger.Debug(settingName + ": Конец приема файла с сервера");

            SetCurrentStatus(settingName, Job.GetFileFromServer, JobStatus.Complite, "", 0);
            OnExchangeProcess(GetCurrentStatus(settingName));
        }