Beispiel #1
0
        public void Open(string announcementConnectionUri, int announcementTimerSec, bool enableAnnouncement)
        {
            try
            {
                ServiceDiscoveryBehavior serviceDiscoveryBehavior = new ServiceDiscoveryBehavior();

                EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();

                XElement endpointMetadata = new XElement("Root", new XElement(_metaDataElement, _metaDataElementValue));
                XElement aliasMetadata    = new XElement("Root", new XElement(_metaDataAlias, _metaDataAliasValue));
                XElement LoginMetadata    = new XElement("Root", new XElement(_metaDataLogIn, _metaDataLogInValue));

                endpointDiscoveryBehavior.Extensions.Add(endpointMetadata);
                endpointDiscoveryBehavior.Extensions.Add(aliasMetadata);
                endpointDiscoveryBehavior.Extensions.Add(LoginMetadata);

                _netTcpEndpoint.Behaviors.Add(endpointDiscoveryBehavior);

                _serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior);
                _serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

                _serviceHost.Open();

                if (enableAnnouncement)
                {
                    Uri             announcementEndpointUri     = new Uri(announcementConnectionUri);
                    EndpointAddress announcementEndpointAddress = new EndpointAddress(announcementEndpointUri);
                    NetTcpBinding   binding = new NetTcpBinding();
                    binding.Security.Mode = SecurityMode.None;
                    _announcementEndpoint = new AnnouncementEndpoint(binding, announcementEndpointAddress);

                    _announcementClient        = new AnnouncementClient(_announcementEndpoint);
                    _endpointDiscoveryMetadata = EndpointDiscoveryMetadata.FromServiceEndpoint(_netTcpEndpoint);

                    //Start a timer that send announcement message
                    _announcementTimer           = new Timer(announcementTimerSec * 1000);
                    _announcementTimer.AutoReset = true;
                    _announcementTimer.Elapsed  += new ElapsedEventHandler(_announcementTimer_Elapsed);
                    _announcementTimer.Start();

                    _announcementClient.Open();
                }

                IsRunning = true;
            }
            catch (EndpointNotFoundException ex)
            {
                //this error occurs when announcement endpoint is not on the network
            }
            catch (CommunicationException ex)
            {
                _serviceHost.Abort();
                // customize this exception to be more specific
                throw;
            }
        }
        internal static AnnouncementEventArgs CreateAnnouncementArgs(Uri address, string contractName, string contractNamespace, Uri[] scopes)
        {
            Type            type        = typeof(AnnouncementEventArgs);
            ConstructorInfo constructor = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0];

            ContractDescription contract = new ContractDescription(contractName, contractNamespace);

            ServiceEndpoint           endpoint = new ServiceEndpoint(contract, null, new EndpointAddress(address));
            EndpointDiscoveryMetadata metadata = EndpointDiscoveryMetadata.FromServiceEndpoint(endpoint);

            return(constructor.Invoke(new object[] { null, metadata }) as AnnouncementEventArgs);
        }
        public static IServiceCollection AddECSContainerSupport(this IServiceCollection services, IConfiguration configuration)
        {
            if (Environment.GetEnvironmentVariable(EMG.Extensions.Configuration.ECSMetadataExtensions.ECSContainerMetadataFileKey) != null)
            {
                services.Configure <AnnouncementServiceOptions>(o => o.EndpointDiscoveryMetadata = GetEndpointMetadataFromECS);
            }

            return(services);

            EndpointDiscoveryMetadata GetEndpointMetadataFromECS(ServiceEndpoint endpoint)
            {
                if (endpoint.Address is null)
                {
                    throw new ArgumentNullException(nameof(endpoint), "endpoint address should not be null");
                }

                var containerMetadata = configuration.Get <ECSContainerMetadata>();

                var endpointMetadata = EndpointDiscoveryMetadata.FromServiceEndpoint(endpoint);

                if (endpointMetadata is null)
                {
                    return(null);
                }

                if (containerMetadata?.HostPrivateIPv4Address is null)
                {
                    return(endpointMetadata);
                }

                var uriBuilder = new UriBuilder(endpointMetadata.Address.Uri)
                {
                    Host = containerMetadata.HostPrivateIPv4Address,
                    Port = GetMappedPortOrDefault(containerMetadata.PortMappings, endpointMetadata.Address.Uri.Port)
                };

                endpointMetadata.Address = new System.ServiceModel.EndpointAddress(uriBuilder.Uri.ToString());

                return(endpointMetadata);
            }

            int GetMappedPortOrDefault(IEnumerable <PortMapping> mappings, int port)
            {
                var hostPortByContainerPort = mappings.ToDictionary(k => k.ContainerPort, v => v.HostPort);

                if (hostPortByContainerPort.TryGetValue(port, out var mappedPort))
                {
                    return(mappedPort);
                }

                return(port);
            }
        }