Пример #1
0
        /// <summary>
        /// Adds the custom service behavior required for dependency injection.
        /// </summary>
        /// <param name="serviceHost">The service host.</param>
        /// <param name="serviceType">The service contract type.</param>
        /// <param name="provider">The service provider.</param>
        public static void AddDependencyInjectionBehavior(this ServiceHostBase serviceHost, Type serviceType, IServiceProvider provider)
        {
            if (serviceHost == null)
            {
                throw new ArgumentNullException(nameof(serviceHost));
            }
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

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

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

            var data = new ServiceImplementationData()
            {
                ConstructorString      = serviceType.AssemblyQualifiedName,
                ServiceTypeToHost      = serviceType,
                ImplementationResolver = l => l.GetRequiredService(serviceType)
            };

            var behavior = new DependencyInjectionServiceBehavior(provider, data);

            serviceHost.Description.Behaviors.Add(behavior);
        }
        /// <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="ArgumentNullException">
        /// Thrown if <paramref name="serviceData" /> is <see langword="null" />.
        /// </exception>
        public object Resolve(ServiceImplementationData serviceData)
        {
            if (serviceData == null)
            {
                throw new ArgumentNullException(nameof(serviceData));
            }

            return(serviceData.ImplementationResolver(OperationLifetime.ServiceProvider));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DependencyInjectionInstanceProvider"/> class.
        /// </summary>
        /// <param name="provider">
        /// 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="provider" /> or <paramref name="serviceData" /> is <see langword="null" />.
        /// </exception>
        public DependencyInjectionInstanceProvider(IServiceProvider provider, ServiceImplementationData serviceData)
        {
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }
            if (serviceData == null)
            {
                throw new ArgumentNullException(nameof(serviceData));
            }

            this.provider    = provider;
            this.serviceData = serviceData;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DependencyInjectionServiceBehavior"/> class.
 /// </summary>
 /// <param name="provider">
 /// 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="ArgumentNullException">
 /// Thrown if <paramref name="provider" /> or <paramref name="serviceData" /> is <see langword="null" />.
 /// </exception>
 public DependencyInjectionServiceBehavior(IServiceProvider provider, ServiceImplementationData serviceData)
 {
     this.provider    = provider ?? throw new ArgumentNullException(nameof(provider));
     this.serviceData = serviceData ?? throw new ArgumentNullException(nameof(serviceData));
 }