3

I am using Enterprise Library 6 and Unity v.3.5.0.0

The following error occurs:

Could not load type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSectionManageabilityProviderAttribute' from assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

when using this code to register multiple types with the container automatically by using a set of rules and conventions (Registration by Convention)

container.RegisterTypes(
    AllClasses.FromLoadedAssemblies()
    .Where(x => (x.IsPublic == true) &&
    (x.GetInterfaces().Any() == true) &&
    (x.IsAbstract == false) &&
    (x.IsClass == true) &&
    x.Namespace == "Company.Project.Data.DA.NW" ),
    WithMappings.FromAllInterfaces, type => (container.Registrations.Select(x => x.RegisteredType)
    .Any(r => type.GetInterfaces().Contains(r) == true) == true) ? WithName.TypeName(type) : WithName.Default(type),
    WithLifetime.Transient);

UPDATE:

The type:

Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSectionManageabilityProviderAttribute

is not a part of EntLib 6 anymore, but in Entlib 5.

Stacktrace:

[TypeLoadException: Could not load type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSectionManageabilityProviderAttribute' from assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.]
System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) +0
System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +145
System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) +162
System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) +87
System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) +438 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) +103
System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) +64
System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit) +87
System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) +13
System.Reflection.CustomAttributeExtensions.GetCustomAttribute(Assembly element) +57
Microsoft.Practices.Unity.AllClasses.IsSystemAssembly(Assembly a) +78 Microsoft.Practices.Unity.<>c__DisplayClass10.b__f(Assembly a) +72 System.Linq.WhereArrayIterator1.MoveNext() +48
System.Linq.<SelectManyIterator>d__14
2.MoveNext() +168
System.Linq.WhereEnumerableIterator1.MoveNext() +152
Microsoft.Practices.Unity.UnityContainerRegistrationByConventionExtensions.RegisterTypes(IUnityContainer container, IEnumerable
1 types, Func2 getFromTypes, Func2 getName, Func2 getLifetimeManager, Func2 getInjectionMembers, Boolean overwriteExistingMappings) +1323

Legends
  • 21,202
  • 16
  • 97
  • 123

1 Answers1

0

It's not really a solution but a workaround:

I simply replace the first parameter:

AllClasses.FromLoadedAssemblies()
    .Where(x => (x.IsPublic == true) &&
    (x.GetInterfaces().Any() == true) &&
    (x.IsAbstract == false) &&
    (x.IsClass == true) &&
    x.Namespace == "Company.Project.Data.DA.NW" )

and replaced it with a custom function (assFilter is a List of string with simple Assembly names and classFilter a list of namespaces)

var filteredAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => assFilter.Contains(a.FullName.Split(',')[0]));

    List<Type> allClasses = new List<Type>();
    foreach (var assembly in filteredAssemblies)
    {
        var classArray = assembly.GetTypes().Where(t => t.IsPublic &&
            !t.IsAbstract &&
            t.IsClass == true &&
            classFilter.Contains(t.Namespace));
        if (classArray != null && classArray.Count() > 0)
            allClasses.AddRange(classArray);
    }
Legends
  • 21,202
  • 16
  • 97
  • 123
  • Did you ever find an actual solution to this? – Dan Csharpster Jul 02 '20 at 21:36
  • 1
    Didn't use it long time, can't remember. I switched to maintained DI fx – Legends Jul 02 '20 at 21:48
  • 1
    Gotcha. Thanks! For the record, I had trouble when I tried to upgrade from EL 3.1 to 6 since that's all thats on nuget and we're switching to nuget packages. My solution was to publish EL 3.1 packages to a private feed and just reference those instead of upgrading. – Dan Csharpster Jul 02 '20 at 23:21