Пример #1
0
        private IResolveServices RegisterServices(ICollection <IServiceRegistration> serviceRegistrations, IContainer container, IDictionary <string, object> environment)
        {
            var serviceResolver = new StructuremapServiceResolver(container);

            var assemblies = environment.GetAssemblies();

            container.Configure(x =>
            {
                x.For <IResolveServices>().Use(serviceResolver);
                x.For <IDictionary <string, object> >().Use(environment);

                foreach (var normalRegistration in serviceRegistrations.OfType <NormalRegistration>())
                {
                    var registration = x.For(normalRegistration.ServiceType).Use(normalRegistration.ImplimentationType);

                    SetLifecycle(registration, normalRegistration.Lifecycle);
                }

                foreach (var dynamicRegistration in serviceRegistrations.OfType <DynamicRegistration>())
                {
                    var registration = x.For(dynamicRegistration.ServiceType).Use(y => dynamicRegistration.FindService(dynamicRegistration.ServiceType, new ContextServiceResolver(y)));

                    SetLifecycle(registration, dynamicRegistration.Lifecycle);
                }

                foreach (var instanceRegistration in serviceRegistrations.OfType <InstanceRegistration>())
                {
                    x.For(instanceRegistration.ServiceType).Use(instanceRegistration.Instance);
                }

                x.Scan(y =>
                {
                    foreach (var assembly in assemblies)
                    {
                        y.Assembly(assembly);
                    }

                    foreach (var scanRegistration in serviceRegistrations.OfType <ScanRegistration>())
                    {
                        y.AddAllTypesOf(scanRegistration.ScanType);
                    }

                    foreach (var genericScanRegistration in serviceRegistrations.OfType <GenericInterfaceScanRegistration>())
                    {
                        y.ConnectImplementationsToTypesClosing(genericScanRegistration.ScanType);
                    }
                });
            });

            return(serviceResolver);
        }
Пример #2
0
        public async Task Invoke(IDictionary <string, object> environment)
        {
            var parentContainer = environment.GetStructuremapContainer();

            using (var container = parentContainer.GetNestedContainer())
            {
                var resolver = new StructuremapServiceResolver(container);

                container.Configure(x =>
                {
                    x.For <IResolveServices>().Use(resolver);
                    x.For <IDictionary <string, object> >().Use(environment);
                });

                environment[StructuremapEnvironmentExtensions.StructuremapEnvironmentKeys.ContainerKey] = container;
                environment[SetupIocConfiguration.ServiceResolverKey] = resolver;

                await _next(environment).ConfigureAwait(false);
            }
        }