title: How to add a reference to a class library with all references of this library?
body: <p>I try to explain my issue.</p>
  
  <p>I have my own windows service(<strong>WS</strong>). This <strong>WS</strong> refers on <code>Helpers.dll</code>. <code>Helpers.dll</code> it's my own <em>class library</em> that has several references. One of this references is <code>System.Web.Mvc.dll</code>.</p>
  
  <p>For example if I'm adding reference on <code>Helpers.dll</code> to my <strong>WS</strong>, all references from <code>Helpers.dll</code> won't be added to <strong>WS</strong>.(At most situations this behavior is true, I think)</p>
  
  <p>I'm trying to set <code>Copy Local = True</code> inside properties of reference on <code>Syste.Web.Mvc.dll</code> (<code>Helpers.csproj</code>)</p>
  
  <p><a href="https://i.stack.imgur.com/N6jzi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N6jzi.png" alt="enter image description here"></a></p>
  
  <pre><code>&lt;Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"&gt;
        &lt;Private&gt;True&lt;/Private&gt;
  &lt;/Reference&gt;
  </code></pre>
  
  <p>Nothing changes, though.</p>
  
  <p>Without references that placed in Helpers.dll my <strong>WS</strong> doesn't work correctly.
  <code>TypeInitializationException</code> will been thrown in the rintume. (Exception happens inside static constructor that tries to execute <code>AppDomain.CurrentDomain.GetAssemblies()</code> method one of this assemblies is <code>Helpers.dll</code> with link on <code>System.Web.Mvc.dll</code>)</p>
  
  <p>How to add a class library reference with all references of this library?</p>
  
  <p>Exactly, i can to add this references (<code>System.Web.Mvc.dll</code>) in <strong>WS</strong> project by hand, I want to do this by settings or <code>.config</code> file or somethig else.</p>
  
  <p>Actually, I use code below for dynamic resolving assembly:</p>
  
  <p>Any way, I get the exception inside <strong>WS</strong> after the first using <code>AssemblyHelpers</code> class during resolving assemblies - my <strong>WS</strong> uses <code>Helpers.dll</code> that uses <code>System.Web.Mvc.dll</code>(this <code>dll</code> is placed only in <em>bin</em> folder of Helpers project and isn't palced in bin foder of <strong>WS</strong>). </p>
  
  <p>But my <strong>WS</strong> also uses a <code>AssemblyHelpers</code> class that tries to load <code>System.Web.Mvc.dll</code>(first, custom assembly resolver loads <code>Helpers.dll</code> and then all references from this <code>dll</code>, including <code>System.Web.Mvc.dll</code>) and gets a Exception(<code>Description: The process was terminated due to an unhandled exception.Exception Info: System.TypeInitializationException</code>). \</p>
  
  <p><code>System.Web.Mvc.dll</code> isn't in bin folder of my <strong>WS</strong>.</p>
  
  <pre><code>public static class AssemblyHelper
  {
      private static readonly object _syncLock = new object();
  
      private static readonly List&lt;Assembly&gt; _assemblies = new List&lt;Assembly&gt;();
  
      public static ICollection&lt;Assembly&gt; SyncronizedList
      {
          get
          {
              lock (_syncLock)
              {
                  return new List&lt;Assembly&gt;(_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&lt;MethodInfo&gt; 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&lt;MethodInfo&gt; lst = new List&lt;MethodInfo&gt;();
              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&lt;MethodInfo&gt; FindAllExecuteOnAssemblyLoadMethods(Assembly assembly)
      {
          return FindAllMethods(assembly, typeof(Attributes.ExecuteOnAssemblyLoadAttribute));
      }
  
      public static ICollection&lt;MethodInfo&gt; 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));
      }
  
  }
  </code></pre>