【发布时间】:2015-03-19 07:16:31
【问题描述】:
我有一个 .Net 4.5.1 控制台应用程序配置为 Windows 服务(派生自 ServiceBase),使用通过 Nuget 包安装的 EntityFramework 6.1。
它在本地运行良好(我将它作为控制台应用程序运行)。
我将它部署到安装了 .Net 4.5.2 的 Windows Server 2008 机器上,将其注册为 Windows 服务,并成功启动该服务。
该服务有一个每 90 秒工作一次的计时器。第一次工作时(90 秒后),当它尝试从 SQL Server 数据库读取时,它会失败并显示以下错误消息:
System.Configuration.ConfigurationErrorsException: The 'DbProviderFactories' section can only appear once per config file.
我查看了机器上的 2 个 machine.config 文件(在 .Net 2 和 4 文件夹中)并确认每个文件只有一个 DbProviderFactories 部分。我的服务 app.config 中没有 DbProviderFactories。 EventViewer 没有任何信息。我重新安装了 .Net 4.5.2 并重新启动服务器无济于事。
我怎样才能知道它在哪里尝试加载两个 DbProviderFactories 部分?
我正在添加下一点信息,以防万一。
服务第二次工作时(180 秒后),当它尝试从同一个 SQL Server 数据库中读取时,它会失败并显示不同的错误消息:
The ADO.NET provider with invariant name 'System.Data.SqlClient' is either not registered in the machine or application config file, or could not be loaded.
第二条错误消息与此问题类似: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient' 但不幸的是,我已经实施了所有修复它的建议。
我在我的数据访问层和我的控制台应用程序 Windows 服务中安装了 Nuget 包中的 Entity Framework 6。 NuGet 安装程序将所需的部分插入到我的 app.config 中:
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
和
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
我在构建时部署了 Visual Studio 放入我的 bin 文件夹中的内容。部署包括 EntityFramework.dll 和 EntityFramework.SqlServer.dll
我的数据库连接字符串是:
<connectionString value="data source=SQLServerName;initial catalog=DatabaseName;integrated security=true;persist security info=True" />
Windows 服务以有权访问数据库的身份运行。
第二个错误一直持续到我关闭服务为止。
【问题讨论】:
标签: c# windows-services entity-framework-6.1