简体   繁体   English

WCF调用函数而不等待它完成它的工作?

[英]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. 在每次从WCF服务调用函数时,在客户端,它等待直到WCF服务上的函数完成,就像调用本地函数一样。 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: EX:
I call a function from CLIENT side client.ReceiveFile() : 我从CLIENT端调用一个函数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. 关键是,当我从客户端sendFile时,它告诉服务开始接收,但如果我没有启动新线程,客户端将等待服务接收文件,并且不会发送文件数据。

So does WCF support a way that doesn't make the client wait for the service function to be done! 因此,WCF支持一种不会使客户端等待服务功能完成的方式! or I will have to use the above method? 或者我将不得不使用上述方法?

您正在寻找的是[OperationContract(IsOneWay = true)]有关详细信息,请参阅http://msdn.microsoft.com/en-us/magazine/cc163537.aspx

The .NET client code-generation tools have built in support for this-- you shouldn't have to manually generate asynchronous versions of every method. .NET客户端代码生成工具内置了对此的支持 - 您不必手动生成每个方法的异步版本。

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 . 如果您在Visual Studio中使用“ 添加服务引用” ,请检查顶部附近的对话框:有一个“ 生成异步操作”复选框。 If you're using svcutil.exe , then use the parameter /async to generate the asynchronous client methods. 如果您使用的是svcutil.exe ,则使用参数/ async生成异步客户端方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM