Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF call a function without waiting for it to finish its job?

In the client side every time I call a function from my WCF service, it waits until the function is complete at the WCF Service, like calling a local function. So I added an extra function for each one that starts the intended function in a new thread, so that almost all my function are like this:

EX:
I call a function from CLIENT side client.ReceiveFile() :

private void SendFile(Socket socket, Job job, DoWorkEventArgs e)
    {
        UpdateInfo(job.Name, job.Icon, job.Size);
        client.ReceiveFile((_File)job.Argument, bufferSize);
        SizeAll = job.Size;
        UpdateCurrFile(((_File)job.Argument).Path, "1");
        SendX(socket, ((_File)job.Argument).Path, e);
    }

In the the server I have to do it this way:

 public void ReceiveFile(_File file, int bufferSize)
    {
        System.Threading.Thread th = new System.Threading.Thread(unused => ReceiveFileTh(client, file.Name, file.Size, bufferSize));
        th.Start();
    }
    private void ReceiveFileTh(Socket client, string destPath, long size, int bufferSize)
    {
        try
        {
            ReceiveX(client, destPath, size, bufferSize);
        }
        finally
        {
            CloseClient();
        }
    }

The point is, when I sendFile from client, it tells the service to start receiving, but if I didn't start a new thread, the client will wait for the service to receive the file, and will not send the file data.

So does WCF support a way that doesn't make the client wait for the service function to be done! or I will have to use the above method?

like image 299
Murhaf Sousli Avatar asked Apr 26 '12 05:04

Murhaf Sousli


2 Answers

what you're looking for is [OperationContract(IsOneWay = true)] for more details see http://msdn.microsoft.com/en-us/magazine/cc163537.aspx

like image 170
nathan gonzalez Avatar answered Nov 09 '22 00:11

nathan gonzalez


The .NET client code-generation tools have built in support for this-- you shouldn't have to manually generate asynchronous versions of every method.

If you're using Add Service Reference in Visual Studio, check in the dialog box near the top: there's a checkbox that says Generate Asynchronous Operations. If you're using svcutil.exe, then use the parameter /async to generate the asynchronous client methods.

like image 35
McGarnagle Avatar answered Nov 09 '22 01:11

McGarnagle