Пример #1
0
        //[OperationContract]
        public bool ServiceIsExists(string serviceName)
        {
            try
            {
                ExportServiceHost host = ServiceHostManager.Services.First <ExportServiceHost>(h => h.GetServiceName() == serviceName);
                return(host != null);
            }
            catch (ArgumentNullException)
            {
                // Todo: ArgumentNullException
            }
            catch (InvalidOperationException)
            {
                // Todo: InvalidOperationException
            }

            return(false);
        }
Пример #2
0
        //[OperationContract]
        public Uri[] GetServiceAddress(string serviceName)
        {
            try
            {
                ExportServiceHost host = ServiceHostManager.Services.First <ExportServiceHost>(h => h.GetServiceName() == serviceName);
                return(host.GetAddresses());
            }
            catch (ArgumentNullException)
            {
                // Todo: ArgumentNullException
            }
            catch (InvalidOperationException)
            {
                // Todo: InvalidOperationException
            }

            return(null);
        }
        /// <summary>
        /// Creates an instance of <see cref="ExportServiceHost"/> using service metadata.
        /// </summary>
        /// <param name="container">The composition container.</param>
        /// <param name="meta">The metadata.</param>
        public static ExportServiceHost CreateExportServiceHost(CompositionContainer container, IHostedServiceMetadata meta)
        {
            if (container == null) throw new ArgumentNullException("container");
            if (meta == null) throw new ArgumentNullException("meta");

            var host = new ExportServiceHost(meta, new Uri[0]);
            host.Description.Behaviors.Add(new ExportServiceBehavior(container, meta.Name));
#if WITH_TOKEN
            if (meta.TokenValidationMode == TokenValidationMode.Check)
                host.Description.Behaviors.Add(new TokenValidationServiceBehavior(container));
#endif

#if WITH_ERROR_HANDLER
            foreach (ServiceEndpoint se in host.Description.Endpoints)
                se.Behaviors.Add(new EndpointErrorHandlerBehavior(container));
#endif

            return host;
        }
        /// <summary>
        /// Creates an instance of <see cref="ExportServiceHost"/> using service metadata.
        /// </summary>
        /// <param name="container">The composition container.</param>
        /// <param name="meta">The metadata.</param>
        public static ExportServiceHost CreateExportServiceHost(CompositionContainer container, IHostedServiceMetadata meta)
        {
            if (container == null) throw new ArgumentNullException("container");
            if (meta == null) throw new ArgumentNullException("meta");

            var host = new ExportServiceHost(meta, new Uri[0]);
            host.Description.Behaviors.Add(new ExportServiceBehavior(container, meta.Name));

            foreach (var behavior in BehaviorFactory.GetConfiguredBehaviors(meta.Name))
            {
                var behaviorType = behavior.GetType();
                if (host.Description.Behaviors.Contains(behaviorType))
                {
                    host.Description.Behaviors.Remove(behaviorType);
                }
                host.Description.Behaviors.Add(behavior);
            }

            return host;
        }
        /// <summary>
        /// Creates the service host that will handle web service requests.
        /// </summary>
        /// <param name="constructorString">The constructor string used to select the service.</param>
        /// <param name="baseAddresses">The set of base address for the service.</param>
        /// <returns>An instance of <see cref="ServiceHostBase"/> for the service.</returns>
        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            var meta = Container
                .GetExports<IHostedService, IHostedServiceMetadata>()
                .Where(e => e.Metadata.Name.Equals(constructorString, StringComparison.OrdinalIgnoreCase))
                .Select(e => e.Metadata)
                .SingleOrDefault();

            if (meta == null)
                return null;

            var host = new ExportServiceHost(meta, baseAddresses);
            host.Description.Behaviors.Add(new ExportServiceBehavior(Container, meta.Name));

            var contracts = meta.ServiceType
                .GetInterfaces()
                .Where(t => t.IsDefined(typeof(ServiceContractAttribute), true));

            EnsureHttpBinding(host, contracts);

            return host;
        }
        /// <summary>
        /// Ensures that the Http binding has been created.
        /// </summary>
        /// <param name="host">The Http binding.</param>
        /// <param name="contracts">The set of contracts</param>
        private static void EnsureHttpBinding(ExportServiceHost host, IEnumerable<Type> contracts)
        {
            var binding = new BasicHttpBinding();

            host.Description.Endpoints.Clear();

            foreach (var contract in contracts)
                host.AddServiceEndpoint(contract.FullName, binding, "");
        }