0

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

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

这可能吗?

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)
            {

            }    
        }
    }
}
4

2 回答 2

2

当您return在一个函数中时,它将停在该行。您在返回之后放置的任何内容都不会执行。实际上,您的编译器应该警告您有关无法访问的代码。

编辑:

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

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);

        }
于 2013-03-21T20:18:46.610 回答
1

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

如果出现错误,WCF 内置了自动异常处理,因此您可以只Throw处理异常,不需要返回它......然后可以在客户端捕获它。并且将有更多的信息,而不仅仅是“错误”。

使用 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";
   }
于 2013-03-21T20:22:14.433 回答