using System;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel; namespace WcfServer
{
internal class Program
{
private static void Main()
{
using (var host = new ServiceHost(typeof (StreamServices)))
{
host.Opened += (a, b) => Console.WriteLine("...");
host.Open(); Console.ReadKey();
}
}
} [ServiceContract(Name = "IStreamServices",
SessionMode = SessionMode.Required,
Namespace = "http://www.msdn.com/IStreamServices/14/04/11")]
public interface IStreamServices
{
[OperationContract(Name = "Upload")]
FileInformation Upload(FileInformation fileInfo); [OperationContract(Name = "Download")]
FileInformation Download(FileInformation fileInfo);
} [ServiceBehavior(Name = "StreamServices",
Namespace = "http://www.msdn.com/StreamServices/14/04/11"
, InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple)]
public class StreamServices : IStreamServices
{
private static readonly string AttachmentPath = AppDomain.CurrentDomain.BaseDirectory + "Attachments//"; #region IServices 成员 public FileInformation Upload(FileInformation fileInfo)
{
try
{
if (fileInfo == null)
return new FileInformation {Error = "FileInformation对象不能为空"};
if (string.IsNullOrEmpty(fileInfo.FileNameNew))
fileInfo.FileNameNew = Guid.NewGuid() + fileInfo.FileSuffix;
var savePath = AttachmentPath + fileInfo.FileNameNew;
using (var fs = new FileStream(savePath, FileMode.OpenOrCreate))
{
long offset = fileInfo.Offset;
using (var write = new BinaryWriter(fs))
{
write.Seek((int) offset, SeekOrigin.Begin);
write.Write(fileInfo.Data);
fileInfo.Offset = fs.Length;
}
}
return fileInfo;
}
catch (IOException ex)
{
return new FileInformation {Error = ex.Message};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return new FileInformation {Error = "wcf内部错误"};
}
} public FileInformation Download(FileInformation fileInfo)
{
try
{
if (fileInfo == null)
return new FileInformation {Error = "FileInformation对象不能为空"};
var readFileName = AttachmentPath + fileInfo.FileName;
if (!File.Exists(readFileName))
return new FileInformation {Error = "DirectoryNotFoundException"};
var stream = File.OpenRead(readFileName);
fileInfo.Length = stream.Length;
if (fileInfo.Offset.Equals(fileInfo.Length))
return new FileInformation {Offset = fileInfo.Offset, Length = stream.Length};
var maxSize = fileInfo.MaxSize > * ? * : fileInfo.MaxSize;
fileInfo.Data =
new byte[fileInfo.Length - fileInfo.Offset <= maxSize ? fileInfo.Length - fileInfo.Offset : maxSize];
stream.Position = fileInfo.Offset;
stream.Read(fileInfo.Data, , fileInfo.Data.Length);
return fileInfo;
}
catch (IOException ex)
{
return new FileInformation {Error = ex.Message};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return new FileInformation {Error = "wcf内部错误"};
}
} #endregion
} [DataContract(Name = "MyStreamInfo")]
public class FileInformation
{
[DataMember(Name = "Length", IsRequired = true)]
public long Length { get; set; } [DataMember(Name = "FileName", IsRequired = true)]
public string FileName { get; set; } [DataMember(Name = "Data")]
public byte[] Data { get; set; } [DataMember(Name = "FileSuffix")]
public string FileSuffix { get; set; } [DataMember(Name = "Offset", IsRequired = true)]
public long Offset { get; set; } [DataMember(Name = "Error")]
public string Error { get; set; } private int maxSize = *; //200k [DataMember(Name = "MaxSize")]
public int MaxSize
{
get { return maxSize; }
set { maxSize = value; }
} [DataMember(Name = "KeyToken")]
public string KeyToken { get; set; } [DataMember(Name = "FileNameNew")]
public string FileNameNew { get; set; }
}
}
 using System;
using System.IO;
using WcfClientApp.ServiceReference1; namespace WcfClientApp
{
internal class Program
{
private static void Main()
{
Upload();
Download();
Console.ReadKey();
} /// <summary>
/// 上传
/// </summary>
private static void Upload()
{
try
{
var filePath = AppDomain.CurrentDomain.BaseDirectory + "UploadFiles//张国荣 - 共同度过.mp3";
const string fileName = "张国荣 - 共同度过.mp3";
const int maxSize = *;
if (!File.Exists(filePath))
{
Console.WriteLine("DirectoryNotFoundException");
return;
}
FileStream stream = File.OpenRead(filePath);
var fileInfo = new MyStreamInfo {Length = stream.Length, FileName = fileName,FileSuffix=".mp3"};
using (var client = new StreamServicesClient())
{
while (fileInfo.Length != fileInfo.Offset)
{
fileInfo.Data =
new byte[
fileInfo.Length - fileInfo.Offset <= maxSize
? fileInfo.Length - fileInfo.Offset
: maxSize];
stream.Position = fileInfo.Offset;
stream.Read(fileInfo.Data, , fileInfo.Data.Length);
fileInfo = client.Upload(fileInfo);
if (!string.IsNullOrEmpty(fileInfo.Error))
{
Console.WriteLine(fileInfo.Error);
break;
}
}
if (fileInfo.Length.Equals(fileInfo.Offset))
Console.WriteLine("Upload successful!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 下载
/// </summary>
private static void Download()
{
try
{
var filePath = AppDomain.CurrentDomain.BaseDirectory +
"DownloadFiles//c228d4df-8bdc-468b-96ec-46860c2f026a.mp3";
const string fileName = "c228d4df-8bdc-468b-96ec-46860c2f026a.mp3";
var fileInfo = new MyStreamInfo {FileName = fileName, Length = , MaxSize = *};
using (var client = new StreamServicesClient())
{
while (fileInfo.Length != fileInfo.Offset)
{
fileInfo = client.Download(fileInfo);
if (!string.IsNullOrEmpty(fileInfo.Error))
{
Console.WriteLine(fileInfo.Error);
break;
}
using (var fs = new FileStream(filePath, FileMode.OpenOrCreate))
{
long offset = fileInfo.Offset;
using (var write = new BinaryWriter(fs))
{
write.Seek((int) offset, SeekOrigin.Begin);
write.Write(fileInfo.Data);
fileInfo.Offset = fs.Length;
}
}
}
if (fileInfo.Length.Equals(fileInfo.Offset))
Console.WriteLine("download successful!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

使用MaxSize控制下载、上传大小(k)

一开始设计的时候,我考虑使用双工模式,但是觉得有不太好,使用双工模式反而增加了代码开发量。

不知道各位有没有什么更好的解决办法?是否愿意分享一下,谢谢各位的指点。

WCF传输大数据 --断点续传(upload、download)的更多相关文章

  1. WCF传输大数据的设置

    在从客户端向WCF服务端传送较大数据(>65535B)的时候,发现程序直接从Reference的BeginInvoke跳到EndInvoke,没有进入服务端的Service实际逻辑中,怀疑是由于 ...

  2. 【转】WCF传输大数据的设置

    在从客户端向WCF服务端传送较大数据(>65535B)的时候,发现程序直接从Reference的BeginInvoke跳到EndInvoke,没有进入服务端的Service实际逻辑中,怀疑是由于 ...

  3. WCF传输大数据的设置2

    本节主要内容:1.如何读取Binding中的binding元素.2.CustomBinding元素的基本配置.3.代码示例 一.Bingding是由binding元素构成的,可以根据实际需要,进行适当 ...

  4. 快速传输大数据(tar+lz4+pv)

    快速传输大数据(tar+lz4+pv)   如果用传统SCP远程拷贝,速度是比较慢的.现在采用lz4压缩传输.LZ4是一个非常快的无损压缩算法,压缩速度在单核300MB/S,可扩展支持多核CPU.它还 ...

  5. 解决WCF传输的数据量过大问题

    今天写了个WCF接口,然后自测通过,和别人联调时报 远程服务器返回错误: (413) Request Entity Too Large        错误!记得以前写的时候也出现过这个错误,大致解决办 ...

  6. WCF传送大数据时的错误“ 超出最大字符串内容长度配额”

    格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: GetLzdtArticleResult.InnerException 消息是“反序 ...

  7. php传输大数据大文件时候php.ini相关设置

    post_max_size which is directly related to the POST size---针对采用post上传的,大文件,此项为关键 upload_max_filesize ...

  8. 【转载】大数据量传输时配置WCF的注意事项

    WCF传输数据量的能力受到许多因素的制约,如果程序中出现因需要传输的数据量较大而导致调用WCF服务失败的问题,应注意以下配置: 1.MaxReceivedMessageSize:获取或设置配置了此绑定 ...

  9. WCF 传输和接受大数据

    向wcf传入大数据暂时还没找到什么好方案,大概测了一下传输2M还是可以的,有待以后解决. 接受wcf传回的大数据,要进行web.config的配置,刚开是从网上搜自己写进行配置,折磨了好长时间. 用以 ...

随机推荐

  1. Docker常用命令汇总,和常用操作举例

    Docker命令 docker 常用命令如下 管理命令: container 管理容器 image 管理镜像 network 管理网络 node 管理Swarm节点 plugin 管理插件 secre ...

  2. 【Python】实现对大文件的增量读取

    背景 前段时间在做一个算法测试,需要对源于日志的数据进行分析才能获取到结果:日志文件较大,所以想要获取数据的变化曲线,增量读取是最好的方式. 网上有很多人的技术博客都是写的用for循环readline ...

  3. PowerDesigner16工具学习笔记-建立BPM

    根据不同用途,BPM分为分析性(Analysis).执行型(Executable)和协作型(Collaborative) BPM的类型 业务流程语言 描述  分析型  Analysis  提供流程层次 ...

  4. 关于 XML 头声明和standalone 的解释

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <roo ...

  5. 在返回值拒绝——reference

    在上一篇博客中,我们介绍了简单地使用值传递带来的种种麻烦,相信有些朋友会一心一意将其斩草除根,但是当返回值也使用了引用的时候,麻烦就来了. 依然来个简答的例子 class Rational { pub ...

  6. L187 DKK2

    Why can millions of hairs grow from our heads, and yet our palms手掌 and the soles of our feet are as ...

  7. specialized English for automation-Lesson 2 Basic Circuits of Operational Amplifiers

    排版有点乱.... ========================================================================= Operational Ampl ...

  8. Loj 114 k大异或和

    Loj 114 k大异或和 构造线性基时有所变化.试图构造一个线性基,使得从高到低位走,异或上一个非 \(0\) 的数,总能变大. 构造时让任意两个 \(bas\) 上有值的 \(i,j\) ,满足 ...

  9. 浅谈SQL Server---1

    浅谈SQL Server优化要点 https://www.cnblogs.com/wangjiming/p/10123887.html 1.SQL Server 体系结构由哪几部分组成? 2.SQL ...

  10. streamsets 包管理

    streamsets 自带一个包管理,可以方便的进行三方组件的添加,比如我们需要处理mongodb 数据,默认是没有添加这个组件的,操作如下: 选择包管理 选择组件 安装 点击安装 提示界面 安装完成 ...