這是我的WCF服務。我想通知 多個訂戶並進行一些更新並異步執行。我怎麼做?WCF異步通知用戶
// Callback contract
public interface IMyServiceCallback
{
[OperationContract]
void Notify(String sData);
}
public class MyService:IMyService
{
List<IMyServiceCallback> _subscribers = new List<IMyServiceCallback>();
// This function is used to subscribe to service.
public bool Subscribe()
{
try
{
IMyServiceCallback callback = OperationContext.Current.GetCallbackChannel<IMyServiceCallback>();
if (!_subscribers.Contains(callback))
_subscribers.Add(callback);
return true;
}
catch
{
return false;
}
}
// this function is used to notify all the subsribers
// I want ForEach to be asynchronous.
public void OnGetMsg(string sData)
{
_subscribers.ForEach(
callback =>
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
callback.Notify(sData); //THIS OPERATION
}
else
{
_subscribers.Remove(callback);
}
});
}
}
"MSDN: WCF Publisher/Subscriber Client crashing" is strongly related to my problem.
我按照Mini Webcast在創建該服務。
另外,我是誰認爲WCF是在痛苦中唯一一個頸部? (你知道我的意思。) – 2011-04-25 08:33:51
你的代碼在哪裏?異常或邏輯(算法)問題? – 2011-04-25 08:47:54
如果其中一個用戶死了,那麼'callback.Notify()'會出現超時異常。由於向其他用戶發送的通知延遲了。 – 2011-04-25 08:50:38