在下面的代码中,我想做的是允许我的客户端在服务器上调用 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)
{
}
}
}
}