如何使用此库的所有引用添加对类库的引用?

isx*_*ker 2 .net c# dll dependencies .net-assembly

我试着解释我的问题.

我有自己的Windows服务(WS).这个WS引用Helpers.dll.Helpers.dll它是我自己的类库,有几个引用.其中一个参考是System.Web.Mvc.dll.

例如,如果我在Helpers.dll我的WS上添加引用,那么所有引用Helpers.dll都不会被添加到WS中.(在大多数情况下,这种行为是正确的,我认为)

我正在尝试Copy Local = TrueSyste.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>
Run Code Online (Sandbox Code Playgroud)

但是,没有任何改变.

如果没有放在Helpers.dll中的引用,我的WS无法正常工作. TypeInitializationException将被扔进rintume.(在尝试执行AppDomain.CurrentDomain.GetAssemblies()此程序集之一的静态构造函数中发生异常,Helpers.dll并且链接已启用System.Web.Mvc.dll)

如何使用此库的所有引用添加类库引用?

确切地说,我可以手动System.Web.Mvc.dllWS项目中添加这个references(),我想通过设置或.config文件或其他方式来实现.

实际上,我使用下面的代码进行动态解析汇编:

无论如何,在解析程序集的第一个使用类之后,我在WS中得到了异常AssemblyHelpers- 我使用的是WS(这只放在Helpers项目的bin文件夹中,而不是在WS的 bin foder中).Helpers.dllSystem.Web.Mvc.dlldll

但是我的WS也使用了一个AssemblyHelpers尝试加载的类System.Web.Mvc.dll(首先,自定义程序集解析器加载Helpers.dll,然后加载所有引用dll,包括System.Web.Mvc.dll),并得到一个Exception(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));
    }

}
Run Code Online (Sandbox Code Playgroud)

Nip*_*ris 6

如何使用此库的所有引用添加对类库的引用?

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

我认为真正的问题是将依赖程序集部署到构建文件夹的过程.尝试在问题中使用一些建议:MSBuild不会获取引用项目的引用

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