简体   繁体   中英

How to add a reference to a class library with all references of this library?

I try to explain my issue.

I have my own windows service( WS ). This WS refers on Helpers.dll . Helpers.dll it's my own class library that has several references. One of this references is System.Web.Mvc.dll .

For example if I'm adding reference on Helpers.dll to my WS , all references from Helpers.dll won't be added to WS .(At most situations this behavior is true, I think)

I'm trying to set Copy Local = True inside properties of reference on 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>

Nothing changes, though.

Without references that placed in Helpers.dll my WS doesn't work correctly. TypeInitializationException will been thrown in the rintume. (Exception happens inside static constructor that tries to execute AppDomain.CurrentDomain.GetAssemblies() method one of this assemblies is Helpers.dll with link on System.Web.Mvc.dll )

How to add a class library reference with all references of this library?

Exactly, i can to add this references ( System.Web.Mvc.dll ) in WS project by hand, I want to do this by settings or .config file or somethig else.

Actually, I use code below for dynamic resolving assembly:

Any way, I get the exception inside WS after the first using AssemblyHelpers class during resolving assemblies - my WS uses Helpers.dll that uses System.Web.Mvc.dll (this dll is placed only in bin folder of Helpers project and isn't palced in bin foder of WS ).

But my WS also uses a AssemblyHelpers class that tries to load System.Web.Mvc.dll (first, custom assembly resolver loads Helpers.dll and then all references from this dll , including System.Web.Mvc.dll ) and gets a Exception( Description: The process was terminated due to an unhandled exception.Exception Info: System.TypeInitializationException ). \\

System.Web.Mvc.dll isn't in bin folder of my WS .

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));
    }

}

How to add a reference to a class library with all references of this library?

You shouldn't add every transitive dependency you have as a assembly reference. Reference is required to import types from another assembly and make it usable in your code.

I think the real problem is the process of deploying the dependent assemblies to the build folder. Try to use some advice in the question: MSBuild doesn't pick up references of the referenced project

The modern way to deal with transitive dependencies (to deploy it correctly to every dependent project) is the using of package manager. NuGet is the de-facto standard for this purpose. Also a lot of innovations in DNX project system are intended to solve such issues and to use NuGet packages as the first-class build and deploy entities.

First, I want to thank @Nipheris for the answer and proposal to use Nuget. Using of Nuget really helps me.

Any way, i've been getting the error. I think try to use unity instead custom dependency resolver( AssemblyHelper class is part of that) helper. In my opinion, the using of custom dependency resolver has been getting me error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM