我试图解释我的问题。如何使用该库的所有引用来添加对类库的引用?
我有我自己的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