0

I use WCF Service for Copying files in the local environment. The WPF GUI is used to call methods from WCF Service using some buttons. Type of communication I use is Named Pipes.

I have problem with "getting" any progress of copying a file and passing it to GUI to fill up the progress bar.

Is there any chance to handle that using named pipes when it's one-way communication?

5
  • 1
    If the sender knows how big the file is, the first thing it could write to the pipe could be a 64-bit length, and then the receiver could read that before reading the actual file bytes - and it could subsequently work out progress from the total number of bytes received. Not sure whether the context in which you're using the pipe would allow you to do that, though. Commented Apr 5 at 9:52
  • It seem a bit weird to use named pipes for passing files within the same machine. Is there any reason to not just pass file paths to the UI?
    – JonasH
    Commented Apr 5 at 12:59
  • I want pass a progress of copying file not entire file. I'm passing only file name to UI with some key that I know which one needs to be copied, then I'm handling a button with a command.
    – kalview
    Commented Apr 6 at 13:04
  • First of all, you should need the client to access the server to achieve this effect, right? So you mentioned later that you are using one-way communication, and I don't think this can be achieved. You need to use duplex communication, turning the server into a client and the client into a server to achieve your purpose. In the meantime, can you provide your code? If you have a code review problem, I think it can help you better.
    – QI You
    Commented Apr 8 at 7:28
  • The purpose is to host WCF locally using Windows Service. Then in GUI do some copy actions, "get" progress of copy, display it to user. All of this in 1 PC, not using server. I've prepared it as it was in many tutorials/docs. In both App.config(WCF & Windows Service) I setup <bindings> with net pipe endpoint and it works. In WCF I've created a MyService.cs and IMyService.cs - it allows me to call WCF methods from GUI(in GUI I prepared class ServiceClient with ChannelFactory) and methods. If I'm wrong please correct me if you know how to handle that type of communication...
    – kalview
    Commented Apr 8 at 8:25

1 Answer 1

0

First of all, it is not difficult to copy the file through WCF.

The problem is to get the percentage, and my method can be viewed in real-time in the WCF console.

Server:

Service1:

public void CopyFile(string source, string target, ref float percent, int bufferSize = 1024 * 1024 * 10)
{
    byte[] array = new byte[bufferSize]; 
    using (FileStream fsRead = File.Open(source, FileMode.Open, FileAccess.Read))
    {
        using (FileStream fsWrite = File.Open(target, FileMode.Create, FileAccess.Write))
        {
            while (fsRead.Position < fsRead.Length)
            {
               
                int length = fsRead.Read(array, 0, array.Length);
                
                fsWrite.Write(array, 0, length);
              
                percent = (float)fsRead.Position / fsRead.Length;
                Console.WriteLine(percent.ToString());
            }
        }
    }
    
}

IService1:

 [OperationContract]
 void CopyFile(string source, string target, ref float percent, int bufferSize = 1024 * 1024 * 10);

Client:

Service1Client client=new Service1Client();
   float percent = 0;
    string source = @"xxxx";
    string target = @"xxxx";
    percent=client.CopyFile(source, target,ref percent,1024*1024*10);

My suggestion would be to put this code directly in your WPF project. This will allow you to replace the Console.Write line with the display data.

Not the answer you're looking for? Browse other questions tagged or ask your own question.