服务报告'找不到指定文件'对于存在的文件

本文关键字:文件 存在 于存在 报告 找不到 服务 | 更新日期: 2023-09-27 17:50:03

我正在编写可以作为控制台应用程序或服务运行的c#代码。当作为控制台应用运行时,它运行得很好。当作为服务运行时,我得到一个奇怪的错误。

在启动时,我的服务生成一个线程,这个线程在一个目录中搜索实现服务某些功能的插件。dll。我枚举插件目录中的文件,然后对于找到的每个文件,尝试加载程序集并确定它是否实现了所需的接口。

此架构描述如下:http://code.msdn.microsoft.com/windowsdesktop/Creating-a-simple-plugin-b6174b62

我发现我可以枚举我所有的插件,但是当我调用assembly . gettypes()时,我得到一个LoaderException消息:"无法加载文件或程序集'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'或其依赖项之一。系统找不到指定的文件。"

好吧,这是胡扯,因为在这行代码之前的代码行中,文件是从对Directory的调用中枚举的。getfile(路径,"* . dll")。请记住,这段代码在作为控制台应用程序运行时也适用于find。

我的服务是作为本地系统运行的,系统帐户对文件有完全的权限,管理员也有。根据此页,本地系统的令牌包括NT AUTHORITY' System和BUILTIN'Administrators SID,所以我认为这不是文件系统权限问题。

我的困惑。谁能提出原因和解决办法?

下面是代码(它看起来很长,但很多都是日志记录):

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

日志文件如下:

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.

有人建议我使用程序集绑定日志查看器(fuslogvw.exe)来记录故障。以下是日志显示的内容:

    *** 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.

在这个测试运行中,我使用assembly . loadfrom()而不是assembly . load(),但在这两种情况下,程序集都加载了。这个日志显示它在LoadFrom load上下文中,我有其他运行显示它在load load上下文中与其他调用。在任何情况下,加载都是成功的,但是试图枚举GetTypes失败,提示"the system cannot find the file specified"。

任何帮助将不胜感激!

服务报告'找不到指定文件'对于存在的文件

请检查您引用的程序集是否反过来引用了较旧的版本。删除、重建和重新引用可能会有所帮助。

"FuseLogVw"有助于找到谁在加载程序集,定义日志路径,并运行您的解决方案,然后检查FuseLogVw

中的第一行

From http://msdn.microsoft.com/en-us/library/ky3942xh(v=vs.110).aspx:

"如果assemblyString指定完整的程序集名称,并且与简单名称匹配的第一个程序集具有不同的版本、区域性或公钥令牌,则抛出FileLoadException。加载器不会继续探测与简单名称匹配的其他程序集。"

那么存在多个版本的程序集吗?