Unity v3 - RegistrationByConvention - 无法加载类型异常

时间:2015-04-28 10:48:13

标签: c# unity-container aop enterprise-library-6

我正在使用Enterprise Library 6和Unity v.3.5.0.0

发生以下错误:

  

无法加载类型   ' Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSectionManageabilityProviderAttribute'   来自assembly' Microsoft.Practices.EnterpriseLibrary.Common,   Version = 6.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'。

使用此代码通过一组规则和约定(按约定注册)自动向容器注册多个类型

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);

更新

类型:

  

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

不再是EntLib 6的一部分,而是在Entlib 5中。

堆栈跟踪:

  

[TypeLoadException:无法加载类型   ' Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSectionManageabilityProviderAttribute'   来自assembly' Microsoft.Practices.EnterpriseLibrary.Common,   Version = 6.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'。]
  System.ModuleHandle.ResolveType(RuntimeModule模块,Int32 typeToken,   IntPtr * typeInstArgs,Int32 typeInstCount,IntPtr * methodInstArgs,   Int32 methodInstCount,ObjectHandleOnStack type)+0
  System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule模块,   Int32 typeToken,RuntimeTypeHandle [] typeInstantiationContext,   RuntimeTypeHandle [] methodInstantiationContext)+ 1455   System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken,   键入[] genericTypeArguments,Type [] genericMethodArguments)+162
  System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord   caRecord,MetadataImport范围,汇编& lastAptcaOkAssembly,   RuntimeModule decoratedModule,MetadataToken decoratedToken,   RuntimeType attributeFilterType,Boolean mustBeInheritable,Object []   属性,IList derivedAttributes,RuntimeType&属性类型,   IRuntimeMethodInfo&安培; ctor,布尔& 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(类型   attributeType,布尔继承)+64
  System.Attribute.GetCustomAttributes(Assembly元素,Type   attributeType,布尔继承)+87
  System.Attribute.GetCustomAttribute(Assembly元素,Type   attributeType,布尔继承)+13
  System.Reflection.CustomAttributeExtensions.GetCustomAttribute(大会   元素)+57
  Microsoft.Practices.Unity.AllClasses.IsSystemAssembly(程序集a)+78   Microsoft.Practices.Unity<> c__DisplayClass10.b__f(大会   a)+72 System.Linq.WhereArrayIterator 1.MoveNext() +48
System.Linq.<SelectManyIterator>d__14
2.MoveNext()+168
  System.Linq.WhereEnumerableIterator 1.MoveNext() +152
Microsoft.Practices.Unity.UnityContainerRegistrationByConventionExtensions.RegisterTypes(IUnityContainer container, IEnumerable
1种类型,Func 2 getFromTypes, Func 2 getName,   Func 2 getLifetimeManager, Func 2 getInjectionMembers,布尔值   overwriteExistingMappings)+1323

1 个答案:

答案 0 :(得分:0)

它不是一个真正的解决方案,而是一种解决方法:

我只是替换第一个参数:

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

并将其替换为自定义函数(assFilter是一个包含简单程序集名称的字符串列表,而classFilter是一个名称空间列表)

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);
    }