Exemple #1
0
 /// <summary>
 /// Retrieve a service instance from the context.
 /// </summary>
 /// <param name="serviceData">
 /// Data object containing information about how to resolve the service
 /// implementation instance.
 /// </param>
 /// <returns>The service instance.</returns>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="serviceData" /> is <see langword="null" />.
 /// </exception>
 public object Resolve(ServiceImplementationData serviceData)
 {
     if (serviceData == null)
     {
         throw new ArgumentNullException("serviceData");
     }
     return(serviceData.ImplementationResolver(this.OperationLifetime));
 }
 public void ReleaseInstance_NullInstanceContext()
 {
     var data = new ServiceImplementationData();
     var container = new ContainerBuilder().Build();
     var provider = new AutofacInstanceProvider(container, data);
     object instance = new object();
     Assert.Throws<ArgumentNullException>(() => provider.ReleaseInstance(null, instance));
 }
 public void GetInstance_NullInstanceContext()
 {
     var data = new ServiceImplementationData();
     var container = new ContainerBuilder().Build();
     var provider = new AutofacInstanceProvider(container, data);
     var message = new TestMessage();
     Assert.Throws<ArgumentNullException>(() => provider.GetInstance(null, message));
 }
        /// <summary>
        /// Gets data about a service implementation.
        /// </summary>
        /// <param name="value">
        /// The constructor string passed in to the service host factory
        /// that is used to determine which type to host/use as a service
        /// implementation.
        /// </param>
        /// <returns>
        /// A <see cref="Autofac.Integration.Wcf.ServiceImplementationData"/>
        /// object containing information about which type to use in
        /// the service host and which type to use to resolve the implementation.
        /// </returns>
        /// <remarks>
        /// <para>
        /// This resolver takes the constructor string stored in the .svc file
        /// and resolves a matching keyed or typed service from the root
        /// application container. That resolved type is used both for the
        /// service host as well as the implementation type.
        /// </para>
        /// </remarks>
        /// <exception cref="System.InvalidOperationException">
        /// Thrown if the <see cref="Autofac.Integration.Wcf.AutofacHostFactory.Container"/>
        /// is <see langword="null" />;
        /// if the service indicated by <paramref name="value" />
        /// is not registered with the <see cref="Autofac.Integration.Wcf.AutofacHostFactory.Container"/>;
        /// or if the service is a singleton that isn't registered as a singleton.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="value" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrown if <paramref name="value" /> is empty.
        /// </exception>
        public virtual ServiceImplementationData GetServiceImplementationData(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (value.Length == 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.ArgumentException_StringEmpty, "value"));
            }
            if (AutofacHostFactory.Container == null)
            {
                throw new InvalidOperationException(AutofacHostFactoryResources.ContainerIsNull);
            }
            IComponentRegistration registration = null;

            if (!AutofacHostFactory.Container.ComponentRegistry.TryGetRegistration(new KeyedService(value, typeof(object)), out registration))
            {
                Type serviceType = Type.GetType(value, false);
                if (serviceType != null)
                {
                    AutofacHostFactory.Container.ComponentRegistry.TryGetRegistration(new TypedService(serviceType), out registration);
                }
            }

            if (registration == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.ServiceNotRegistered, value));
            }

            var data = new ServiceImplementationData
            {
                ConstructorString      = value,
                ServiceTypeToHost      = registration.Activator.LimitType,
                ImplementationResolver = l => l.ResolveComponent(registration, Enumerable.Empty <Parameter>())
            };

            var implementationType = registration.Activator.LimitType;

            if (IsSingletonWcfService(implementationType))
            {
                if (!IsRegistrationSingleInstance(registration))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.ServiceMustBeSingleInstance, implementationType.FullName));
                }

                data.HostAsSingleton = true;
            }
            else
            {
                if (IsRegistrationSingleInstance(registration))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.ServiceMustNotBeSingleInstance, implementationType.FullName));
                }
            }

            return(data);
        }
        /// <summary>
        /// Gets data about a service implementation.
        /// </summary>
        /// <param name="value">
        /// The constructor string passed in to the service host factory
        /// that is used to determine which type to host/use as a service
        /// implementation.
        /// </param>
        /// <returns>
        /// A <see cref="Autofac.Integration.Wcf.ServiceImplementationData"/>
        /// object containing information about which type to use in
        /// the service host and which type to use to resolve the implementation.
        /// </returns>
        /// <remarks>
        /// <para>
        /// This resolver takes the constructor string stored in the .svc file
        /// and resolves a matching keyed or typed service from the root
        /// application container. That resolved type is used both for the
        /// service host as well as the implementation type.
        /// </para>
        /// </remarks>
        /// <exception cref="System.InvalidOperationException">
        /// Thrown if the <see cref="Autofac.Integration.Wcf.AutofacHostFactory.Container"/>
        /// is <see langword="null" />;
        /// if the service indicated by <paramref name="value" />
        /// is not registered with the <see cref="Autofac.Integration.Wcf.AutofacHostFactory.Container"/>;
        /// or if the service is a singleton that isn't registered as a singleton.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="value" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrown if <paramref name="value" /> is empty.
        /// </exception>
        public virtual ServiceImplementationData GetServiceImplementationData(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (value.Length == 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.ArgumentException_StringEmpty, "value"));
            }
            if (AutofacHostFactory.Container == null)
            {
                throw new InvalidOperationException(AutofacHostFactoryResources.ContainerIsNull);
            }
            IComponentRegistration registration = null;
            if (!AutofacHostFactory.Container.ComponentRegistry.TryGetRegistration(new KeyedService(value, typeof(object)), out registration))
            {
                Type serviceType = Type.GetType(value, false);
                if (serviceType != null)
                {
                    AutofacHostFactory.Container.ComponentRegistry.TryGetRegistration(new TypedService(serviceType), out registration);
                }
            }

            if (registration == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.ServiceNotRegistered, value));
            }

            var data = new ServiceImplementationData
            {
                ConstructorString = value,
                ServiceTypeToHost = registration.Activator.LimitType,
                ImplementationResolver = l => l.ResolveComponent(registration, Enumerable.Empty<Parameter>())
            };

            var implementationType = registration.Activator.LimitType;
            if (IsSingletonWcfService(implementationType))
            {
                if (!IsRegistrationSingleInstance(registration))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.ServiceMustBeSingleInstance, implementationType.FullName));
                }

                data.HostAsSingleton = true;
            }
            else
            {
                if (IsRegistrationSingleInstance(registration))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AutofacHostFactoryResources.ServiceMustNotBeSingleInstance, implementationType.FullName));
                }
            }

            return data;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AutofacDependencyInjectionServiceBehavior"/> class.
        /// </summary>
        /// <param name="rootLifetimeScope">
        /// The container from which service implementations should be resolved.
        /// </param>
        /// <param name="serviceData">
        /// Data about which service type should be hosted and how to resolve
        /// the type to use for the service implementation.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="rootLifetimeScope" /> or <paramref name="serviceData" /> is <see langword="null" />.
        /// </exception>
        public AutofacDependencyInjectionServiceBehavior(ILifetimeScope rootLifetimeScope, ServiceImplementationData serviceData)
        {
            if (rootLifetimeScope == null)
            {
                throw new ArgumentNullException("rootLifetimeScope");
            }
            if (serviceData == null)
            {
                throw new ArgumentNullException("serviceData");
            }

            _rootLifetimeScope = rootLifetimeScope;
            _serviceData       = serviceData;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AutofacDependencyInjectionServiceBehavior"/> class.
        /// </summary>
        /// <param name="rootLifetimeScope">
        /// The container from which service implementations should be resolved.
        /// </param>
        /// <param name="serviceData">
        /// Data about which service type should be hosted and how to resolve
        /// the type to use for the service implementation.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="rootLifetimeScope" /> or <paramref name="serviceData" /> is <see langword="null" />.
        /// </exception>
        public AutofacDependencyInjectionServiceBehavior(ILifetimeScope rootLifetimeScope, ServiceImplementationData serviceData)
        {
            if (rootLifetimeScope == null)
            {
                throw new ArgumentNullException("rootLifetimeScope");
            }
            if (serviceData == null)
            {
                throw new ArgumentNullException("serviceData");
            }

            _rootLifetimeScope = rootLifetimeScope;
            _serviceData = serviceData;
        }
Exemple #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AutofacInstanceProvider"/> class.
        /// </summary>
        /// <param name="rootLifetimeScope">
        /// The lifetime scope from which service instances should be resolved.
        /// </param>
        /// <param name="serviceData">
        /// Data object containing information about how to resolve the service
        /// implementation instance.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="rootLifetimeScope" /> or <paramref name="serviceData" /> is <see langword="null" />.
        /// </exception>
        public AutofacInstanceProvider(ILifetimeScope rootLifetimeScope, ServiceImplementationData serviceData)
        {
            if (rootLifetimeScope == null)
            {
                throw new ArgumentNullException("rootLifetimeScope");
            }

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

            _rootLifetimeScope = rootLifetimeScope;
            _serviceData       = serviceData;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AutofacInstanceProvider"/> class.
        /// </summary>
        /// <param name="rootLifetimeScope">
        /// The lifetime scope from which service instances should be resolved.
        /// </param>
        /// <param name="serviceData">
        /// Data object containing information about how to resolve the service
        /// implementation instance.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="rootLifetimeScope" /> or <paramref name="serviceData" /> is <see langword="null" />.
        /// </exception>
        public AutofacInstanceProvider(ILifetimeScope rootLifetimeScope, ServiceImplementationData serviceData)
        {
            if (rootLifetimeScope == null)
            {
                throw new ArgumentNullException("rootLifetimeScope");
            }

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

            _rootLifetimeScope = rootLifetimeScope;
            _serviceData = serviceData;
        }
        /// <summary>
        /// Adds the custom service behavior required for dependency injection.
        /// </summary>
        /// <param name="serviceHost">The service host.</param>
        /// <param name="contractType">The web service contract type.</param>
        /// <param name="container">The container.</param>
        /// <param name="parameters">Parameters for the instance.</param>
        public static void AddDependencyInjectionBehavior(this ServiceHostBase serviceHost, Type contractType, ILifetimeScope container, IEnumerable <Parameter> parameters)
        {
            if (serviceHost == null)
            {
                throw new ArgumentNullException("serviceHost");
            }
            if (contractType == null)
            {
                throw new ArgumentNullException("contractType");
            }
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var serviceBehavior = serviceHost.Description.Behaviors.Find <ServiceBehaviorAttribute>();

            if (serviceBehavior != null && serviceBehavior.InstanceContextMode == InstanceContextMode.Single)
            {
                return;
            }

            IComponentRegistration registration;
            var serviceToResolve = new TypedService(contractType);

            if (!container.ComponentRegistry.TryGetRegistration(serviceToResolve, out registration))
            {
                var message = string.Format(CultureInfo.CurrentCulture, ServiceHostExtensionsResources.ContractTypeNotRegistered, contractType.FullName);
                throw new ArgumentException(message, "contractType");
            }
            var data = new ServiceImplementationData
            {
                ConstructorString      = contractType.AssemblyQualifiedName,
                ServiceTypeToHost      = contractType,
                ImplementationResolver = l => l.ResolveComponent(new ResolveRequest(serviceToResolve, registration, parameters))
            };

            var behavior = new AutofacDependencyInjectionServiceBehavior(container, data);

            serviceHost.Description.Behaviors.Add(behavior);
        }
        public void Dispose_InstancesDisposed()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<DisposeTracker>();
            var container = builder.Build();

            var impData = new ServiceImplementationData()
            {
                ConstructorString = "TestService",
                ServiceTypeToHost = typeof(DisposeTracker),
                ImplementationResolver = l => l.Resolve<DisposeTracker>()
            };

            var context = new AutofacInstanceContext(container);
            var disposable = (DisposeTracker)context.Resolve(impData);
            Assert.IsFalse(disposable.IsDisposedPublic);
            context.Dispose();
            Assert.IsTrue(disposable.IsDisposedPublic);
        }
 public void Ctor_RequiresContainer()
 {
     var data = new ServiceImplementationData();
     Assert.Throws<ArgumentNullException>(() => new AutofacInstanceProvider(null, data));
 }
 public void Ctor_RequiresContainer()
 {
     var data = new ServiceImplementationData();
     Assert.Throws<ArgumentNullException>(() => new AutofacDependencyInjectionServiceBehavior(null, data));
 }