Example #1
0
        /// <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>
        public static void AddDependencyInjectionBehavior(this ServiceHostBase serviceHost, Type contractType, ILifetimeScope container)
        {
            if (contractType == null)
            {
                throw new ArgumentNullException("contractType");
            }
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

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

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

            IComponentRegistration registration;

            if (!container.ComponentRegistry.TryGetRegistration(new TypedService(contractType), out registration))
            {
                var message = string.Format(ServiceHostExtensionsResources.ContractTypeNotRegistered, contractType.FullName);
                throw new ArgumentException(message, "contractType");
            }

            var behavior = new AutofacDependencyInjectionServiceBehavior(container, serviceHost.Description.ServiceType, registration);

            serviceHost.Description.Behaviors.Add(behavior);
        }
        /// <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 ApplyDispatchBehavior_NullServiceHost()
 {
     var provider = new AutofacDependencyInjectionServiceBehavior(new ContainerBuilder().Build(), new ServiceImplementationData());
     var description = new ServiceDescription();
     Assert.Throws<ArgumentNullException>(() => provider.ApplyDispatchBehavior(description, null));
 }