以下のコードで私がやろうとしているのは、クライアントがサーバー上で 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)
{
}
}
}
}