title: AssemblyResolve in Windows Services c#
body: <p>I have an issue which I have solved in a normal application in the normal AssemblyResolve way, that is by creating an event such as</p>
  
  <pre><code>AppDomain.CurrentDomain.AssemblyResolve += EmbeddedAssemblyResolver;
  </code></pre>
  
  <p>Where the EmbeddedAssemblyResolver looks like</p>
  
  <pre><code>private static Assembly EmbeddedAssemblyResolver(object sender, ResolveEventArgs args)
      {
              String resourceName = Assembly.GetExecutingAssembly().FullName.Split(',').First() + "." + new AssemblyName(args.Name).Name + ".dll";
              using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
              {
                  if (stream != null)
                  {
                      Byte[] assemblyData = new Byte[stream.Length];
                      stream.Read(assemblyData, 0, assemblyData.Length);
                      return Assembly.Load(assemblyData);
                  }
              }
          return null;
      }
  </code></pre>
  
  <p>Which works all groovy when in an application, I embed all my dll's in the application and then they load with the loader.  But the same is not true for my service which I want to work in the same way, all I get in the event log is FileNotFound exceptions.  I run the above code in the Program.cs Main() method and that method does precious little else other than load the services to run and links to no dynamic assemblies.  Here is the code</p>
  
  <pre><code>    static void Main()
      {
          ServiceBase[] ServicesToRun;
          ServicesToRun = new ServiceBase[]
              {
                  new ServiceLoader()
              };
          ServiceBase.Run(ServicesToRun);
      }
  </code></pre>
  
  <p>Does anyone know why the resolver works in an application, but not in a windows service?</p>