Esempio n. 1
0
        private ChannelFactory <T> GetChannelFactory(Action <ChannelFactory <T> > modifyChannelFactory = null, Action <System.ServiceModel.Channels.Binding> modifyBinding = null)
        {
            System.ServiceModel.Channels.Binding binding = Binding.GetDefaultBinding();

            if (modifyBinding != null)
            {
                modifyBinding(binding);
            }

            ChannelFactory <T> channelFactory = new ChannelFactory <T>(binding);
            ServiceBehavior    behavior       = new ServiceBehavior(Transport.Protocol, Transport.EnableSsl);

            behavior.SetBehavior(channelFactory);

            if (modifyChannelFactory != null)
            {
                modifyChannelFactory(channelFactory);
            }

            if (EnableLogging)
            {
                channelFactory.Endpoint.Behaviors.Add(new LogMessageBehavior());
            }

            return(channelFactory);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes the <see cref="ServiceHostBase" /> with a single endpoint.
        /// </summary>
        /// <param name="name">The name of the endpoint.</param>
        /// <param name="url">The url to host the endpoint at.</param>
        /// <param name="serviceType">The interface type of the service.</param>
        /// <param name="modifyServiceHost"></param>
        /// <param name="modifyBinding"></param>
        protected void Initialize(string name, string url, Type serviceType, Action <System.ServiceModel.ServiceHostBase> modifyServiceHost = null, Action <System.ServiceModel.Channels.Binding> modifyBinding = null)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }

            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            if (CheckIsInitialized)
            {
                return;
            }

            try
            {
                InitializeDisposeLock.EnterWriteLock();

                if (CheckIsInitialized)
                {
                    return;
                }

                ServiceHost = new ServiceHost(this, new Uri(url));

                EndpointIdentity endpointIdentity = EndpointIdentity.CreateDnsIdentity(Transport.ServiceDomainName);
                EndpointAddress  endpointAddress  = new EndpointAddress(new Uri(url), endpointIdentity);

                System.ServiceModel.Channels.Binding binding = Binding.GetDefaultBinding();

                if (modifyBinding != null)
                {
                    modifyBinding(binding);
                }

                ServiceEndpoint serviceEndpoint = new ServiceEndpoint(ContractDescription.GetContract(serviceType))
                {
                    Name     = name,
                    Address  = endpointAddress,
                    Contract = ContractDescription.GetContract(serviceType),
                    Binding  = binding
                };

                ServiceHost.AddServiceEndpoint(serviceEndpoint);

                ServiceBehavior behavior = new ServiceBehavior(Transport.Protocol, Transport.EnableSsl);
                behavior.SetBehavior(ServiceHost);

                if (modifyServiceHost != null)
                {
                    modifyServiceHost(ServiceHost);
                }

                if (EnableLogging)
                {
                    ServiceHost.Description.Behaviors.Add(new LogMessageBehavior());
                }

                ServiceHost.Open();
            }
            finally
            {
                InitializeDisposeLock.ExitWriteLock();
            }
        }