如何在.NET Core 2.0中植入角色?

时间:2018-05-03 11:21:03

标签: c# asp.net-core .net-core asp.net-core-mvc .net-core-2.0

目前我正在尝试使用Email配置角色,内置于.NET Core 2.0,mvc-framework。

但是我收到以下错误:

RoleManager<Identity>

这是因为System.ObjectDisposedException HResult=0x80131622 Message=Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. The error occured in line 20 of UserRoleSeed-class. 方法的异步字符吗?

我的Program.cs:

Seed()

我的UserRoleSeed.cs:

public class Program
{
    public static void Main(string[] args)
    {
        var host = BuildWebHost(args);

        using (var scope = host.Services.CreateScope())
        {
            var serviceProvider = scope.ServiceProvider;
            try
            {
                var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();

                new UserRoleSeed(roleManager).Seed();
            }
            catch
            {
                throw new Exception();
            }
        }

        host.Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

这应该在我的Context imo的dbo.AspNetRoles表中创建一个新条目,但事实并非如此。问题可能很小,这是我第一次尝试在Mvc框架中使用角色。

我首先尝试使用Startup.cs文件,在此文件的Configure()方法中调用public class UserRoleSeed { private readonly RoleManager<IdentityRole> _roleManager; public UserRoleSeed(RoleManager<IdentityRole> roleManager) { _roleManager = roleManager; } public async void Seed() { if ((await _roleManager.FindByNameAsync("Berijder")) == null) { await _roleManager.CreateAsync(new IdentityRole {Name = "Berijder"}); } } } 方法,该方法不起作用(可能是因为这是CORE 2.0而不是1.0)。

1 个答案:

答案 0 :(得分:1)

async void可用作“即发即弃”方法。在异步void完成之前,代码将继续运行并处理所有对象。 处理此问题的另一种方法:

将其设为异步Task并致电Wait();

public async Task SeedAsync()
{
    ...
}

这样称呼:

// Calling Wait in EFCore is not so much of a problem, since there is not
// really a main blocking thread like UI program's have in .NET framework.
new UserRoleSeed(roleManager).SeedAsync().Wait();

另一个解决方案是使用任务运行器:

public static void Main(string[] args)
{
    var host = BuildWebHost(args);
    Task.Run(() => InitializeAsync(host));
    host.Run();
}

private static async Task InitializeAsync(IWebHost host)
{
    using (var scope = host.Services.CreateScope())
    {
        var serviceProvider = scope.ServiceProvider;
        try
        {
            var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();

            await new UserRoleSeed(roleManager).SeedAsync();
        }
        catch
        {
            // TODO: log the exception
            throw;
        }
    }
}

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .Build();