public FindResponse Find(FindCriteria criteria) { string contractName = criteria.ContractTypeNames[0].Name; string contractNamespace = criteria.ContractTypeNames[0].Namespace; FindResponse response = DiscoveryHelper.CreateFindResponse(); ManualResetEvent handle = new ManualResetEvent(false); Action <Uri, Uri[]> addEndpoint = (address, scopes) => { EndpointDiscoveryMetadata metadata = new EndpointDiscoveryMetadata(); metadata.Address = new EndpointAddress(address); if (scopes != null) { foreach (Uri scope in scopes) { metadata.Scopes.Add(scope); } } response.Endpoints.Add(metadata); if (response.Endpoints.Count >= criteria.MaxResults) { handle.Set(); } }; DiscoveryResponseCallback callback = new DiscoveryResponseCallback(contractName, contractNamespace, addEndpoint); ServiceHost host = new ServiceHost(callback); host.AddServiceEndpoint(typeof(IServiceBusDiscoveryCallback), Endpoint.Binding, ResponseAddress.AbsoluteUri); host.Description.Endpoints[0].Behaviors.Add(new ServiceRegistrySettings(DiscoveryType.Public)); TransportClientEndpointBehavior credentials = Endpoint.Behaviors.Find <TransportClientEndpointBehavior>(); Debug.Assert(credentials != null); host.Description.Endpoints[0].Behaviors.Add(credentials); host.Open(); try { DiscoveryRequest(criteria.ContractTypeNames[0].Name, criteria.ContractTypeNames[0].Namespace, criteria.Scopes.ToArray(), ResponseAddress); bool found = handle.WaitOne(criteria.Duration); if (found == false) { Trace.WriteLine("Could not find endpoints within specified discovery criteria duration"); } } catch {} finally { try { host.Abort(); } catch (ProtocolException) {} } return(response); }
public FindResponse Find(FindCriteria criteria) { string contractName = criteria.ContractTypeNames[0].Name; string contractNamespace = criteria.ContractTypeNames[0].Namespace; FindResponse response = DiscoveryHelper.CreateFindResponse(); ManualResetEvent handle = new ManualResetEvent(false); Action <Uri, Uri[]> addEndpoint = (address, scopes) => { if (handle.WaitOne(0)) { return; } EndpointDiscoveryMetadata metadata = new EndpointDiscoveryMetadata(); metadata.Address = new EndpointAddress(address); if (scopes != null) { foreach (Uri scope in scopes) { metadata.Scopes.Add(scope); } } lock (response) { response.Endpoints.Add(metadata); if (response.Endpoints.Count >= criteria.MaxResults) { handle.Set(); } } }; //To receive the callbacks from the services ClientDiscoveryResponseCallback callback = new ClientDiscoveryResponseCallback(contractName, contractNamespace, addEndpoint); IDiscovery discoveryServiceProxy = DuplexChannelFactory <IDiscovery, IDiscoveryCallback> .CreateChannel(callback, DiscoveryFactory.Binding, DiscoveryFactory.Address.DiscoveryService); discoveryServiceProxy.OnDiscoveryRequest(criteria.ContractTypeNames[0].Name, criteria.ContractTypeNames[0].Namespace, criteria.Scopes.ToArray()); bool found = handle.WaitOne(criteria.Duration); handle.Set(); handle.Close(); //If there is cardinality of 'some' the channel will fault, but we don't care here as we already got the max results try { (discoveryServiceProxy as ICommunicationObject).Close(); } catch {} if (found == false) { Trace.WriteLine("Could not find endpoints within specified discovery criteria duration"); } return(response); }