I'm writing C# code that can run either as a console app or a service. When run as a console app, it works fine. When run as a service I'm getting an odd error.

On startup, my service spawns a thread and this thread searches a directory for plugin .dll's that implement some of the service's functionality. I enumerate the files in the plugin directory, then for each file found, attempt to load the assembly and determine if it implements a desired interface.

This architecture is described here: <http://code.msdn.microsoft.com/windowsdesktop/Creating-a-simple-plugin-b6174b62>

I'm finding that I can enumerate all of my plugins, but when I call Assembly.GetTypes(), I get a LoaderException with the message: "Could not load file or assembly 'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."

Well, that's bunk because in the line of code just before this one, the file was enumerated from a call to Directory.GetFiles(path, "\*.dll"). Remember, this code also works find when run as a console app.

My service is running as Local System and the SYSTEM account has full permissions to the files, as do Administrators. Per [this page](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684190%28v=vs.85%29.aspx), Local System's token includes both the NT AUTHORITY\SYSTEM and BUILTIN\Administrators SID, so I don't think it's a filesystem permissions issue.

I'm flummoxed. Can anyone suggest a cause and a solution?

Here's the code (it looks long but a lot of it is logging):

```
private void LoadPlugins()
    {
        lstPlugins = new List<MyDesiredInterface>();
        string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        path += "\\PlugIns";
        if (Directory.Exists(path))
        {
            Log("PlugIns folder exists.", TAG);
            string[] dllFileNames = Directory.GetFiles(path, "*.dll");
            ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
            foreach (string dllFile in dllFileNames)
            {
                AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
                Assembly assembly = Assembly.Load(an);
                assemblies.Add(assembly);
            }

            Log("Found " + assemblies.Count + " assemblies.", TAG);

            Type pluginType = typeof(MyDesiredInterface);
            ICollection<Type> pluginTypes = new List<Type>();
            foreach (Assembly assembly in assemblies)
            {
                if (assembly != null)
                {
                    Log("Evaluating assembly: " + assembly.Location, TAG);
                    try
                    {
                        Type[] types = assembly.GetTypes(); <-- error happens here
                        foreach (Type type in types)
                        {
                            if (type.IsInterface || type.IsAbstract)
                            {
                                Log("Assembly does not implement our interface.", TAG);
                                continue;
                            }
                            else
                            {
                                if (type.GetInterface(pluginType.FullName) != null)
                                {
                                    Log("Assembly implements our interface!", TAG);
                                    pluginTypes.Add(type);
                                }
                            }
                        }
                    }
                    catch (ReflectionTypeLoadException ex)
                    {
                        StringBuilder errMsg = new StringBuilder("An exception occurred tying to load types in an assembly.\r\n");
                        errMsg.Append("The assembly is: " + assembly.Location + "\r\n");
                        errMsg.Append("Exceptions are:\r\n");
                        foreach (Exception e in ex.LoaderExceptions)
                        {
                            errMsg.Append(e.Message + "\r\n");
                        }
                        Log(errMsg.ToString(), TAG);
                    }
                }
            }

            foreach (Type type in pluginTypes)
            {
                MyDesiredInterface plugin = (MyDesiredInterface)Activator.CreateInstance(type);
                this.Log("Loading plugin: " + plugin.CommandName(), TAG);
                plugin.Register(this);
                lstPlugins.Add(plugin);
            }
            this.Log("Total plugins loaded: " + lstPlugins.Count, TAG);
        }
        else
        {
            Log("PlugIns folder not found.", TAG);
        }
    }

```

Here's the log file:

```
3/12/2014 9:53:46 AM    chatInterface   LoadPlugIns()
3/12/2014 9:53:46 AM    chatInterface   PlugIns folder exists.
3/12/2014 9:53:46 AM    chatInterface   Found 13 assemblies.
3/12/2014 9:53:46 AM    chatInterface   Evaluating assembly: C:\Users\MyUsername\Documents\Visual Studio 2010\Projects\MyProject\MyProjectService\bin\Debug\PlugIns\PlugIn1.dll
3/12/2014 9:53:46 AM    chatInterface   An exception occurred tying to load types in an assembly.
The assembly is: C:\Users\MyUsername\Documents\Visual Studio 2010\Projects\MyProject\MyProjectService\bin\Debug\PlugIns\PlugIn1.dll
Exceptions are:
Could not load file or assembly 'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

3/12/2014 9:53:46 AM    chatInterface   Evaluating assembly: C:\Users\MyUsername\Documents\Visual Studio 2010\Projects\MyProject\MyProjectService\bin\Debug\PlugIns\PlugIn2.dll
3/12/2014 9:53:46 AM    chatInterface   An exception occurred tying to load types in an assembly.
The assembly is: C:\Users\MyUsername\Documents\Visual Studio 2010\Projects\MyProject\MyProjectService\bin\Debug\PlugIns\PlugIn2.dll
Exceptions are:
Could not load file or assembly 'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

```

It was suggested that I use the Assembly Binding Log Viewer (fuslogvw.exe) to log the failures. Here's what that log shows:

```
    *** Assembly Binder Log Entry  (3/13/2014 @ 9:50:30 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
Running under executable  C:\Users\MyUsername\Documents\Visual Studio 2010\Projects\MyProject\MyProjectService\bin\Debug\MyProjectService.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = NT AUTHORITY\SYSTEM
LOG: Where-ref bind. Location = C:\Users\MyUsername\Documents\Visual Studio 2010\Projects\MyProject\MyProjectService\bin\Debug\PlugIns\MyPlugIn.dll
LOG: Appbase = file:///C:/Users/MyUsername/Documents/Visual Studio 2010/Projects/MyProject/MyProjectService/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: No application configuration file found.
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Attempting download of new URL file:///C:/Users/MyUsername/Documents/Visual Studio 2010/Projects/MyProject/MyProjectService/bin/Debug/PlugIns/MyPlugIn.dll.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\MyUsername\Documents\Visual Studio 2010\Projects\MyProject\MyProjectService\bin\Debug\PlugIns\MyPlugIn.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: MyPlugIn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
LOG: Re-apply policy for where-ref bind.
LOG: Binding succeeds. Returns assembly from C:\Users\MyUsername\Documents\Visual Studio 2010\Projects\MyProject\MyProjectService\bin\Debug\PlugIns\MyPlugIn.dll.
LOG: Assembly is loaded in LoadFrom load context.

```

In this test run, I was using Assembly.LoadFrom() instead of Assembly.Load(), but in both cases the assembly loads. This log shows it in LoadFrom load context, I have other runs that show it in Load load context with the other call. In any case, the load succeeds, but attempting to enumerate GetTypes fails with "The system cannot find the file specified."

Any help would be appreciated!