示例#1
0
 public AddressBinding(string uri, int limitMultiplier, TimeSpan msgLifetime)
 {
     Address = new EndpointAddress(uri);
     Binding = WcfHelper.CreateBinding(WcfHelper.GetSchemeFromAddress(Address), limitMultiplier, msgLifetime);
 }
        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);
        }