【发布时间】:2016-08-03 15:53:10
【问题描述】:
这会让我发疯的。
我构建了一个简单的网络应用程序供在内网使用,它在本地测试期间运行良好。当我将它部署到 IIS 服务器上时,它给了我一些问题,我们认为这与错误的 web.config 文件有关。
我完全按照 MySQL 文档的说明检查并配置了 web.config 文件,现在该应用程序也无法在本地运行。这显然是相关的,但我无法弄清楚是什么原因造成的。
使用 MySQL 设置,我得到一个带有此堆栈跟踪的 NullReferenceException:
[NullReferenceException: Object reference not set to an instance of an object.]
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +56
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +234
MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection connection) +36
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) +55
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +56
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +79
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +112
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +635
System.Data.Entity.Internal.InternalContext.Initialize() +28
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +27
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +79
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +28
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.IEnumerable.GetEnumerator() +64
System.Web.UI.WebControls.ListViewPagedDataSource.GetEnumerator() +305
System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) +215
System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +952
System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data) +63
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +34
System.Web.UI.WebControls.ModelDataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +80
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.ListView.PerformSelect() +114
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +114
System.Web.UI.WebControls.ListView.CreateChildControls() +77
System.Web.UI.Control.EnsureChildControls() +92
System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +883
尝试单步执行代码只会导致它在第一项运行时失败,似乎没有链接到任何特定行。这就是我的 web.config 文件中的内容,因为我已经对其进行了更改。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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>
<appSettings>
<add key="PrinterTemplate" value="--redacted--" />
<add key="PrinterQueue" value="--redacted--" />
<add key="FileVault" value="--redacted--" />
<add key="AdminGroup" value="--redacted--" />
</appSettings>
<location path="Modify.aspx">
<system.web>
<authorization>
<allow roles="--redacted--" />
<deny users="*,?" />
</authorization>
</system.web>
</location>
<location path="AccessDenied.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<connectionStrings>
<add name="DefaultConnection" connectionString="DRIVER={MySQL ODBC 3.51 Driver};Server=servername;port=3306;Database=usermanuals;UId=username;password=password" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
</controls>
</pages>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
</DbProviderFactories>
</system.data>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
我看到很多人说这与他们对连接字符串的实际使用有关,但我的并不是在实际调用数据库时触发,而是在 Web 应用程序运行时触发。无论如何,这就是我在代码中实际使用连接字符串的方式。我所有的用法都有相同的结构,只是命令不同。
using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
connection.Open();
using (OdbcCommand command = new OdbcCommand("SELECT id, title FROM manuals", connection))
using (OdbcDataReader dr = command.ExecuteReader())
{
//databindings and stuff.
}
}
【问题讨论】:
-
看起来您没有为您的项目使用实体框架。你能注释掉底部的
<section name="entityFramework"..以及整个<entityFramework>块,看看是否有帮助。 -
但我的并没有触发对数据库的实际调用,而是在 Web 应用程序运行时触发。那为什么要贴出这部分代码呢?应用程序第一次出现时运行的代码在哪里?
-
尝试生成一个新的错误集,这似乎在应用程序加载之前触发。建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)实际的连接字符串事先工作正常。
标签: c# mysql web-config nullreferenceexception