void InitializeAndFindAsync() { DiscoveryEndpoint discoveryEndpoint = this.discoveryEndpointProvider.GetDiscoveryEndpoint(); if (discoveryEndpoint == null) { throw FxTrace.Exception.AsError( new InvalidOperationException( SR.DiscoveryMethodImplementationReturnsNull("GetDiscoveryEndpoint", this.discoveryEndpointProvider.GetType()))); } this.discoveryClient = new DiscoveryClient(discoveryEndpoint); this.discoveryClient.FindProgressChanged += new EventHandler <FindProgressChangedEventArgs>(OnFindProgressChanged); this.discoveryClient.FindCompleted += new EventHandler <FindCompletedEventArgs>(OnFindCompleted); SynchronizationContext originalSynchronizationContext = SynchronizationContext.Current; if (originalSynchronizationContext != null) { SynchronizationContext.SetSynchronizationContext(null); if (TD.SynchronizationContextSetToNullIsEnabled()) { TD.SynchronizationContextSetToNull(); } } try { // AsyncOperation uses the SynchronizationContext set during its // initialization to Post the FindProgressed and FindProgressCompleted // events. Hence even if the async operation does not complete // synchronously, the right SynchronizationContext will be used by // AsyncOperation. this.discoveryClient.FindAsync(this.findCriteria, this); } finally { if (originalSynchronizationContext != null) { SynchronizationContext.SetSynchronizationContext(originalSynchronizationContext); if (TD.SynchronizationContextResetIsEnabled()) { TD.SynchronizationContextReset(originalSynchronizationContext.GetType().ToString()); } } } if (TD.FindInitiatedInDiscoveryClientChannelIsEnabled()) { TD.FindInitiatedInDiscoveryClientChannel(); } }