我試圖解釋我的問題。如何使用該庫的所有引用來添加對類庫的引用?
我有我自己的windows服務(WS)。此WS參考Helpers.dll
。 Helpers.dll
它是我自己的類庫有幾個引用。其中一個參考文獻是System.Web.Mvc.dll
。
例如如果我對Helpers.dll
將參照我WS,從Helpers.dll
所有引用不會被添加到WS。(在大多數情況下,這種行爲是真實的,我認爲)
我想設定的基準Copy Local = True
內部性能Syste.Web.Mvc.dll
(Helpers.csproj
)
<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.dll
與System.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.dll
)System.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));
}
}
當然,Nuget確實幫了我 – isxaker
我同意,你應該使用NuGet或其他軟件包管理器來管理你的依賴關係。如果您確實必須包含其他程序集,則可以嘗試類似ILMerge http://www.microsoft.com/en-us/download/details.aspx?id=17630 – solidau