我一定錯過了一些愚蠢的東西..
我正在嘗試在我的網路應用程式中使用會話,每當我嘗試運行該站點時,它都會給我這個:
InvalidOperationException:嘗試激活“EcommerceWebsite.Controllers.HomeController”時無法決議“Microsoft.AspNetCore.Http.ISession”型別的服務。

這是我的啟動:
public void ConfigureServices(IServiceCollection services)
{
// MVC Shit
services.AddControllers(options => options.EnableEndpointRouting = false);
// DBContext shit
services.AddDbContextPool<ventsus7_InventoryContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<IIMS, IMSSQL>();
services.AddDbContext<EcommerceWebsiteContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
// Identity shit
services.AddIdentity<EcommerceWebsiteUser, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = true;
})
.AddDefaultUI()
.AddEntityFrameworkStores<EcommerceWebsiteContext>().AddDefaultTokenProviders();
services.AddAuthentication();
services.AddAuthorization();
// Cache
services.AddMemoryCache();
services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.SlidingExpiration = true;
options.LoginPath = "/Identity/Account/Login";
});
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromDays(10);
options.Cookie.IsEssential = true;
});
// Payments Stuff
StripeConfiguration.ApiKey = Configuration.GetSection("Stripe")["SecretKey"];
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//app.UseHttpsRedirection();
app.UseStaticFiles();
//app.UseRouting();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
這是我的控制器:
private readonly ILogger<HomeController> _logger;
private ventsus7_InventoryContext context;
private readonly UserManager<EcommerceWebsiteUser> userManager;
private ISession memorySession;
private JsonSerializerSettings settings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
public HomeController(ILogger<HomeController> logger, ventsus7_InventoryContext dbContext, UserManager<EcommerceWebsiteUser> userManager, ISession memorySession)
{
_logger = logger;
context = dbContext;
this.userManager = userManager;
this.memorySession = memorySession;
}
uj5u.com熱心網友回復:
關于使用 AddSession 和 ISession 的最佳實踐,可以參考以下代碼。
家庭控制器:
private readonly ILogger<HomeController> _logger;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ISession _memorySession;
public HomeController(ILogger<HomeController> logger, IHttpContextAccessor accessor)
{
_logger = logger;
_httpContextAccessor = accessor;
_memorySession = _httpContextAccessor.HttpContext.Session;
}
public string Set(string key)
{
_memorySession.SetString("key", key);
return "success";
}
public string Get()
{
return _memorySession.GetString("key");
}
啟動.cs
...
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromDays(10);
options.Cookie.IsEssential = true;
});
測驗結果


轉載請註明出處,本文鏈接:https://www.uj5u.com/net/454908.html
標籤:asp.net-mvc asp.net 核心 会议
