C # cross-service large file replication,

Source: Internet
Author: User

C # cross-service large file replication,

Cross-service large file replication must be the same as local large file replication and be transferred multiple times, otherwise the memory will not be able to afford it, the following describes how to achieve cross-service replication of large files ······

First, you must establish a WCF Service and the corresponding client to test the service. For this method, refer to my blog:

Create and call a WCF Service: http://www.cnblogs.com/sxw117886/p/5629481.html

After reading this blog, you must have set up your own services and clients. Here is the time to witness the miracle ------ copying large files

 

------------------------------------------- Gorgeous split line ---------------------------------------------------------------------------

 

1. First, we need to write the WCF code (this code is put together with the code for creating and calling the WCF Service)

(1) Add a service corresponding interface CopyFile (Stream stream Stream): for example, the Service "WCFTestService. DbApiInfo" corresponding interface: IApiInfo (that is, the interface name mentioned above must be consistent with the above)

[ServiceContract]    public interface IApiInfo    {        [OperationContract]        [ServiceKnownType(typeof(string))]        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]        string CreateApi(Stream name);        [OperationContract]        [ServiceKnownType(typeof(string))]        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]        void CopyFile(Stream stream);    }

(2) Add the implementation class corresponding to the interface: The public void CopyFile (Stream stream) method corresponding to DbApiInfo. cs)

Public class DbApiInfo: IApiInfo {public string CreateApi (Stream name) {// name. position = 0; StreamReader reader = new StreamReader (name); string text = reader. readToEnd (); return "Your name is:" + text ;}/// <summary> // copy a large file. Each time you input a certain size of file stream, append to the expected price /// </summary> /// <param name = "stream"> </param> public void CopyFile (Stream stream) {try {string FileStr = StreamToString (stream, Encoding. UTF8); byt E [] array = Convert. FromBase64String (FileStr); // parses the file information into a byte format according to Base64 // FileMode. Append is an Append write to a large file. This is a major difference between using (FileStream fsWrite = new FileStream (@ "D: \ 360 secure Browser Download \ 4.avi", FileMode. append, FileAccess. write) {fsWrite. write (array, 0, array. count (); // append Write File} catch (System. exception ex) {}}/// <summary> /// convert the request to a string // </summary> /// <param name = "info"> </param> /// <returns> </returns> public static string StreamToString (Stream stream, encoding encoding) {string result = string. empty; try {using (StreamReader sr = new StreamReader (stream, encoding) {result = sr. readToEnd (); sr. close () ;}} catch {} return result ;}

2. Add client code

Private void button2_Click (object sender, EventArgs e) {// read the copy file stream using (FileStream fsRead = new FileStream (@ "D: \ 360 secure Browser Download \ video \ 2.avi ", fileMode. open, FileAccess. read) {byte [] buffer = new byte [1024*1024*2]; // Read 2 MB at a time. // the file may be large and should be Read cyclically, 2 M while (true) read each time {// data read each time n: the actual data size read each time int n = fsRead. read (buffer, 0, buffer. count (); // if n = 0, the read data is null and has been read to the end. if (n = 0) {break ;} string base64buffer = Convert. toBase64String (buffer); PostHttp ("http: // localhost: 8888/Service/DbApiInfo/CopyFile", base64buffer);} MessageBox. show ("copied ");}}

According to the method, each loop reads 2 mb data streams and calls the server interface to pass the data to the server. This way, the large file board can pass the data multiple times, it won't put too much pressure on memory ······

Finally, start the service and client, and let us witness the miracle:

When starting the client, we can see the interface:

 

Before clicking the "Copy large file" button, add only one 2. avi video file to the file:

Click "Copy large files". The "Copied successfully" page is displayed.

Let's take a look at the corresponding folder:

Two files appear. This is the cross-service replication method for large files ·······

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.