WCF Stream實際應(yīng)用功能體驗
作者:佚名
我們今天將會通過這篇文章為大家詳細講解一下有關(guān)WCF Stream在實現(xiàn)大文件上傳方面的相關(guān)操作步驟,讓大家充分掌握這方面的應(yīng)用技術(shù)。
WCF中的Stream操作有很多使用方法,其中有一種比較常用的就是我們今天為大家介紹的關(guān)于實現(xiàn)上傳大文件的操作方法。在這里我們就會通過這篇文章為大家詳細介紹一下相關(guān)的操作方法。
WCF Stream操作步驟之Test.ASPX.C
- protected void Button3_Click(object sender, EventArgs e)
- {
- FileData file = new FileData();
- file.filename = FileUpload1.FileName;
- file.data = new FileStream(FileUpload1.PostedFile.
FileName, FileMode.Open);- GetDataServiceClient c = new GetDataServiceClient();
- c.UploadFile(file.filename, file.data);
- Response.Write("文件傳輸成功!");
- c.Close();
- }
WCF Stream操作步驟之Contract
- [ServiceContract]
- public interface IGetDataService
- {
- [OperationContract]
- void UploadFile(FileData file);
- }
- [MessageContract]
- public class FileData
- {
- [MessageHeader]
- public string filename;
- [MessageBodyMember]
- public Stream data;
- }
WCF Stream操作步驟之ServiceLib
- public class GetDataService : IGetDataService
- {
- public void UploadFile(FileData file)
- {
- FileStream fs = new FileStream("Files\\"+file.filename,
FileMode.OpenOrCreate);- try
- {
- BinaryReader reader = new BinaryReader(file.data);
- byte[] buffer;
- BinaryWriter writer = new BinaryWriter(fs);
- long offset = fs.Length;
- writer.Seek((int)offset, SeekOrigin.Begin);
- do
- {
- buffer = reader.ReadBytes(1024);
- writer.Write(buffer);
- } while (buffer.Length > 0);
- }
- catch(Exception e)
- {
- }
- finally
- {
- fs.Close();
- file.data.Close();
- }
- }
- }
WCF Stream操作步驟之App.config
- < ?xml version="1.0" encoding="utf-8" ?>
- < configuration>
- < system.serviceModel>
- < services>
- < !--name - 提供服務(wù)的類名-->
- < !--behaviorConfiguration - 指定相關(guān)的行為配置-->
- < service name="ServiceLib.GetDataService"
behaviorConfiguration="BindingBehavior">- < !--address - 服務(wù)地址-->
- < !--binding - 通信方式-->
- < !--contract - 服務(wù)契約-->
- < !--< endpoint binding="basicHttpBinding" contract=
"WCF.ServiceLib.Binding.IHello" address="Hello" />-->- < !--元數(shù)據(jù)交換的endpoint-->
- < !--注:address是mex,它會和host/baseAddresses節(jié)點中的baseAddress做拼接,
即提供元數(shù)據(jù)交換的地址為:http://localhost:12345/Binding/mex-->- < endpoint binding="basicHttpBinding" bindingConfiguration =
"DocumentExplorerServiceBinding" contract="Contract.IGetDataService"
address="mex" />- < host>
- < baseAddresses>
- < add baseAddress="http://localhost:8008/"/>
- < /baseAddresses>
- < /host>
- < /service>
- < /services>
- < behaviors>
- < serviceBehaviors>
- < behavior name="BindingBehavior">
- < !--httpGetEnabled - 使用get方式提供服務(wù)-->
- < serviceMetadata httpGetEnabled="true" />
- < /behavior>
- < /serviceBehaviors>
- < /behaviors>
- < bindings>
- < basicHttpBinding>
- < binding name="DocumentExplorerServiceBinding"
- sendTimeout="00:10:00"
- transferMode="Streamed"
- maxReceivedMessageSize="9223372036854775807">
- < /binding>
- < /basicHttpBinding>
- < /bindings>
- < /system.serviceModel>
- < /configuration>
WCF Stream操作步驟之web.config
- < system.serviceModel>
- < bindings>
- < basicHttpBinding>
- < binding name="BasicHttpBinding_IGetDataService" closeTimeout="00:01:00"
- openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
- allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"- maxBufferSize="65536" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"- transferMode="Streamed"
- useDefaultWebProxy="true">
- < readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"- maxBytesPerRead="4096" maxNameTableCharCount="16384" />
- < security mode="None">
- < transport clientCredentialType="None" proxyCredentialType="None"
- realm="" />
- < message clientCredentialType="UserName" algorithmSuite="Default" />
- < /security>
- < /binding>
- < /basicHttpBinding>
- < /bindings>
- < client>
- < endpoint address="http://192.168.0.19:8008/mex"
binding="basicHttpBinding"- bindingConfiguration="BasicHttpBinding_IGetDataService"
contract="IGetDataService"- name="BasicHttpBinding_IGetDataService" />
- < /client>
- < /system.serviceModel>
以上就是我們?yōu)榇蠹以敿毥榻B的有關(guān)WCF Stream的操作方法。
【編輯推薦】
責(zé)任編輯:曹凱
來源:
CSDN