예제 #1
0
        static string[] DiscoverMexAddresses()
        {
            DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
            FindResponse    discovered      = discoveryClient.Find(FindCriteria.CreateMetadataExchangeEndpointCriteria());

            return(discovered.Endpoints.Select(mexEndpoint => mexEndpoint.Address.Uri.AbsoluteUri).ToArray());
        }
예제 #2
0
        /// <summary>
        /// Gets the endpoint discovery metadata.
        /// </summary>
        /// <param name="contractTypeName">Name of the contract type.</param>
        /// <returns>SerializableEndpoint[][].</returns>
        public SerializableEndpoint[] GetEndpoints(string contractTypeName)
        {
            using (var clt = CreateDiscoveryClient())
            {
                try
                {
                    var lst = new List <XmlQualifiedName>()
                    {
                        new XmlQualifiedName(contractTypeName)
                    };
                    var criteria         = FindCriteria.CreateMetadataExchangeEndpointCriteria(lst);
                    var resp             = clt.Find(criteria);
                    var serviceEndpoints = new List <SerializableEndpoint>();
                    foreach (var it in resp.Endpoints)
                    {
                        serviceEndpoints.AddRange(MetadataHelper
                                                  .GetEndpoints(it.Address.Uri.AbsoluteUri)
                                                  .Select(ep => new SerializableEndpoint(ep, 0)));
                    }
                    return(serviceEndpoints.ToArray());
                }
                catch (Exception ex)
                {
                    FxLog <Discos> .LogException(ex);

                    return(null);
                }
                finally
                {
                    CloseClient(clt);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Gets all endpoints.
        /// </summary>
        /// <returns>SerializableEndpoint[][].</returns>
        public SerializableEndpoint[] GetAllEndpoints()
        {
            using (var clt = CreateDiscoveryClient())
            {
                try
                {
                    var criteria         = FindCriteria.CreateMetadataExchangeEndpointCriteria();
                    var resp             = clt.Find(criteria);
                    var serviceEndpoints = new List <SerializableEndpoint>();
                    foreach (var it in resp.Endpoints)
                    {
                        try
                        {
                            var sep = MetadataHelper.GetEndpoints(it.Address.Uri.AbsoluteUri);
                            serviceEndpoints.AddRange(sep.Select(ep => new SerializableEndpoint(ep, 0)));
                        }
                        catch (Exception e)
                        {
                            FxLog <Discos> .LogException(e);
                        }
                    }
                    return(serviceEndpoints.ToArray());
                }
                catch (Exception ex)
                {
                    FxLog <Discos> .LogException(ex);

                    return(null);
                }
                finally
                {
                    CloseClient(clt);
                }
            }
        }
예제 #4
0
        static T[] CreateChannelsFromMex <T>(Uri scope = null) where T : class
        {
            DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
            FindCriteria    criteria        = FindCriteria.CreateMetadataExchangeEndpointCriteria();

            FindResponse discovered = discoveryClient.Find(criteria);

            discoveryClient.Close();

            if (discovered.Endpoints.Count == 0)
            {
                return(new T[] {});
            }

            List <T> list = new List <T>();

            foreach (EndpointDiscoveryMetadata mexEndpoint in discovered.Endpoints)
            {
                ServiceEndpoint[] endpoints = MetadataHelper.GetEndpoints(mexEndpoint.Address.Uri.AbsoluteUri, typeof(T));
                foreach (ServiceEndpoint endpoint in endpoints)
                {
                    T proxy = ChannelFactory <T> .CreateChannel(endpoint.Binding, endpoint.Address);

                    list.Add(proxy);
                }
            }
            Debug.Assert(list.Count > 0);
            return(list.ToArray());
        }
예제 #5
0
        public static Binding DiscoverBinding <T>(Uri scope = null)
        {
            DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());

            FindCriteria criteria = FindCriteria.CreateMetadataExchangeEndpointCriteria();

            criteria.MaxResults = 1;
            if (scope != null)
            {
                criteria.Scopes.Add(scope);
            }
            FindResponse discovered = discoveryClient.Find(criteria);

            discoveryClient.Close();

            Debug.Assert(discovered.Endpoints.Count == 1);

            Uri mexAddress = discovered.Endpoints[0].Address.Uri;

            ServiceEndpoint[] endpoints = MetadataHelper.GetEndpoints(mexAddress.AbsoluteUri, typeof(T));

            Debug.Assert(endpoints.Length == 1);

            return(endpoints[0].Binding);
        }
        public static T CreateChannel <T>(string serviceNamespace, string secret, Uri scope = null) where T : class
        {
            ServiceBusDiscoveryClient discoveryClient = new ServiceBusDiscoveryClient(serviceNamespace, secret);

            FindCriteria criteria = FindCriteria.CreateMetadataExchangeEndpointCriteria();

            criteria.MaxResults = 1;
            if (scope != null)
            {
                criteria.Scopes.Add(scope);
            }
            FindResponse discovered = discoveryClient.Find(criteria);

            discoveryClient.Close();

            Debug.Assert(discovered.Endpoints.Count == 1);
            Uri mexAddress = discovered.Endpoints[0].Address.Uri;

            ServiceEndpoint[] endpoints = ServiceBusMetadataHelper.GetEndpoints(mexAddress.AbsoluteUri, typeof(T), secret);
            Debug.Assert(endpoints.Length == 1);

            Binding         binding = endpoints[0].Binding;
            EndpointAddress address = endpoints[0].Address;

            ChannelFactory <T> factory = new ChannelFactory <T>(binding, address);

            factory.SetServiceBusCredentials(secret);

            return(factory.CreateChannel());
        }
예제 #7
0
        /// <summary>
        /// Finds the list of servers with the provided filter criteria
        /// </summary>
        /// <param name="serverId">Identifier for the server</param>
        /// <param name="filterCriteria">filter criteria</param>
        /// <returns>Find results</returns>
        public static async Task <FindResponse> FindServersAsync(string serverId, Dictionary <string, string> filterCriteria)
        {
            var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint(UdpDiscoveryEndpoint.DefaultIPv4MulticastAddress));
            var findCriteria    = FindCriteria.CreateMetadataExchangeEndpointCriteria(typeof(IIPC));

            findCriteria.Scopes.Add(new Uri($"id:{serverId}".ToLower()));
            if (filterCriteria != null)
            {
                foreach (var entry in filterCriteria)
                {
                    findCriteria.Scopes.Add(new Uri($"{entry.Key}:{entry.Value}".ToLower()));
                }
            }

            return(await discoveryClient.FindTaskAsync(findCriteria));
        }
예제 #8
0
        string[] DiscoverServiceBusMexAddresses(string serviceNamespace)
        {
            NetOnewayRelayBinding binding = new NetOnewayRelayBinding();

            if (m_DisoveryPaths.ContainsKey(serviceNamespace) == false)
            {
                m_DisoveryPaths[serviceNamespace] = DiscoverableServiceHost.DiscoveryPath;
            }
            EndpointAddress address = new EndpointAddress(ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, m_DisoveryPaths[serviceNamespace]));

            ServiceBusDiscoveryClient discoveryClient = new ServiceBusDiscoveryClient(binding, address);
            IServiceBusProperties     properties      = discoveryClient as IServiceBusProperties;

            properties.Credential = m_NamespaceCredentials[serviceNamespace];

            FindResponse discovered = discoveryClient.Find(FindCriteria.CreateMetadataExchangeEndpointCriteria());

            return(discovered.Endpoints.Select(mexEndpoint => mexEndpoint.Address.Uri.AbsoluteUri).ToArray());
        }
예제 #9
0
        /// <summary>
        /// Loads all Probe messages from file into list.
        /// </summary>
        /// <param name="list">Reference to a list</param>
        /// <param name="path">Path to xml file</param>
        public static void LoadMessages(List <FindRequestContext> list, string path)
        {
            MethodInfo      loadCriteria           = typeof(FindCriteria).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic).First((x) => "ReadFrom" == x.Name);
            ConstructorInfo ctorFindRequestContext = typeof(FindRequestContext).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First();

            using (XmlReader reader = XmlReader.Create(path))
            {
                while (reader.ReadToFollowing("Envelope", "http://www.w3.org/2003/05/soap-envelope"))
                {
                    using (Message msg = Message.CreateMessage(reader, Int16.MaxValue, MessageVersion.Soap12))
                    {
                        FindCriteria data = FindCriteria.CreateMetadataExchangeEndpointCriteria();

                        loadCriteria.Invoke(data, new object[] { DiscoveryVersion.WSDiscovery11, msg.GetReaderAtBodyContents() });

                        list.Add((FindRequestContext)ctorFindRequestContext.Invoke(new object[] { data }));
                    }
                }
            }
        }
예제 #10
0
        /// <summary>
        /// Gets all endpoint addresses.
        /// </summary>
        /// <returns>DiscoItem[][].</returns>
        public DiscoItem[] GetAllEndpointAddresses()
        {
            using (var clt = CreateDiscoveryClient())
            {
                try
                {
                    var criteria  = FindCriteria.CreateMetadataExchangeEndpointCriteria();
                    var resp      = clt.Find(criteria);
                    var addresses = new List <DiscoItem>();

                    foreach (var it in resp.Endpoints.Where(i => (i.Address.Uri.Scheme == Uri.UriSchemeHttp))) // only consider http mex endpoints
                    {
                        try
                        {
                            var sep = MetadataHelper.GetEndpoints(it.Address.Uri.AbsoluteUri);
                            foreach (var ep in sep)
                            {
                                if (!ep.IsSystemEndpoint)
                                {
                                    addresses.Add(new DiscoItem()
                                    {
                                        Id       = Guid.NewGuid().ToString(),
                                        Endpoint = ep.Address.Uri.OriginalString,
                                        Contract = String.Format("{0}:{1}", ep.Contract.Namespace, ep.Contract.Name)
                                    });
                                }
                            }
                        }
                        catch (Exception exp)
                        {
                            FxLog <Discos> .LogException(exp);
                        }
                    }
                    return(addresses.ToArray());
                }
                finally
                {
                    CloseClient(clt);
                }
            }
        }
        public static T[] CreateChannels <T>(string serviceNamespace, string secret, Uri scope = null) where T : class
        {
            ServiceBusDiscoveryClient discoveryClient = new ServiceBusDiscoveryClient(serviceNamespace, secret);
            FindCriteria criteria = FindCriteria.CreateMetadataExchangeEndpointCriteria();

            if (scope != null)
            {
                criteria.Scopes.Add(scope);
            }
            FindResponse discovered = discoveryClient.Find(criteria);

            discoveryClient.Close();

            if (discovered.Endpoints.Count == 0)
            {
                return(new T[] {});
            }
            Debug.Assert(discovered.Endpoints.Count > 0);

            List <T> list = new List <T>();

            foreach (EndpointDiscoveryMetadata mexEndpoint in discovered.Endpoints)
            {
                ServiceEndpoint[] endpoints = ServiceBusMetadataHelper.GetEndpoints(mexEndpoint.Address.Uri.AbsoluteUri, typeof(T), secret);
                foreach (ServiceEndpoint endpoint in endpoints)
                {
                    ChannelFactory <T> factory = new ChannelFactory <T>(endpoint.Binding, endpoint.Address);
                    factory.SetServiceBusCredentials(secret);

                    T proxy = factory.CreateChannel();
                    list.Add(proxy);
                }
            }
            Debug.Assert(list.Count > 0);
            return(list.ToArray());
        }
예제 #12
0
        public static T CreateChannel <T>(Uri scope = null) where T : class
        {
            DuplexDiscoveryClient discoveryClient = new DuplexDiscoveryClient(Address.DiscoveryService);

            FindCriteria criteria = FindCriteria.CreateMetadataExchangeEndpointCriteria();

            criteria.MaxResults = 1;
            if (scope != null)
            {
                criteria.Scopes.Add(scope);
            }
            FindResponse discovered = discoveryClient.Find(criteria);

            Debug.Assert(discovered.Endpoints.Count == 1);
            Uri mexAddress = discovered.Endpoints[0].Address.Uri;

            ServiceEndpoint[] endpoints = MetadataHelper.GetEndpoints(mexAddress.AbsoluteUri, typeof(T));
            Debug.Assert(endpoints.Length == 1);

            Binding         binding = endpoints[0].Binding;
            EndpointAddress address = endpoints[0].Address;

            return(ChannelFactory <T> .CreateChannel(binding, address));
        }
예제 #13
0
        static void Main(string[] args)
        {
            //Tworzymy klienta discovery, którym będziemy łaczyć się z endpointem discovery
            //nako argument pobieramy UdpDiscoveryEndpoint, bo w server.app.cfg taki jest kind enpointa w service
            var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());

            /*XmlQualifiedName pos1 = new XmlQualifiedName("Rashim.RND.AdhocDiscovery.Services.IMessageService2");
             * XmlQualifiedName pos2 = new XmlQualifiedName("IMessageService2");
             * XmlQualifiedName pos3 = new XmlQualifiedName("Rashim.RND.AdhocDiscovery.Services.MessageService2");
             * XmlQualifiedName pos4 = new XmlQualifiedName("MessageService2");
             * var list = new List<XmlQualifiedName>() {pos1, pos2, pos3, pos4};*/

            //Tworzymy filtr. Argument może być
            // * pusty (brak filtrów)
            // * typeof(IMessageService)
            // * IEnumerable<XmlQualifiedName>, ale nie wiem jak tę listę wypełnić, bo powyższe nie działają
            var findCriteria = FindCriteria.CreateMetadataExchangeEndpointCriteria();// typeof(IMessageService));

            //Pobieramy tylko jedną (w tym przypadku i tak serwer ma jeden endpoint discovery, ale w innym
            //przypadku trzeba oprogramować możliwość wystąpienia innych discovery endpointów)
            findCriteria.MaxResults = 1;
            Debug.Assert(findCriteria.Scopes != null, "findCriteria.Scopes != null");

            //Ustalamy adres pod którym będziemy szukać discovery endpoint
            findCriteria.Scopes.Add(new Uri("ldap:///ou=people,o=rashim"));
            //findCriteria.Scopes.Add(new Uri("http://localhost:8733/Mess"));

            //Zapuszczamy szukanie informacji. To może potrwać ładnych kilka sekund.
            var findResponse = discoveryClient.Find(findCriteria);

            //Jeśli mamy odpowiedź
            if (findResponse != null)
            {
                if (findResponse.Endpoints != null)
                {
                    if (findResponse.Endpoints.Count > 0)
                    {
                        //Pobieranie endointów z service wymienionych w contracts. Oczywiście findResponse.Endpoints[0] odda tylko endpointy dla jednego service
                        //Pobieranie endpointów tylko jednego rodzaju, oczywiście to samo mozna uzysjkać za pomocą jedno elementowej listy.
                        //var endpoints = MetadataResolver.Resolve( typeof (IMessageService), findResponse.Endpoints[0].Address);
                        var contracts = new List <ContractDescription>()
                        {
                            ContractDescription.GetContract(typeof(IMessageService)),
                            ContractDescription.GetContract(typeof(IMessageService2))
                        };
                        //W tym wypadku z findResponce pobieramy tylko pierwszy discovery endpoint
                        var endpoints = MetadataResolver.Resolve(contracts, findResponse.Endpoints[0].Address);

                        //Standardowo tworzymy połaczenie. Binding i address pobieamy ze znalezionego endpointa
                        //Tutaj też pobieramy tylko pierwszy endpoint, choć powinniśmy obsłużyć wszystkie z listy.
                        var factory = new ChannelFactory <IMessageService2>(endpoints[0].Binding, endpoints[0].Address);
                        var channel = factory.CreateChannel();

                        //Nieistotne
                        Console.WriteLine("Say something and press enter");
                        var input = Console.ReadLine();
                        while (input != null && input.ToString(CultureInfo.InvariantCulture).ToLower() != "exit")
                        {
                            Console.WriteLine(channel.GetMessage2(input));
                            input = Console.ReadLine();
                        }
                        ((IChannel)channel).Close();
                    }
                }
            }
            else
            {
                Console.WriteLine("Could not find the Services");
            }
        }