通过传递Stream对象来传递大数据文件,但是有一些限制:

1、只有 BasicHttpBinding、NetTcpBinding 和 NetNamedPipeBinding 支持传送流数据。

2、 流数据类型必须是可序列化的 Stream 或 MemoryStream。

3、 传递时消息体(Message Body)中不能包含其他数据。

4、TransferMode的限制和MaxReceivedMessageSize的限制等。

下面具体实现:新建一个FileService,接口文件的代码如下:

using System.IO;
using System.ServiceModel; namespace FileService
{ [ServiceContract]
public interface IFileService
{
[OperationContract]
void UpLoadFile(FileUploadMessage fileUploadMessage);
} [MessageContract]
public class FileUploadMessage
{
[MessageHeader]
public string FileName
{
get;
set;
} [MessageBodyMember]
public Stream FileData
{
get;
set;
}
}
}

定义FileUploadMessage类的目的是因为第三个限制,要不然文件名及其他信息就没办法传递给WCF了,根据第二个限制,文件数据是用System.IO.Stream来传递的。下面是接口的具体实现。

using System;
using System.IO; namespace FileService
{
public class FileService : IFileService
{
//保存文件的目录路径
private const string SaveDirectory = @"F:\V2.4ShareFolder\PRO"; public void UpLoadFile(FileUploadMessage fileUploadMessage)
{
string fileName = fileUploadMessage.FileName; Stream sourceStream = fileUploadMessage.FileData; FileStream targetStream = null; if (!sourceStream.CanRead)
{
throw new Exception("数据流不可读!");
} string filePath = Path.Combine(SaveDirectory, fileName); using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
//read from the input stream in 4K chunks
//and save to output stream
const int bufferLen = ; var buffer = new byte[bufferLen]; try
{
int count; while ((count = sourceStream.Read(buffer, , bufferLen)) > )
{
targetStream.Write(buffer, , count);
}
}
finally
{
sourceStream.Close();
}
}
}
}
}

实现的功能很简单,把文件保存到特定的目录中即可。下面是进行config的配置。

<?xml version="1.0" encoding="utf-8" ?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="FileServiceBinding" maxReceivedMessageSize="" transferMode="Streamed"></binding>
</basicHttpBinding>
</bindings>
<services>
<service name="FileService.FileService" behaviorConfiguration="FileServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9090/FileService"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="FileService.IFileService" bindingConfiguration="FileServiceBinding"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="FileServiceBehavior">
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>

新建客户端,进行服务的测试。

namespace TestClient
{
class Program
{
static void Main(string[] args)
{
FileServiceClient fileServiceClient = new FileServiceClient(); string filepath = @"F:\V2.4ShareFolder\EPHMDY1031975917464876001-7131898.zip"; using (Stream stream = new FileStream(filepath, FileMode.Open))
{
fileServiceClient.UpLoadFile("222.zip", stream);
} Console.ReadKey();
}
}
}

最新文章

  1. 通过lucene的StandardAnalyzer分析器来了解分词
  2. OpenCV播放视频带滚动条(3)
  3. 【ASP.NET MVC 】让@Ajax.ActionLink获取的数据不进Cache
  4. 设计模式:组合模式(Composite)
  5. TermServDevices错误
  6. 理论与实践中的 C# 内存模型,第 2 部分
  7. 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)
  8. mac下SSH很快被断开
  9. TCP Linger的坑
  10. 从客户端检测到有潜在危险的Request.Form值
  11. 【Nginx】磁盘文件写入飞地发
  12. 关于package,import,和“找不到可以加载的主类”报错之间的关系
  13. H+ 显示并激活menuTab 根据tabName
  14. C# 网络通信功能 同步数据交互开发
  15. ChIP-seq motif ROC 相关资料
  16. Testng测试报告
  17. 23.纯 CSS 创作一个菜单反色填充特效
  18. python input选择
  19. HTML5和CSS基础
  20. PHP-FPM高负载的解决办法

热门文章

  1. SaltStack error: No module named &#39;salt&#39;
  2. 流程控制之--if。
  3. VXLAN简介(摘抄)
  4. 成都Uber优步司机奖励政策(2月19日)
  5. java 字符串与整型相互转换
  6. (转)Ruby On Rails 推荐 Gem 列表
  7. solr 常见的问题整理 -费元星
  8. 「LeetCode」0003-Add Two Numbers(Typescript)
  9. eclipse注释快捷键
  10. 安卓客户端浏览器ajax注意