콜백을 사용하여 WCF 서비스를받을 수있었습니다. 클라이언트는 단순히 서비스에 "가입"하고 서비스는 타이머를 시작합니다. 이 타이머는 콜백 함수를 호출 할시기를 결정합니다.WCF 서비스 구독 취소 방법
지금 내 질문은 클라이언트를 단순히 폐쇄 클라이언트 CommunicationException
발생합니다 때문에 구독 취소하는 방법입니다.
내 구현은 Unsubscribe()
이며 올바른 타이머를 사용하지 않도록 설정 했습니까? 아니면 추가 단계를 수행해야합니까? 귀하의 경우에는
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, InstanceContextMode = InstanceContextMode.PerSession)]
internal class HostFunctions : IHostFunctions
{
private static ILog _log = LogManager.GetLogger(typeof(HostFunctions));
private IHostFunctionsCallback _callback;
private Timer _timer;
#region Implementation of IHostFunctions
public void Subscribe()
{
_callback = OperationContext.Current.GetCallbackChannel<IHostFunctionsCallback>();
_timer = new Timer(1000);
_timer.Elapsed += OnTimerElapsed;
_timer.Enabled = true;
}
public void Unsubscribe()
{
_timer.Enabled = false;
}
private void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
if (_callback == null) return;
try
{
_callback.OnCallback();
}
catch (CommunicationException comEx)
{
// Log: Client was closed or has crashed
_timer.Enabled = false;
}
}
#endregion
}
그리고 클라이언트가 충돌하거나 프로세스가 종료되면 어떻게됩니까? 이 경우 클라이언트는 연결을 닫을 수 없습니다. –
그러면 클라이언트 채널이 결국 시간 초과되어 닫힙니다. –