在下面的代码中,我想要做的是允许我的客户端在服务器上调用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)
{
}
}
}
}
答案 0 :(得分: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);
}
答案 1 :(得分: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";
}