Example #1
0
        /// <summary>
        /// Creates a <see cref="System.ServiceModel.ServiceHost"/> with specific base addresses and initializes it with specified data.
        /// </summary>
        /// <param name="constructorString">The initialization data passed to the <see cref="System.ServiceModel.ServiceHostBase"/> instance being constructed by the factory.</param>
        /// <param name="baseAddresses">The <see cref="System.Array"/> of type <see cref="System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="System.ServiceModel.ServiceHost"/> with specific base addresses.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="constructorString" /> or <paramref name="baseAddresses"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrown if <paramref name="constructorString" /> is empty.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// Thrown if the <see cref="Autofac.Integration.Wcf.AutofacHostFactory.Container"/>
        /// is <see langword="null" />.
        /// </exception>
        /// <remarks>
        /// <para>
        /// If <see cref="Autofac.Integration.Wcf.AutofacHostFactory.HostConfigurationAction"/>
        /// is not <see langword="null" />, the new service host instance is run
        /// through the configuration action prior to being returned. This allows
        /// you to programmatically configure behaviors or other aspects of the
        /// host.
        /// </para>
        /// </remarks>
        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            if (constructorString == null)
            {
                throw new ArgumentNullException("constructorString");
            }
            if (constructorString.Length == 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.ArgumentException_StringEmpty, "constructorString"), "constructorString");
            }
            if (Container == null)
            {
                throw new InvalidOperationException(AutofacHostFactoryResources.ContainerIsNull);
            }

            var dataProvider = AutofacHostFactory.ServiceImplementationDataProvider;

            if (dataProvider == null)
            {
                dataProvider = new DefaultServiceImplementationDataProvider();
            }

            var data = dataProvider.GetServiceImplementationData(constructorString);

            if (data.ServiceTypeToHost == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.NoServiceTypeToHost, dataProvider.GetType(), constructorString));
            }
            if (!data.ServiceTypeToHost.IsClass)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.ImplementationTypeUnknown, constructorString, data.ServiceTypeToHost));
            }

            ServiceHost host;

            if (data.HostAsSingleton)
            {
                object singletonInstance = data.ImplementationResolver(Container);
                host = CreateSingletonServiceHost(singletonInstance, baseAddresses);
            }
            else
            {
                host          = CreateServiceHost(data.ServiceTypeToHost, baseAddresses);
                host.Opening += (sender, args) => host.Description.Behaviors.Add(new AutofacDependencyInjectionServiceBehavior(Container, data));
            }

            ApplyHostConfigurationAction(host);

            return(host);
        }
Example #2
0
        /// <summary>
        /// Creates a <see cref="System.ServiceModel.ServiceHost"/> with specific base addresses and initializes it with specified data.
        /// </summary>
        /// <param name="constructorString">The initialization data passed to the <see cref="System.ServiceModel.ServiceHostBase"/> instance being constructed by the factory.</param>
        /// <param name="baseAddresses">The <see cref="System.Array"/> of type <see cref="System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="System.ServiceModel.ServiceHost"/> with specific base addresses.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="constructorString" /> or <paramref name="baseAddresses"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrown if <paramref name="constructorString" /> is empty.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// Thrown if the <see cref="Autofac.Integration.Wcf.AutofacHostFactory.Container"/>
        /// is <see langword="null" />.
        /// </exception>
        /// <remarks>
        /// <para>
        /// If <see cref="Autofac.Integration.Wcf.AutofacHostFactory.HostConfigurationAction"/>
        /// is not <see langword="null" />, the new service host instance is run
        /// through the configuration action prior to being returned. This allows
        /// you to programmatically configure behaviors or other aspects of the
        /// host.
        /// </para>
        /// </remarks>
        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            if (constructorString == null)
            {
                throw new ArgumentNullException("constructorString");
            }
            if (constructorString.Length == 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.ArgumentException_StringEmpty, "constructorString"), "constructorString");
            }
            if (Container == null)
            {
                throw new InvalidOperationException(AutofacHostFactoryResources.ContainerIsNull);
            }

            var dataProvider = AutofacHostFactory.ServiceImplementationDataProvider;
            if (dataProvider == null)
            {
                dataProvider = new DefaultServiceImplementationDataProvider();
            }

            var data = dataProvider.GetServiceImplementationData(constructorString);

            if (data.ServiceTypeToHost == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, AutofacHostFactoryResources.NoServiceTypeToHost, dataProvider.GetType(), constructorString));
            }
            if (!data.ServiceTypeToHost.IsClass)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.ImplementationTypeUnknown, constructorString, data.ServiceTypeToHost));
            }

            ServiceHost host;
            if (data.HostAsSingleton)
            {
                object singletonInstance = data.ImplementationResolver(Container);
                host = CreateSingletonServiceHost(singletonInstance, baseAddresses);
            }
            else
            {
                host = CreateServiceHost(data.ServiceTypeToHost, baseAddresses);
                host.Opening += (sender, args) => host.Description.Behaviors.Add(new AutofacDependencyInjectionServiceBehavior(Container, data));
            }

            ApplyHostConfigurationAction(host);

            return host;
        }