必须在服务集合上调用AddIdentity

时间:2017-04-24 22:22:13

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

使用 netcore 1.1.1 Debian 8 上运行 Asp.net核心网站时遇到问题版本 1.0.3

所以我使用 VS2017 在我的 Windows 10 平台上使用 Asp.net核心创建了一个网站,并由dotnet publish -c release发布

然后我使用FTP将我的项目上传到 Debian 8 ,然后我写了

dotnet Altram.Web.Donate.dll

Unhandled Exception: System.InvalidOperationException: AddIdentity must be called on the service collection.
   at Microsoft.AspNetCore.Builder.BuilderExtensions.UseIdentity(IApplicationBuilder app)
   at Altram.Web.Donate.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in /mnt/d/Development/C#/Visual Studio/Altram System/Altram.Web.Donate/Startup.cs:line 104
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Altram.Web.Donate.Program.Main(String[] args) in /mnt/d/Development/C#/Visual Studio/Altram System/Altram.Web.Donate/Program.cs:line 10
Aborted
Startup.cs 配置服务方法

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    if (Environment.IsDevelopment())
    {
        services.AddSwaggerGen(options =>
        {
            options.SwaggerDoc("v1", new Info());
            options.IncludeXmlComments(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath,
                "Altram.Web.Donate.xml"));
        });

        services.AddDbContext<DatabaseContext>(builder =>
        {
            builder.UseNpgsql(Configuration["Data:AltramDatabase"],
                options => options.MigrationsAssembly("Altram.Web.Donate"));
        });

        services.AddDbContext<DatabaseContextCache>(builder =>
            builder.UseInMemoryDatabase());

        services.AddIdentity<User, IdentityRole>()
            .AddEntityFrameworkStores<DatabaseContext>()
            .AddErrorDescriber<ErrorDescriber>()
            .AddDefaultTokenProviders();

        services.Configure<IdentityOptions>(options =>
        {
            options.Password.RequiredLength = 46;
        });

        services.AddMvc();
        services.AddDataInitializer();
        services.DomainInitializer();

        services.Initialize(links =>
            {
                links.Login = Configuration["Robokassa:RobokassaLogin"];
                links.Mode = Configuration["Robokassa:RobokassaMode"];
                links.Pass1 = Configuration["Robokassa:RobokassaPass1"];
                links.Pass2 = Configuration["Robokassa:RobokassaPass2"];
            });

        services.AddLogging();
    }
}

配置方法

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseIdentity();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

所以它在 app.UseIdentity();

给我错误

它在Windows 10上没有任何问题,工作得很好

我将PostgresSql用于我的项目

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我已修复错误,删除并将其放在if括号

之外
services.AddIdentity<User, IdentityRole>()
    .AddEntityFrameworkStores<DatabaseContext>()
    .AddErrorDescriber<ErrorDescriber>()
    .AddDefaultTokenProviders();

来自if(Enviroment.IsDevelopment())代码,因为当我从终端启动dotnet时,它从生产环境模式开始,因此addIdentity无法正常工作。