【问题标题】:System.NullReferenceException when published to azureSystem.NullReferenceException 发布到 azure 时
【发布时间】:2015-05-27 20:05:42
【问题描述】:

您好,我已经发布了我的网站,该网站在本地从 Visual Studio 分叉到 Azure,现在每次我尝试访问该页面时都会遇到以下异常。

[NullReferenceException: Object reference not set to an instance of an object.]
   WebApplication1.Logic.RoleActions.AddUserAndRole() +390
   WebApplication1.Global.Application_Start(Object sender, EventArgs e) +47

因为它指向 Logic.RoleActions 和 Global.Application_Start 我将在其中发布代码。

角色操作

namespace WebApplication1.Logic
{
internal class RoleActions
{
    internal void AddUserAndRole()
    {
        // Access the application context and create result variables.
        Models.ApplicationDbContext context = new ApplicationDbContext();
        IdentityResult IdRoleResult;
        IdentityResult IdUserResult;

        // Create a RoleStore object by using the ApplicationDbContext object. 
        // The RoleStore is only allowed to contain IdentityRole objects.
        var roleStore = new RoleStore<IdentityRole>(context);

        // Create a RoleManager object that is only allowed to contain IdentityRole objects.
        // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object. 
        var roleMgr = new RoleManager<IdentityRole>(roleStore);

        // Then, you create the "canEdit" role if it doesn't already exist.
        if (!roleMgr.RoleExists("canEdit"))
        {
            IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
        }

        // Create a UserManager object based on the UserStore object and the ApplicationDbContext  
        // object. Note that you can create new objects and use them as parameters in
        // a single line of code, rather than using multiple lines of code, as you did
        // for the RoleManager object.
        var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var appUser = new ApplicationUser
        {
            UserName = "canEditUser@wingtiptoys.com",
            Email = "canEditUser@wingtiptoys.com"
        };
        IdUserResult = userMgr.Create(appUser, "Pa$$word1");

        // If the new "canEdit" user was successfully created, 
        // add the "canEdit" user to the "canEdit" role. 
        if (!userMgr.IsInRole(userMgr.FindByEmail("canEditUser@wingtiptoys.com").Id, "canEdit"))
        {
            IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("canEditUser@wingtiptoys.com").Id, "canEdit");
        }

        if (!userMgr.IsInRole(userMgr.FindByEmail("kasi64@seznam.cz").Id, "canEdit"))
        {
            IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("kasi64@seznam.cz").Id, "canEdit");
        }
    }
}
}

和全球

namespace WebApplication1
{
public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Create the custom role and user.
            RoleActions roleActions = new RoleActions();
            roleActions.AddUserAndRole();



        }
    }
}

我不明白为什么它在本地运行良好,但在发布时出现问题。谢谢

【问题讨论】:

标签: c# asp.net sql-server azure ado.net


【解决方案1】:

NullReferenceException 很可能是由userMgr.FindByEmail("xxx") 调用之一引起的。你不处理userMgr.FindByEmail方法返回null的情况,你直接访问它的Id属性。

【讨论】:

    【解决方案2】:

    我怀疑这是因为 Azure 中没有用户的电子邮件为 kasi64@seznam.cz。所以在这一行调用Id会抛出空引用异常。

    userMgr.FindByEmail("kasi64@seznam.cz").Id
    

    【讨论】:

    • 是的,就是这样。
    猜你喜欢
    • 2015-01-29
    • 2021-04-28
    • 2012-09-27
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    相关资源
    最近更新 更多