gpt4 book ai didi

sql-server - 在 Azure 上运行 MVC4 时出错

转载 作者:行者123 更新时间:2023-12-02 07:35:39 24 4
gpt4 key购买 nike

我们有时会在 Azure 上启动 MVC4 站点时遇到错误。在我们的本地服务器上从未见过这些错误。将数据库和应用程序部署到 Azure 后,如果请求授权并且在站点上处于不活动状态一段时间后,启动主页时会出现问题(可能由超时引起的错误)。代码很简单,如下所示:

[InitializeSimpleMembership]
[Authorize(Roles = "Administrator")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}

请求主页有时会失败:

[Win32Exception (0x80004005): Access is denied]

[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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
....
System.Data.SqlClient.SqlConnection.Open() +96
System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext context, Boolean revertImpersonate) +88
System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) +239
System.Web.Security.SqlRoleProvider.GetRolesForUser(String username) +762
WebMatrix.WebData.SimpleRoleProvider.GetRolesForUser(String username) +54
...

如何防止此(超时)错误?感谢您的帮助!

最佳答案

将以下行从 InitializeSimpleMembershipAttribute.cs(过滤器)移至 AuthConfig.cs:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

所以代码将如下所示:

public static class AuthConfig
{
public static void RegisterAuth()
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
...

也许您的 web.config(system.web 部分)中还需要以下内容:

<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear/>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear/>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>

关于sql-server - 在 Azure 上运行 MVC4 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16708202/

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