Пример #1
0
 public NinjectServiceScope(
     IResolutionRoot resolver,
     IEnumerable <IParameter> inheritedParameters)
 {
     _scope = new ScopeParameter();
     inheritedParameters = inheritedParameters.AddOrReplaceScopeParameter(_scope);
     _serviceProvider    = new NinjectServiceProvider(resolver, inheritedParameters.ToArray());
 }
 internal static IEnumerable <IParameter> AddOrReplaceScopeParameter(
     this IEnumerable <IParameter> parameters,
     ScopeParameter scopeParameter)
 {
     return(parameters
            .Where(p => p.Name != typeof(ScopeParameter).FullName)
            .Concat(new[] { scopeParameter }));
 }
 public NinjectServiceScope(
     IResolutionRoot resolver,
     IEnumerable<IParameter> inheritedParameters)
 {
     _scope = new ScopeParameter();
     inheritedParameters = inheritedParameters.AddOrReplaceScopeParameter(_scope);
     _serviceProvider = new NinjectServiceProvider(resolver, inheritedParameters.ToArray());
 }
Пример #4
0
        public override void Load()
        {
            foreach (var descriptor in _serviceDescriptors)
            {
                IBindingWhenInNamedWithOrOnSyntax <object> binding;

                if (descriptor.ImplementationType != null)
                {
                    binding = Bind(descriptor.ServiceType).To(descriptor.ImplementationType);
                }
                else if (descriptor.ImplementationFactory != null)
                {
                    binding = Bind(descriptor.ServiceType).ToMethod(context =>
                    {
                        var serviceProvider = context.Kernel.Get <IServiceProvider>();
                        return(descriptor.ImplementationFactory(serviceProvider));
                    });
                }
                else
                {
                    binding = Bind(descriptor.ServiceType).ToConstant(descriptor.ImplementationInstance);
                }

                switch (descriptor.Lifetime)
                {
                case ServiceLifetime.Singleton:
                    binding.InSingletonScope();
                    break;

                case ServiceLifetime.Scoped:
                    binding.InRequestScope();
                    break;

                case ServiceLifetime.Transient:
                    binding.InTransientScope();
                    break;
                }
            }

            Bind <IServiceProvider>().ToMethod(context =>
            {
                var resolver        = context.Kernel.Get <IResolutionRoot>();
                var inheritedParams = context.Parameters.Where(p => p.ShouldInherit);

                var scopeParam  = new ScopeParameter();
                inheritedParams = inheritedParams.AddOrReplaceScopeParameter(scopeParam);

                return(new NinjectServiceProvider(resolver, inheritedParams.ToArray()));
            }).InRequestScope();

            Bind <IServiceScopeFactory>().ToMethod(context =>
            {
                return(new NinjectServiceScopeFactory(context));
            }).InRequestScope();
        }
        public override void Load()
        {
            foreach (var descriptor in _serviceDescriptors)
            {
                IBindingWhenInNamedWithOrOnSyntax<object> binding;

                if (descriptor.ImplementationType != null)
                {
                    binding = Bind(descriptor.ServiceType).To(descriptor.ImplementationType);
                }
                else if (descriptor.ImplementationFactory != null)
                {
                    binding = Bind(descriptor.ServiceType).ToMethod(context =>
                    {
                        var serviceProvider = context.Kernel.Get<IServiceProvider>();
                        return descriptor.ImplementationFactory(serviceProvider);
                    });
                }
                else
                {
                    binding = Bind(descriptor.ServiceType).ToConstant(descriptor.ImplementationInstance);
                }

                switch (descriptor.Lifetime)
                {
                    case ServiceLifetime.Singleton:
                        binding.InSingletonScope();
                        break;
                    case ServiceLifetime.Scoped:
                        binding.InRequestScope();
                        break;
                    case ServiceLifetime.Transient:
                        binding.InTransientScope();
                        break;
                }
            }

            Bind<IServiceProvider>().ToMethod(context =>
            {
                var resolver = context.Kernel.Get<IResolutionRoot>();
                var inheritedParams = context.Parameters.Where(p => p.ShouldInherit);

                var scopeParam = new ScopeParameter();
                inheritedParams = inheritedParams.AddOrReplaceScopeParameter(scopeParam);

                return new NinjectServiceProvider(resolver, inheritedParams.ToArray());
            }).InRequestScope();

            Bind<IServiceScopeFactory>().ToMethod(context =>
            {
                return new NinjectServiceScopeFactory(context);
            }).InRequestScope();
        }
            public NinjectServiceScope(
                IResolutionRoot resolver,
                IEnumerable<IParameter> inheritedParameters)
            {
                _scope = new ScopeParameter();
                inheritedParameters = inheritedParameters.AddOrReplaceScopeParameter(_scope);
                _serviceProvider = new NinjectServiceProvider(resolver, inheritedParameters.ToArray());

                var httpContext = resolver.Get<IHttpContextAccessor>().HttpContext;

                if (httpContext != null)
                {
                    httpContext.Items.Add(typeof(ScopeParameter).FullName, _scope);
                }
            }
            public NinjectServiceScope(
                IResolutionRoot resolver,
                IEnumerable <IParameter> inheritedParameters)
            {
                _scope = new ScopeParameter();
                inheritedParameters = inheritedParameters.AddOrReplaceScopeParameter(_scope);
                _serviceProvider    = new NinjectServiceProvider(resolver, inheritedParameters.ToArray());

                var httpContext = resolver.Get <IHttpContextAccessor>().HttpContext;

                if (httpContext != null)
                {
                    httpContext.Items.Add(typeof(ScopeParameter).FullName, _scope);
                }
            }