2015-09-11 51 views
1

我试图解释我的问题。如何使用该库的所有引用来添加对类库的引用?

我有我自己的windows服务(WS)。此WS参考Helpers.dllHelpers.dll它是我自己的类库有几个引用。其中一个参考文献是System.Web.Mvc.dll

例如如果我对Helpers.dll将参照我WS,从Helpers.dll所有引用不会被添加到WS。(在大多数情况下,这种行为是真实的,我认为)

我想设定的基准Copy Local = True内部性能Syste.Web.Mvc.dllHelpers.csproj

enter image description here

<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> 
     <Private>True</Private> 
</Reference> 

虽然没有什么变化。

未放置在Helpers.dll中的引用我的WS无法正常工作。 TypeInitializationException将被扔在rintume。 (例外情况,试图执行AppDomain.CurrentDomain.GetAssemblies()方法这个组件之一是Helpers.dllSystem.Web.Mvc.dll链接静态构造函数内)

如何添加与该库的所有引用类库引用?

没错,我可以用手在WS项目添加此引用(System.Web.Mvc.dll),我想通过设置或.config文件或其他somethig做到这一点。

其实,我用下面的代码进行动态解析组件:

任何方式,我用AssemblyHelpers类进去WS异常后先在解决组件 - 我WS使用Helpers.dll使用System.Web.Mvc.dll(这个dll仅被放置在bin Helpers项目的文件夹中,并且不在WS的bin编码中。

但我WS还采用了AssemblyHelpers类试图加载(从这个dll第一,自定义程序集解析器负荷Helpers.dll,然后所有引用,包括System.Web.Mvc.dllSystem.Web.Mvc.dll并得到一个异常(Description: The process was terminated due to an unhandled exception.Exception Info: System.TypeInitializationException)。 \

System.Web.Mvc.dll不在我的WS的bin文件夹中。

public static class AssemblyHelper 
{ 
    private static readonly object _syncLock = new object(); 

    private static readonly List<Assembly> _assemblies = new List<Assembly>(); 

    public static ICollection<Assembly> SyncronizedList 
    { 
     get 
     { 
      lock (_syncLock) 
      { 
       return new List<Assembly>(_assemblies); 
      } 
     } 
    } 

    static AssemblyHelper() 
    { 
     AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(OnAssemblyLoad); 
     AppDomain.CurrentDomain.DomainUnload += new EventHandler(OnDomainUnload); 

     // explicitly register previously loaded assemblies since they was not registered yet 
     foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) 
     { 
      RegisterAssembly(assembly); 
     } 
    } 

    private static void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args) 
    { 
     if (RegisterAssembly(args.LoadedAssembly)) 
     { 
      ExecuteAllExecuteOnAssemblyLoadMethods(args.LoadedAssembly); 
     } 
    } 

    private static void OnDomainUnload(object sender, EventArgs args) 
    { 
     foreach (Assembly assembly in SyncronizedList) 
     { 
      ExecuteAllExecuteOnDomainUnloadMethods(assembly); 
     } 
    } 

    public static bool RegisterAssembly(Assembly assembly) 
    { 
     if (assembly == null) 
     { 
      throw new ArgumentNullException("assembly"); 
     } 
     lock (_syncLock) 
     { 
      if (_assemblies.Contains(assembly)) 
      { 
       return false; 
      } 
      else 
      { 
       _assemblies.Add(assembly); 
       ExecuteAllExecuteOnAssemblyLoadMethods(assembly); 
       return true; 
      } 
     } 
    } 

    private static void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args) 
    { 
     if (RegisterAssembly(args.LoadedAssembly)) 
     { 
      ExecuteAllExecuteOnAssemblyLoadMethods(args.LoadedAssembly); 
     } 
    } 

    private static void OnDomainUnload(object sender, EventArgs args) 
    { 
     foreach (Assembly assembly in SyncronizedList) 
     { 
      ExecuteAllExecuteOnDomainUnloadMethods(assembly); 
     } 
    } 

    public static ICollection<MethodInfo> FindAllMethods(Assembly assembly, Type attributeType) 
    { 
     String assemblyName = "unknown"; 
     String attributeTypeName = String.Empty; 
     try 
     { 
      if (assembly == null) 
      { 
       throw new ArgumentNullException("assembly"); 
      } 
      assemblyName = assembly.FullName; 
      if (attributeType == null) 
      { 
       throw new ArgumentNullException("attributeType"); 
      } 
      attributeTypeName = attributeType.FullName; 
      List<MethodInfo> lst = new List<MethodInfo>(); 
      foreach (Type type in assembly.GetTypes()) 
      { 
       foreach (MethodInfo mi in type.GetMethods(
        BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) 
       { 
        if (Attribute.IsDefined(mi, attributeType)) 
        { 
         lst.Add(mi); 
        } 
       } 
      } 
      return lst; 
     } 
     catch (Exception ex) 
     { 
      //exception 
     } 
    } 

    public static int ExecuteAllMethods(Assembly assembly, Type attributeType) 
    { 
     int count = 0; 
     foreach (MethodInfo mi in FindAllMethods(assembly, attributeType)) 
     { 
      try 
      { 
       mi.Invoke(null, null); 
       count++; 
      } 
      catch (Exception ex) 
      { 
       Trace.WriteLine(string.Format("Failed to execute method {0} of {1}, reason: {2}", 
        mi.Name, mi.DeclaringType, Converter.GetExceptionMessageRecursive(ex))); 
      } 
     } 
     return count; 
    } 


    public static ICollection<MethodInfo> FindAllExecuteOnAssemblyLoadMethods(Assembly assembly) 
    { 
     return FindAllMethods(assembly, typeof(Attributes.ExecuteOnAssemblyLoadAttribute)); 
    } 

    public static ICollection<MethodInfo> FindAllExecuteOnDomainUnloadMethods(Assembly assembly) 
    { 
     return FindAllMethods(assembly, typeof(Attributes.ExecuteOnDomainUnloadAttribute)); 
    } 

    public static int ExecuteAllExecuteOnAssemblyLoadMethods(Assembly assembly) 
    { 
     return ExecuteAllMethods(assembly, typeof(Attributes.ExecuteOnAssemblyLoadAttribute)); 
    } 

    public static int ExecuteAllExecuteOnDomainUnloadMethods(Assembly assembly) 
    { 
     return ExecuteAllMethods(assembly, typeof(Attributes.ExecuteOnDomainUnloadAttribute)); 
    } 

} 

回答

5

如何使用这个库的所有引用添加到类库的引用?

您不应该添加每个传递依赖项作为程序集引用。需要引用从另一个程序集导入类型并使其在代码中可用。

我认为真正的问题是将相关程序集部署到构建文件夹的过程。尝试在问题中使用一些建议:MSBuild doesn't pick up references of the referenced project

处理传递性依赖关系(将其正确部署到每个相关项目)的现代方法是使用包管理器。 NuGet是实现此目的的事实标准。此外,DNX项目系统中的许多创新旨在解决这些问题,并将NuGet包用作一流的构建和部署实体。

+0

当然,Nuget确实帮了我 – isxaker

+1

我同意,你应该使用NuGet或其他软件包管理器来管理你的依赖关系。如果您确实必须包含其他程序集,则可以尝试类似ILMerge http://www.microsoft.com/en-us/download/details.aspx?id=17630 – solidau

0

首先,我想感谢@Nipheris的答案和使用Nuget的建议。使用Nuget真的帮助我。

无论如何,我一直在收到错误。我认为尝试使用统一,而不是自定义依赖解析器(AssemblyHelper类是其中的一部分)助手。在我看来,使用自定义依赖解析器一直让我错误。

相关问题