private static void Register(
            this PocketContainer container,
            ServiceDescriptor descriptor)
        {
            if (descriptor.ImplementationInstance != null)
            {
                container.RegisterSingle(
                    descriptor.ServiceType,
                    c => descriptor.ImplementationInstance);
                return;
            }

            if (descriptor.ImplementationFactory != null)
            {
                if (descriptor.Lifetime != ServiceLifetime.Singleton)
                {
                    container.Register(
                        descriptor.ServiceType,
                        c => descriptor.ImplementationFactory(c));
                }
                else
                {
                    container.RegisterSingle(
                        descriptor.ServiceType,
                        c => descriptor.ImplementationFactory(c));
                }
                return;
            }

            if (descriptor.ImplementationType != null)
            {
                if (descriptor.IsOpenGeneric())
                {
                    container.RegisterGeneric(
                        variantsOf: descriptor.ServiceType,
                        to: descriptor.ImplementationType);
                }
                else if (descriptor.ServiceType == descriptor.ImplementationType)
                {
                    // no need to register it
                }
                else
                {
                    if (descriptor.Lifetime != ServiceLifetime.Singleton)
                    {
                        container.Register(
                            descriptor.ServiceType,
                            c => c.Resolve(descriptor.ImplementationType));
                    }
                    else
                    {
                        container.RegisterSingle(
                            descriptor.ServiceType,
                            c => c.Resolve(descriptor.ImplementationType));
                    }
                }
            }
        }
Example #2
0
        public void ConfigureApplication(IAppBuilder app, string nodeId, IObservable<IEnumerable<Peer>> peerObserver)
        {
            thisServersNode = new Node(new NodeSettings(nodeId,
                                                      TimeSpan.FromSeconds(5),
                                                      TimeSpan.FromSeconds(1),
                                                      peerObserver));

            var config = new HttpConfiguration();
            var pocket = new PocketContainer();
            config.ResolveDependenciesUsing(pocket);
            pocket.RegisterSingle(c => thisServersNode);
            config.MapHttpAttributeRoutes();
            JsonSerialization(config);
            app.UseWebApi(config);
            thisServersNode.Start();
        }
        public static IServiceProvider AsServiceProvider(
            this PocketContainer container,
            IServiceCollection services)
        {
            foreach (var service in services)
            {
                Register(container, service);
            }

            container.RegisterSingle <IServiceProvider>(c => container)
            .RegisterSingle <IServiceScopeFactory>(c => new ServiceScopeFactory(c));

            container.AfterResolve += container.RegisterSingletonForDisposal;

            container.OnFailedResolve = (type, exception) => null;

            return(container);
        }