/// <inheritdoc />
        public void Validate(IDiManagerElement diManagerElement)
        {
            var diManagerAssembly = diManagerElement.DiManager.GetType().Assembly;

            var     diManagerAssemblyVersion = diManagerAssembly.GetName().Version;
            Version minSupportedVersion      = null;

            var diManagerTypeName = diManagerElement.DiManager.GetType().FullName;

            switch (diManagerTypeName)
            {
            case "IoC.Configuration.Autofac.AutofacDiManager":
                minSupportedVersion = new Version(2, 0, 0, 0);
                break;

            case "IoC.Configuration.Ninject.NinjectDiManager":
                minSupportedVersion = new Version(2, 0, 0, 0);
                break;
            }

            if (minSupportedVersion != null && diManagerAssemblyVersion < minSupportedVersion)
            {
                throw new ConfigurationParseException(diManagerElement,
                                                      string.Format("'{0}, {1}' is not compatible with '{2}, {3}'. Minimum compatible version for '{2}, {3}' is '{0}, {4}'. Please get a newer version of '{0}' from 'https://www.nuget.org'.",
                                                                    diManagerAssembly.GetName().Name, diManagerAssemblyVersion,
                                                                    GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version,
                                                                    minSupportedVersion));
            }
        }
        public override void AddChild(IConfigurationFileElement child)
        {
            base.AddChild(child);

            if (child is IDiManagerElement)
            {
                var diManagerElement = (IDiManagerElement)child;

                if (_diManagerNameToDiManagerMap.ContainsKey(diManagerElement.Name))
                {
                    throw new ConfigurationParseException(diManagerElement, $"Multiple occurrences of '{ConfigurationFileElementNames.DiManager}' elements with the same value for attribute '{ConfigurationFileAttributeNames.Name}'. The value of attibute is '{diManagerElement.Name}'.", this);
                }

                _diManagerNameToDiManagerMap[diManagerElement.Name] = diManagerElement;
                if (_activeDiManagerName.Equals(diManagerElement.Name, StringComparison.OrdinalIgnoreCase))
                {
                    ActiveDiManagerElement = diManagerElement;
                }
            }
        }
예제 #3
0
        public override void ValidateAfterChildrenAdded()
        {
            base.ValidateAfterChildrenAdded();

            Type validBaseType = null;

            if (typeof(IDiModule).IsAssignableFrom(_typeInfo.Type))
            {
                validBaseType = typeof(IDiModule);
            }
            else if (_configuration.DiManagers.ActiveDiManagerElement.DiManager.ModuleType.IsAssignableFrom(_typeInfo.Type))
            {
                validBaseType = _configuration.DiManagers.ActiveDiManagerElement.DiManager.ModuleType;
            }
            else
            {
                _isDiManagerInactive = true;

                // If this is a native module, lets see if there is any non-active dependency manager module,
                // for which the type is correct.

                IDiManagerElement ownerDiManagerElement = null;

                var validModuleTypes = new List <Type>();
                foreach (var diManagerElement in _configuration.DiManagers.AllDiManagers)
                {
                    validModuleTypes.Add(diManagerElement.DiManager.ModuleType);

                    if (diManagerElement == _configuration.DiManagers.ActiveDiManagerElement)
                    {
                        continue;
                    }

                    if (diManagerElement.DiManager.ModuleType.IsAssignableFrom(_typeInfo.Type))
                    {
                        ownerDiManagerElement = diManagerElement;
                        validBaseType         = diManagerElement.DiManager.ModuleType;
                        break;
                    }
                }

                if (ownerDiManagerElement == null)
                {
                    var errorMessage = new StringBuilder();
                    errorMessage.Append($"Invalid type for module: '{_typeInfo.TypeCSharpFullName}'.The type used as a module should be one of the following types or a subclass of any of these types: '{typeof(IDiModule).FullName}'");

                    foreach (var valdModuleType in validModuleTypes)
                    {
                        errorMessage.Append($", '{valdModuleType.FullName}'");
                    }

                    errorMessage.Append(".");

                    throw new ConfigurationParseException(this, errorMessage.ToString());
                }

                LogHelper.Context.Log.DebugFormat("Note, the module '{0}' is disabled since dependency injection manager '{1}' that handles the module is not the active dependency injection manager. The active dependency injection manager is '{2}'.",
                                                  _typeInfo.TypeCSharpFullName, ownerDiManagerElement.Name, _configuration.DiManagers.ActiveDiManagerElement.Name);
            }

            if (validBaseType != null && !_isDiManagerInactive && this.Enabled)
            {
                DiModule = _createInstanceFromTypeAndConstructorParameters.CreateInstance(this, validBaseType, _typeInfo.Type, _parameters?.AllParameters ?? new IParameterElement[0]);
                LogHelper.Context.Log.InfoFormat("Created an instance of dependency injection module: {0}.", _typeInfo.TypeCSharpFullName);
            }
        }