コード例 #1
0
        public List <string> GetIpV4Addresses(string scheme)
        {
            List <string> results = new List <string>();
            // get IP address instead of hostname for addresses
            string hostIpv4 = null;

            foreach (IPAddress ipAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                {
                    hostIpv4 = ipAddress.ToString();
                }
            }
            if (hostIpv4 == null)
            {
                throw new Exception("Unable to resolve host name to IPV4 address!");
            }
            foreach (EndpointData ep in _endpoints)
            {
                if (scheme == null || ep.Scheme.ToLower() == scheme.ToLower())
                {
                    results.Add(WcfHelper.FormatAddress(ep.Scheme, hostIpv4, ep.Port, _applName, _intfName));
                }
            }
            return(results);
        }
コード例 #2
0
        public CustomServiceHost(
            ILogger logger, S serviceImpl, string endpoints, string applName, string intfName,
            bool multiThreaded)
        //: base(serviceImpl)
        {
            int      limitMultiplier = WcfConst.LimitMultiplier;
            TimeSpan msgLifetime     = WcfConst.ExpressMsgLifetime;

            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            if (endpoints == null)
            {
                throw new ArgumentNullException(nameof(endpoints));
            }
            string[] endpointsParts = endpoints.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            if (endpointsParts.Length == 0)
            {
                throw new ArgumentNullException(nameof(endpoints));
            }
            _endpoints = new EndpointData[endpointsParts.Length];
            for (int i = 0; i < _endpoints.Length; i++)
            {
                _endpoints[i] = new EndpointData(endpointsParts[i]);
            }
            _applName = applName ?? throw new ArgumentNullException(nameof(applName));
            _intfName = intfName ?? throw new ArgumentNullException(nameof(intfName));
            // add standard behaviour
            ServiceBehaviorAttribute serviceBehavior = Description.Behaviors.Find <ServiceBehaviorAttribute>();

            if (serviceBehavior == null)
            {
                serviceBehavior = new ServiceBehaviorAttribute();
                Description.Behaviors.Add(serviceBehavior);
            }
            // exception handling
            //serviceBehavior.IncludeExceptionDetailInFaults = true;
            // multi-threading singleton mode
            //serviceBehavior.UseSynchronizationContext = false;
            serviceBehavior.InstanceContextMode = InstanceContextMode.Single;
            serviceBehavior.ConcurrencyMode     = multiThreaded ? ConcurrencyMode.Multiple : ConcurrencyMode.Single;
            // add endpoints
            string httpAddress = null;

            foreach (var endpoint in _endpoints)
            {
                endpoint.Binding = WcfHelper.CreateBinding(endpoint.Scheme, limitMultiplier, msgLifetime);
                endpoint.Address = WcfHelper.FormatAddress(endpoint.Scheme, null, endpoint.Port, _applName, _intfName);
                // save http base address for metadata behaviour setup (below)
                if (endpoint.Scheme == WcfConst.Http)
                {
                    httpAddress = endpoint.Address;
                }
                endpoint.Endpoint = AddServiceEndpoint(typeof(I), endpoint.Binding, endpoint.Address);
                if (endpoint.Scheme == WcfConst.NetMsmq)
                {
                    endpoint.QueueName = WcfHelper.FormatQueueName(_applName, endpoint.Port, _intfName);
                }
                //_Logger.LogDebug("{1}: Added service endpoint: {0}", _Endpoints[i].Endpoint.Address, this.GetType().Name);
            }
            // add metadata behaviour (if HTTP endpoint exists)
            ServiceMetadataBehavior metadataBehavior = Description.Behaviors.Find <ServiceMetadataBehavior>();

            if ((metadataBehavior == null) && (httpAddress != null))
            {
                metadataBehavior = new ServiceMetadataBehavior
                {
                    HttpGetEnabled = true,
                    HttpGetUrl     = new Uri(httpAddress)
                };
                Description.Behaviors.Add(metadataBehavior);
                _logger.LogDebug("Added MEX for endpoint: {0}", httpAddress);
            }
            // ready to open
            Open();
            if (State != CommunicationState.Opened)
            {
                throw new Exception($"ServiceHost State ({State}) <> Opened!");
            }
            //_Logger.LogDebug("ServiceHost State: {0}", this.State);
        }