title: GacInstall on Sharepoint Timer Job a.k.a SPJobDefinition
body: <p>I am trying to implement a plugin architecture to our Sharepoint solution at the moment where administrators can upload dll files which will be installed and processed by our solution.</p>
  
  <p>Implementation requires GAC installation of the uploaded dll, which I have accomplished using</p>
  
  <pre><code>private void InstallToGac(byte[] libraryContent, string filename)
          {
              string path = tempFolder + filename;
              File.WriteAllBytes(path, libraryContent);
              try
              {
                  new System.EnterpriseServices.Internal.Publish().GacInstall(path);
              }
              catch (Exception ex)
              {
                  Logger.Error("Error while installing dll to GAC.", ex);
                  throw;
              }
              finally
              {
                  File.Delete(path);
              }
          }
  </code></pre>
  
  <p>This simply installs the dll to GAC and works fine if it is called within Sharepoint context. However, it wouldn't work on multi-server environment since this will only install it on the front-end where the administrator uploaded the file. So, I have moved the code above to a timer job, by inheriting <code>SPJobDefinition</code>. GacInstall does not install the dll to GAC anymore. On debug mode <code>GacInstall</code> line works fine without throwing any exception but without any effect either.</p>
  
  <pre><code>System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  </code></pre>
  
  <p>returns <code>Network Service</code> within the timer job. I work on single server Sharepoint 2010 Foundation but I need this to work on 2007/2010/2013 on both single and multi server environments.</p>