gpt4 book ai didi

sql-server - 通过声明枚举会导致 HttpException SqlException

转载 作者:行者123 更新时间:2023-12-03 01:46:06 25 4
gpt4 key购买 nike

当我对 ClaimsIdentity 中的某些声明执行“foreach”循环时遇到问题。当我单步执行它时 - 在它应该完成枚举时,我看到它返回到“in”,然后在我收到有关与 SQL Server 连接的 HttpException 之前有十秒的延迟。我知道这听起来很奇怪,但这就是根据调试发生的情况 - 在我的 FindAllClaimsByType 方法的 foreach 循环中(我在使用 Entity Framework 后很快就连接到数据库,但我的断点永远不会被命中)。编辑:我应该提到,如果我跳过这个并在 Controller 中执行某些操作,我可以很好地连接到我的数据库。这是 ASP.NET MVC 4 中的

编辑 2:我已经缩小了范围。只有当它找不到Claim时才会出现这种情况。我如何枚举声明并不重要(如果我使用 ClaimsPrincipal.HasClaim()foreach 循环或 .Any() LINQ 表达式) - 如果找不到声明,它总是会抛出此异常。

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

我的代码如下:

public class ClaimsService
{

private ClaimsIdentity _identity;

public ClaimsService(ClaimsIdentity identity)
{
_identity = identity;
}

private Claim FindFirstClaimByType(string claimtype)
{
return FindAllClaimsByType(claimtype).FirstOrDefault();
}

private IEnumerable<Claim> FindAllClaimsByType(string claimtype)
{
ICollection<Claim> claims = new Collection<Claim>();
foreach (Claim claim in _identity.Claims)
{
if (claim.Type == claimtype)
{
claims.Add(claim);
}
}

return claims;
}

public School FindSchoolFromClaims()
{
string realm = FindFirstClaimByType(Data.Configuration.RealmClaimType).Value.ToString();

using (SqlDatabaseContext db = new SqlDatabaseContext())
{
School school = null;


school = db.Schools.Where(s => s.Realm == realm).FirstOrDefault();

return school;
}
}

public Developer FindDeveloperFromClaims()
{
string realm = FindFirstClaimByType(Data.Configuration.RealmClaimType).Value.ToString();
string nameidentifier = FindFirstClaimByType(ClaimTypes.NameIdentifier).Value.ToString();

using (SqlDatabaseContext db = new SqlDatabaseContext())
{
Developer developer = null;


if (realm == Data.Configuration.SoloDeveloperRealmMsft)
{
developer = db.Developers.OfType<SoloDeveloper>().Where(dev => dev.NameIdentifier == nameidentifier).FirstOrDefault();
}
else
{
developer = db.Developers.OfType<CompanyDeveloper>().Where(dev => dev.Realm == realm).FirstOrDefault();
}

return developer;
}
}
}

堆栈跟踪:

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +5296071
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +558
System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5308555
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +920
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +5311099
System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +5313314 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) +143
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry) +83 System.Data.SqlClient.SqlConnection.Open() +96 System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +76

[HttpException (0x80004005): Unable to connect to SQL Server database.] System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +131 System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +89 System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +27 System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +386

最佳答案

我在 Azure 云解决方案中使用全新的 ASP MVC Web 角色时遇到了同样的问题。尚未设置 SQL,也没有实现其他功能(除了 Azure ACS 联合登录)。我只是测试 ACS、WIF 和多个身份提供商的功能。

RoleManager 功能似乎是默认内置的。一旦我在 web.config 中删除了它 - 枚举声明时就不再尝试访问 SQL Server。

添加这一行:

  <modules>
<remove name="RoleManager" />
...

希望有帮助。

瑞克·舒里

关于sql-server - 通过声明枚举会导致 HttpException SqlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15448740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com