2015-09-11 61 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類是其中的一部分)助手。在我看來,使用自定義依賴解析器一直讓我錯誤。

相關問題