2017-03-08 2 views
1

동기식 스트리밍 알림을 작동 한 후 비동기 BeginSubscribeToStreamingNotifications()을 발견했습니다.EWS BeginSubscribeToStreamingNotifications 사용

매우 제한된 문서를 사용하여이 작업을 조금만 수행 한 후에는 포기했습니다. 나는 이걸 작동시키지 못한다.

환경
여기에 관련 코드가 있습니다. 서비스를 시작하고 연결하여 BeginSubscribeToStreamingNotifications() 메소드를 호출하고 있습니다.

public static void InitSubscriptions() 
{ 
    var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 

    service.Credentials = new NetworkCredential("some", "login", "here"); 
    service.Url = new Uri("https://some.exchange/server"); 
    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]"); 

    // Initiating the subscription 
    var subscription = service.BeginSubscribeToStreamingNotifications(new AsyncCallback(Callback), null, new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail); 
} 

private static void Callback(IAsyncResult result) 
{ 
    // ??? 
} 

여러분도 알다시피, 콜백 메소드에서 무엇을해야할지 모르겠습니다.

문제
실제로이 방법을 사용하여 알림을 구독하는 방법을 모르겠어요. 나는 그것을 동 기적으로 작동 시켰지만 가능하다면 비동기 적으로 작동하게하고 싶다.

다음은 의도 한대로 동작하는 동기 예제이다.

public static void InitSubscriptions() 
{ 
    var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 

    service.Credentials = new NetworkCredential("some", "login", "here"); 
    service.Url = new Uri("https://some.exchange/server"); 
    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]"); 

    // Initiating the subscription 
    var subscription = service.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail); 
    var connection = new StreamingSubscriptionConnection(service, 30); 

    connection.AddSubscription(subscription); 

    // Delegate handlers 
    connection.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(SomeMethod); 
    connection.OnSubscriptionError += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(SomeErrorMethod); 
    connection.OnDisconnect += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(SomeDisconnectMethod); 
} 

내 질문은 : 동 기적으로 스트리밍 구독을 비동기 방식으로 스트리밍 구독으로 변환하려면 어떻게해야합니까?

답변

0

콜백에서 EndSubscribeToStreamingNotifications으로 전화를 걸어 연결 및 이벤트를 설정해야합니다.

public static void InitSubscriptions() 
{ 
    var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
    // ... 
    service.BeginSubscribeToStreamingNotifications(
     new AsyncCallback(Callback), 
     service, // Pass the service as state. 
     new FolderId[] { WellKnownFolderName.Inbox },  
     EventType.NewMail);  
} 

private static void Callback(IAsyncResult result) 
{ 
    // Retrieve the service from the passed state. 
    var service = result.AsyncState as ExchangeService; 
    var subscription = service.EndSubscribeToStreamingNotifications(result); 

    var connection = new StreamingSubscriptionConnection(service, 30); 
    connection.AddSubscription(subscription); 

    // Delegate handlers 
    connection.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(SomeMethod); 
    connection.OnSubscriptionError += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(SomeErrorMethod); 
    connection.OnDisconnect += new StreamingSubscriptionConnection.SubscriptionErrorDelegate  
} 

서비스를 상태로 콜백 메소드에 전달하는 방법에 유의하십시오.

나는 코드를 테스트하지 않았고 시작/종료 방법을 직접 사용하지 않았지만 올바른 방향으로 얻을 수 있기를 바랍니다.

작동 시키려면 TaskFactory.FromAsync을 사용하여 시작/종료 패턴을 Task 기반 패턴으로 변환하는 방법을 알아야 할 수 있습니다.