在Windows Server 2012 R2 Standard上運行的是使用我撰寫的物體框架的C# Windows 服務(.Net Framework 4.7.2),它從檔案中讀取資料并將資料傳遞到本地 SQLExpress (2016)存盤程序以將資料插入到資料庫。
在我的 Windows 10 機器上測驗時它作業正常,但在將可執行檔案和 exe.config 移動到 Windows Server 并使用連接字串中的正確 DB 資訊更新配置后,事件查看器顯示以下 Windows 應用程式錯誤:
來源:.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*)
這似乎阻止了存盤程序被呼叫,因此沒有資料被傳遞到資料庫。
我已將問題縮小到物體資料模型物件的實體化:
PrintMailEntities dataEntities = new PrintMailEntities();
此行呼叫由物體框架創建的自動生成的代碼:
public partial class PrintMailEntities : DbContext
{
public PrintMailEntities()
: base("name=PrintMailEntities")
{
}
// Several more auto-generated methods...
}
和超類建構式(同樣是物體框架的一部分,不是我的代碼):
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...
}
此行之前程式中的所有內容都按需要作業(我使用了幾個應用程式事件日志來跟蹤發生的情況,因為我在 Windows Server 上沒有用于除錯的 Visual Studio。)我試圖從中讀取的檔案不是問題,我可以很好地閱讀該資訊。
我還嘗試使用 try/catch 來包圍實體化以捕獲 FileNotFoundException,但從未觸發捕獲。我還鏡像了 Windows Server DB 的資料庫權限以匹配我本地機器 DB 的資料庫權限,并以管理員權限運行兩者。
Here is the connection string in the 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="data source=machineName\HPWJA;initial catalog=PrintMail;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" 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. 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.
EDIT
Thanks to RB I now have more details about this "file" that couldn't be found. 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
<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?
uj5u.com熱心網友回復:
問題是我缺少在構建輸出的 Release 目錄中找到的 EntityFramework.dll。添加 EntityFramework.dll 和 EntityFramework.SqlServer.dll 解決了這個問題。感謝 RB 幫我找到這個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/332839.html
標籤:c# entity-framework tsql sql-server-express windows-server-2012
上一篇:加速磁區ROW_NUMBER()
下一篇:SQL運行總數達到X
