示例#1
0
        public void Register(ServiceRegistration registration)
        {
            if (registration == null)
                throw new ArgumentNullException("registration");

            if (container == null)
                throw new InvalidOperationException("The container was not initialized.");

            try {
                lock (this) {
                    var serviceType = registration.ServiceType;
                    var service = registration.Instance;
                    var serviceName = registration.ServiceKey;
                    var implementationType = registration.ImplementationType;

                    var reuse = Reuse.Singleton;
                    if (!String.IsNullOrEmpty(ScopeName))
                        reuse = Reuse.InCurrentNamedScope(ScopeName);

                    if (!String.IsNullOrEmpty(registration.Scope))
                        reuse = Reuse.InCurrentNamedScope(registration.Scope);

                    if (service == null) {
                        container.Register(serviceType, implementationType, serviceKey: serviceName, reuse: reuse);
                    } else {
                        container.RegisterInstance(serviceType, service, serviceKey: serviceName, reuse: reuse);
                    }
                }
            } catch (Exception ex) {
                throw new Exception("Error when registering service.", ex);
            }
        }
示例#2
0
        public SystemBuilder Use(ServiceUseOptions options)
        {
            if (options == null)
                throw new ArgumentNullException("options");

            options.Validate();

            var key = options.Key;
            var type = options.ServiceType;
            var policy = options.Policy;

            var registration = new ServiceRegistration(type, options.ImplementationType) {
                Instance = options.Instance,
                Scope = options.Scope,
                ServiceKey = key
            };

            if (key != null) {
                if (policy == ServiceUsePolicy.Replace) {
                    if (ServiceContainer.IsRegistered(type, key))
                        ServiceContainer.Unregister(type, key);
                }

                ServiceContainer.Register(registration);
            } else {
                if (policy == ServiceUsePolicy.Replace) {
                    if (ServiceContainer.IsRegistered(type))
                        ServiceContainer.Unregister(type);
                }

                ServiceContainer.Register(registration);
            }

            return this;
        }