여기가 WCF가 까다로워집니다. 서버가 콜백 메소드를 호출하면 클라이언트 프록시가 닫히기 전에 해당 호출을 완료해야합니다. 이것은 클라이언트와 왕복으로 왕복하는 것을 의미합니다. Microsoft 예제는 Console.ReadLine()을 통해 클라이언트 측에서 사용자 입력을 요청하여 닫기를 트리거합니다. 비동기 콜백을 사용하여 닫기를 트리거하려면 광고 소재를 가져와야합니다. 예를 들어, 새 스레드에 닫기를 대기시킨 다음 실행을 지연시킬 수 있습니다. 이렇게하면 콜백이 서버로 돌아가서 채널을 닫을 수 있습니다.
콜백이 같은 추가 : 다음
var currentChannel = OperationContext.Current.Channel;
ThreadPool.QueueUserWorkItem(o => CloseChannel(currentChannel));
이를 CloseChannel을 위해 :
private static void CloseChannel<T>(T channel)
{
var clientChannel = (IClientChannel)channel;
var success = false;
try
{
//sleep before close so the main thread has a chance to catch up
Thread.Sleep(10000);
clientChannel.Close();
success = true;
}
catch (CommunicationException ce)
{
clientChannel.Abort();
}
catch (TimeoutException te)
{
clientChannel.Abort();
}
finally
{
if (!success)
clientChannel.Abort();
}
}
을 나는 차단'Console.ReadLine()'전'클라이언트가 있기 때문에 샘플이 작동 추측 .Close()'. 그것이 없으면 통화가 완료되기 전에 클라이언트를 종료했을 가능성이 높습니다. –