简体   繁体   English

抛出异常:当实体数据模型对象实例化时,C# Windows 服务中的“System.IO.FileNotFoundException”

[英]Exception Thrown: 'System.IO.FileNotFoundException' in C# Windows Service When Entity Data Model Object Instantiated

Running on Windows Server 2012 R2 Standard is a C# Windows Service (.Net Framework 4.7.2) using Entity Framework that I wrote which reads data from a file and passes the data to a local SQLExpress (2016) stored procedure to insert the data into the DB.Windows Server 2012 R2 Standard上运行的是使用我编写的实体框架C# Windows 服务(.Net Framework 4.7.2) ,它从文件中读取数据并将数据传递到本地 SQLExpress (2016)存储过程以将数据插入到数据库。

While testing on my Windows 10 machine it works fine, but after moving the executable and exe.config to the Windows Server and updating the config with the correct DB information in the connection string, the Event Viewer shows the following Windows Application Error:在我的 Windows 10 机器上测试时它工作正常,但在将可执行文件和 exe.config 移动到 Windows Server 并使用连接字符串中的正确数据库信息更新配置后,事件查看器显示以下 Windows 应用程序错误:

Source: .NET Runtime Event 1026来源:.NET 运行时事件 1026

Application: Print_Mail-DBAuger.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
   at Print_Mail_DBAuger.Program.StartParseAndInsert(System.String, System.Diagnostics.EventLog)
   at Print_Mail_DBAuger.FetchService.OnChanged(System.Object, System.IO.FileSystemEventArgs)
   at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
   at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
   at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

This seems to be preventing the stored procedure from being called, so no data is being passed to the DB.这似乎阻止了存储过程被调用,因此没有数据被传递到数据库。

I have narrowed the issue down to the instantiation of the Entity Data Model object:我已将问题缩小到实体数据模型对象的实例化:

PrintMailEntities dataEntities = new PrintMailEntities();

This line calls auto-generated code created by the Entity Framework:此行调用由实体框架创建的自动生成的代码:

public partial class PrintMailEntities : DbContext
    {
        public PrintMailEntities()
            : base("name=PrintMailEntities")
        {
        }
        
        // Several more auto-generated methods...
    }

And the super class constructor (again, part of the Entity Framework, not my code):和超类构造函数(同样是实体框架的一部分,不是我的代码):

public class DbContext : IDisposable, IObjectContextAdapter
    {
        //
        // Summary:
        //     Constructs a new context instance using the given string as the name or connection
        //     string for the database to which a connection will be made. See the class remarks
        //     for how this is used to create a connection.
        //
        // Parameters:
        //   nameOrConnectionString:
        //     Either the database name or a connection string.
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public DbContext(string nameOrConnectionString);

        // Other overrloaded constructors not used...
     }

Everything in the program prior to this line works as desired (I used several application event logs to track what was happening since I don't have Visual Studio on the Windows Server to debug with.) The file I am trying to read from is not the issue, I can read that information fine.此行之前程序中的所有内容都按预期工作(我使用了几个应用程序事件日志来跟踪发生的情况,因为我在 Windows Server 上没有用于调试的 Visual Studio。)我试图从中读取的文件不是问题,我可以很好地阅读该信息。

I have also tried surrounding the instantiation with a try/catch to catch the FileNotFoundException, but the catch is never fired.我还尝试使用 try/catch 来包围实例化以捕获 FileNotFoundException,但从未触发捕获。 I have also mirrored the database permissions of the Windows Server DB to match that of my local machines DB, and running both with admin privileges.我还镜像了 Windows Server DB 的数据库权限以匹配我本地机器 DB 的数据库权限,并以管理员权限运行两者。

Here is the connection string in the Windows Server exe.config:这是 Windows Server exe.config 中的连接字符串:

<connectionStrings>
        <add name="PrintMailEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=machineName\HPWJA;initial catalog=PrintMail;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>

Again, this works fine on Windows 10 with the connection string pointed to a DB that mirrors the Windows Server DB.同样,这在 Windows 10 上运行良好,连接字符串指向一个镜像 Windows Server DB 的数据库。 There are no build errors on my machine, and there are no SQL Server Logs on the Windows Server stating that anything wrong is happening on the DB side.我的机器上没有构建错误,Windows Server 上也没有 SQL Server 日志表明 DB 端发生了任何错误。

EDIT编辑

Thanks to RB I now have more details about this "file" that couldn't be found.多亏了 RB,我现在有了关于这个无法找到的“文件”的更多详细信息。 Here is the updated event log.这是更新的事件日志。

Error: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
   at Print_Mail_DBAuger.Program.StartParseAndInsert(String inputFile, EventLog programEventLog)
   at Print_Mail_DBAuger.FetchService.OnChanged(Object sender, FileSystemEventArgs e)
EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 was missing.

The error seems to be referencing a section element in app.config该错误似乎引用了 app.config 中的一个部分元素

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>

The server does not have internet, so maybe it's trying to pull the nuget package from online?服务器没有互联网,所以它可能试图从网上提取 nuget 包?

The issue was that I was missing the EntityFramework.dll found in the Release directory of the build output.问题是我缺少在构建输出的 Release 目录中找到的 EntityFramework.dll。 Adding both EntityFramework.dll and EntityFramework.SqlServer.dll solved this issue.添加 EntityFramework.dll 和 EntityFramework.SqlServer.dll 解决了这个问题。 Thanks RB for helping me find this.感谢 RB 帮我找到这个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# | 抛出异常:Businesslaag_Class_Libary.dll 中的“System.IO.FileNotFoundException” - C# | Exception thrown: 'System.IO.FileNotFoundException' in Businesslaag_Class_Libary.dll 抛出异常:mscorlib.dll,Visual Studio C# 中的“System.IO.FileNotFoundException” - Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll, Visual studio C# c#中类型为&#39;System.IO.FileNotFoundException&#39;的未处理异常 - An Unhandled exception of type 'System.IO.FileNotFoundException' in c# C# System.IO.FileNotFoundException 当文件确实存在时 - C# System.IO.FileNotFoundException when file definitely exists 抛出异常:mscorlib.dll 中的“System.IO.FileNotFoundException” - Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll 引发异常:Prism.Wpf.dll中的“ System.IO.FileNotFoundException” - Exception thrown: 'System.IO.FileNotFoundException' in Prism.Wpf.dll 抛出异常:mscorlib.dll C3 Discord.NET bot 中的“System.IO.FileNotFoundException” - Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll C3 Discord.NET bot 在Windows Phone 8中调用PCL中的方法时抛出了System.IO.FileNotFoundException - System.IO.FileNotFoundException been thrown when calling method in PCL in Windows phone 8 WCF Windows 服务启动失败,抛出 System.IO.FileNotFoundException - WCF Windows Service fails to start, throws System.IO.FileNotFoundException System.IO.FileNotFoundException:找不到网络路径。 在Windows 7上使用DirectoryEntry对象时出现异常 - System.IO.FileNotFoundException: The network path was not found. Exception while using DirectoryEntry object on windows 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM