[英]WCF: calling self-hosted service from client callback handler
問題:在WCF客戶端中收到死鎖異常消息。
場景:
服務調用客戶端回調(此調用是完全獨立的,並由服務器上的某些條件啟動)。
在客戶端回調函數內部時,客戶端會在服務中調用一個函數,此時將引發死鎖異常:
該操作將陷入僵局,因為在當前消息完成處理之前,無法接收答復。 如果要允許亂序消息處理,請在CallbackBehaviorAttribute上指定Reentrant或Multiple的ConcurrencyMode。
我盡力簡化了代碼。 我確實讀過這篇文章,但仍然找不到問題所在: http : //msdn.microsoft.com/zh-cn/library/cc294424.aspx我將不勝感激任何建議。
[ServiceContract(Namespace = "http://abc.com/Core", SessionMode = SessionMode.Required, CallbackContract = typeof(ISvcCallback))]
public interface ISvc
{
// One way only - does not wait until operation completes and returns
// Can initiate session
[OperationContract(IsOneWay = true)]
void Initialize(string appId);
[OperationContract(IsInitiating = false)]
Account GetCurrentAccount();
}
public interface ISvcCallback
{
/// <summary>
/// Report status of the account
/// </summary>
/// <param name="acct"></param>
[OperationContract(IsOneWay=true)]
void AccountStatus(Account acct);
}
Service Implementation
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false)]
public class Svc : ISvc
{
public Account GetCurrentAccount()
{
SipAccount sipAcct = null;
try
{
Account acct = m_MyBusinessObject.GetCurrentAccount();
}
catch (Exception ex)
{
}
return Acct;
}
}
}
public class CallbackHandler : WcfSipItfService.IWinSipItfCallback
{
public void AccountStatus(Account Acct)
{
try
{
// display accout status in UI by delegate-wrapped event
// delegate and event declarations are somewhere else
// and work fine...
if (DisplayAccountStatusEvent != null)
DisplayAccountStatusEvent(Acct);
}
catch (Exception ex)
{
....
}
}
private void OnDisplayAccountStatusEvent(Account acct)
{
// call service function results in deadlock
Account acct = GetCurrentAccount();
}
}
服務是Duplex-使用WSDualHttpBinding。
死鎖似乎是由於您在處理來自先前呼叫的回叫時進行了新的出局呼叫。
錯誤消息指出,您可以通過“在CallbackBehaviorAttribute上指定Reentrant或Multiple的ConcurrencyMode”來解決此問題。
編輯
我錯過了屏幕上的代碼。 需要檢查的幾件事:
你的客戶是什么? 它是UI客戶端嗎? 如果是這樣,則需要向實現回調協定的客戶端添加callbackBehavior UseSynchronizationContext = false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.