返回值给客户端,然后进行回调

本文关键字:回调 然后 客户端 返回值 | 更新日期: 2023-09-27 18:05:06

在下面的代码中,我想做的是允许我的客户端调用服务器上的WCF方法,返回成功,然后对当前进度即完成百分比进行回调。客户端可以在10%后断开连接,该方法将继续。

在下面的代码中——返回"success"行导致函数退出。我想要返回的原因是,这样客户端就会阻塞,直到他知道服务完成了一些重要的处理。

这可能吗?

namespace WCFService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class WCFJobsLibrary : IWCFJobsLibrary
    {
        public String ChatToServer(string texttoServer) // send some text to the server
        {
            Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", 1);
            try
            {
                // Some extemely important prechecks .....
                //........
                return "success";
                // Dont care now if client disconnects but lets give them some updates as progress happens if they are still connected

                IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>();
                // Some processing .....
                callbackmethod("20% complete", callback);
                // Some processing .....
                callbackmethod("40% complete", callback);
                // Some processing .....
                callbackmethod("60% complete", callback);
                // Some processing .....
                callbackmethod("80% complete", callback);
                // Some processing .....
                callbackmethod("100% complete", callback);
            }
            catch (Exception ex)
            {
                return "error";
            }
        }
        public void callbackmethod(string text, IMyContractCallBack somecallback)
        {
            try
            {
                somecallback.callbacktoServer(text);
            }    
            catch (Exception)
            {
            }    
        }
    }
}

返回值给客户端,然后进行回调

当您在函数中使用return时,它将在该行停止。返回之后的任何内容都不会执行。事实上,你的编译器应该警告你不可访问的代码。

编辑:

我建议在调用return之前启动一个线程,它将运行继续工作并发送更新调用。

public String ChatToServer(string texttoServer) // send some text to the server
        {
            try
            {
                Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", 1);
                // Some extemely important prechecks .....
                System.Threading.Thread thread = new System.Threading.Thread(DoWork);
                thread.Start();
                //........
                return "success";
                // Dont care now if client disconnects but lets give them some updates as progress happens if they are still connected
            }
            catch (Exception ex)
            {
                return "error";
            }
        }
        void DoWork()
        {
            IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>();
            // Some processing .....
            callbackmethod("20% complete", callback);
            // Some processing .....
            callbackmethod("40% complete", callback);
            // Some processing .....
            callbackmethod("60% complete", callback);
            // Some processing .....
            callbackmethod("80% complete", callback);
            // Some processing .....
            callbackmethod("100% complete", callback);
        }

你可以使回调异步,它们将触发,方法将返回,但是你必须在回调调用之后放置return行。

在错误的情况下,WCF有自动异常处理内置,所以你可以只是Throw的异常,你不需要返回它…然后它可以在客户端被捕获。并且会有比"error"更多的信息

使用TPL的示例:(使用System.Threading.Tasks命名空间,.net 4+)

    string yourMethod() 
    {
        // Logging
        try 
        {   
            // prechecks 
        } 
        catch (Exception ex)
        {
            return "failed" // ok as you have now if no more information is needed
            // throw; // Can also throw exception which WCF client on other end can catch
        }     
        Task.Factory.StartNew(() => {
            IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>();
            // Some processing .....
            callbackmethod("20% complete", callback);
            // Some processing .....
            callbackmethod("40% complete", callback);
            // Some processing .....
            callbackmethod("60% complete", callback);
            // Some processing .....
            callbackmethod("80% complete", callback);
            // Some processing .....
            callbackmethod("100% complete", callback);
        });
        return "success";
   }