WCF Stream实际应用功能体验

开发 开发工具
我们今天将会通过这篇文章为大家详细讲解一下有关WCF Stream在实现大文件上传方面的相关操作步骤,让大家充分掌握这方面的应用技术。

WCF中的Stream操作有很多使用方法,其中有一种比较常用的就是我们今天为大家介绍的关于实现上传大文件的操作方法。在这里我们就会通过这篇文章为大家详细介绍一下相关的操作方法。

WCF Stream操作步骤之Test.ASPX.C

  1. protected void Button3_Click(object sender, EventArgs e)  
  2. {  
  3. FileData file = new FileData();  
  4. file.filename = FileUpload1.FileName;  
  5. file.data = new FileStream(FileUpload1.PostedFile.
    FileName, FileMode.Open);  
  6. GetDataServiceClient c = new GetDataServiceClient();  
  7. c.UploadFile(file.filename, file.data);  
  8. Response.Write("文件传输成功!");  
  9. c.Close();  

WCF Stream操作步骤之Contract

  1. [ServiceContract]  
  2. public interface IGetDataService  
  3. {  
  4. [OperationContract]  
  5. void UploadFile(FileData file);  
  6. }  
  7. [MessageContract]  
  8. public class FileData  
  9. {  
  10. [MessageHeader]  
  11. public string filename;  
  12. [MessageBodyMember]  
  13. public Stream data;  

WCF Stream操作步骤之ServiceLib

  1. public class GetDataService : IGetDataService  
  2. {  
  3. public void UploadFile(FileData file)  
  4. {  
  5. FileStream fs = new FileStream("Files\\"+file.filename, 
    FileMode.OpenOrCreate);  
  6. try  
  7. {  
  8. BinaryReader reader = new BinaryReader(file.data);  
  9. byte[] buffer;  
  10. BinaryWriter writer = new BinaryWriter(fs);  
  11. long offset = fs.Length;  
  12. writer.Seek((int)offset, SeekOrigin.Begin);  
  13. do  
  14. {  
  15. buffer = reader.ReadBytes(1024);  
  16. writer.Write(buffer);  
  17. } while (buffer.Length > 0);  
  18. }  
  19. catch(Exception e)  
  20. {  
  21. }  
  22. finally  
  23. {  
  24. fs.Close();  
  25. file.data.Close();  
  26. }  
  27. }  

WCF Stream操作步骤之App.config

  1. < ?xml version="1.0" encoding="utf-8" ?> 
  2. < configuration> 
  3. < system.serviceModel> 
  4. < services> 
  5. < !--name - 提供服务的类名--> 
  6. < !--behaviorConfiguration - 指定相关的行为配置--> 
  7. < service name="ServiceLib.GetDataService" 
    behaviorConfiguration="BindingBehavior"> 
  8. < !--address - 服务地址--> 
  9. < !--binding - 通信方式--> 
  10. < !--contract - 服务契约--> 
  11. < !--< endpoint binding="basicHttpBinding" contract=
    "WCF.ServiceLib.Binding.IHello" address="Hello" />--> 
  12. < !--元数据交换的endpoint--> 
  13. < !--注:address是mex,它会和host/baseAddresses节点中的baseAddress做拼接,
    即提供元数据交换的地址为:http://localhost:12345/Binding/mex--
    > 
  14. < endpoint binding="basicHttpBinding" bindingConfiguration =
    "DocumentExplorerServiceBinding" contract="Contract.IGetDataService" 
    address="mex" /> 
  15. < host> 
  16. < baseAddresses> 
  17. < add baseAddress="http://localhost:8008/"/> 
  18. < /baseAddresses> 
  19. < /host> 
  20. < /service> 
  21. < /services> 
  22. < behaviors> 
  23. < serviceBehaviors> 
  24. < behavior name="BindingBehavior"> 
  25. < !--httpGetEnabled - 使用get方式提供服务--> 
  26. < serviceMetadata httpGetEnabled="true" /> 
  27. < /behavior> 
  28. < /serviceBehaviors> 
  29. < /behaviors> 
  30. < bindings> 
  31. < basicHttpBinding> 
  32. < binding name="DocumentExplorerServiceBinding" 
  33. sendTimeout="00:10:00" 
  34. transferMode="Streamed" 
  35. maxReceivedMessageSize="9223372036854775807"> 
  36. < /binding> 
  37. < /basicHttpBinding> 
  38. < /bindings> 
  39. < /system.serviceModel> 
  40. < /configuration> 

WCF Stream操作步骤之web.config

  1. < system.serviceModel> 
  2. < bindings> 
  3. < basicHttpBinding> 
  4. < binding name="BasicHttpBinding_IGetDataService" closeTimeout="00:01:00" 
  5. openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" 
  6. allowCookies="false" bypassProxyOnLocal="false" 
    hostNameComparisonMode="StrongWildcard" 
  7. maxBufferSize="65536" maxBufferPoolSize="524288" 
    maxReceivedMessageSize="65536" 
  8. transferMode="Streamed" 
  9. useDefaultWebProxy="true"> 
  10. < readerQuotas maxDepth="32" maxStringContentLength="8192" 
    maxArrayLength="16384" 
  11. maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
  12. < security mode="None"> 
  13. < transport clientCredentialType="None" proxyCredentialType="None" 
  14. realm="" /> 
  15. < message clientCredentialType="UserName" algorithmSuite="Default" /> 
  16. < /security> 
  17. < /binding> 
  18. < /basicHttpBinding> 
  19. < /bindings> 
  20. < client> 
  21. < endpoint address="http://192.168.0.19:8008/mex" 
    binding="basicHttpBinding" 
  22. bindingConfiguration="BasicHttpBinding_IGetDataService" 
    contract="IGetDataService" 
  23. name="BasicHttpBinding_IGetDataService" /> 
  24. < /client> 
  25. < /system.serviceModel> 

以上就是我们为大家详细介绍的有关WCF Stream的操作方法。

【编辑推荐】

  1. WCF行为扩展正确内容应用技巧分享
  2. ASP.NET Ajax调用WCF服务正确实现方法浅谈
  3. WCF全局错误捕获正确内容解析
  4. WCF传输安全机制相关内容详解
  5. WCF创建WebService正确操作步骤详解
责任编辑:曹凯 来源: CSDN
相关推荐

2010-02-25 16:12:23

WCF IDispos

2010-05-31 15:49:29

MySQL临时表

2010-03-01 13:06:49

WCF继承

2010-02-22 10:42:12

WCF Stream

2010-03-02 17:35:20

WCF服务加载

2009-12-21 14:49:27

2010-02-23 10:25:29

2010-02-24 14:05:08

WCF openati

2010-02-22 13:28:05

WCF异步调用

2013-09-02 16:04:20

Windows

2009-12-21 14:58:57

WCF用户密码认证

2010-03-01 17:52:03

WCF选择绑定

2010-03-01 10:45:59

WCF集合类

2010-01-26 10:38:56

Android消息传递

2010-02-25 17:22:39

WCF服务行为

2017-11-07 22:19:55

iOS 苹果App

2020-12-18 13:00:31

Xedit文本编辑器Linux

2009-11-05 15:00:26

WCF Stream

2010-05-24 09:24:15

MySQL 备份

2009-11-06 16:35:56

WCF Stream对
点赞
收藏

51CTO技术栈公众号