gpt4 book ai didi

c# - 每个 session ASP.net 4.6 MVC 5 个 Autofac 对象

转载 作者:行者123 更新时间:2023-11-30 12:25:00 26 4
gpt4 key购买 nike

在我的 ASP.net MVC 应用程序中,我想为每个 session 应“导出”的对象设置一个值。我试过这样:

我想要的对象:

    public class Core : ICore {

public Core() {
UserSession = new UserSession();
}

public UserSession UserSession { get; set; }
}

global.asax:

protected void Application_Start() {

var builder = new ContainerBuilder();
//register ICore
builder.RegisterType<Core.Core>().As<ICore>().InstancePerLifetimeScope();
builder.RegisterControllers(typeof (MvcApplication).Assembly);
builder.RegisterFilterProvider();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

BundleConfig.RegisterBundles(BundleTable.Bundles);

AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

我给对象(UserController)设置值的地方:

public class UserController : BaseController {
private readonly ICore _core;
private readonly IUserDomain _userDomain;

public UserController(ICore core, IUserDomain userDomain) {
_core = core;
_userDomain = userDomain;
}

[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginForm form) {

if (ModelState.IsValid) {
var user = _userDomain.GetByName(form.Username);
if (user != null) {
// Set Value
_core.UserSession.CurrentUser = user;

return RedirectToAction("Index", "Home");
}
}
return View(form);
}
}

我需要这个 session 对象的地方

在同一申请中排名第一:(这里ICore不为null,但是我之前在Controller中设置的CurrentUser为null)

    public class ClaimsAuthorize : AuthorizeAttribute {

public ICore Core { get; set; }

protected override bool AuthorizeCore(HttpContextBase httpContext) {
//Here is CurrentUser null
var currentUser = Core.UserSession.CurrentUser;
return currentUser != null;
}
}

在另一个项目中排名第二(SQL 上下文):

public class BaseContext <TModel> where TModel : BaseModel {
protected readonly ICore Core;
protected NHibernateSession NHibernateSession;

public BaseContext(ICore core) {
Core = core;
}

public virtual bool Save(TModel model) {
if (Core.UserSession.CurrentUser == null) {
return false;
}
model.CreatedBy = Core.UserSession.CurrentUser.Id;
model.CreatedAt = DateTime.Now;
using (var session = NHibernateSession.SessionFactory.OpenSession()) {
using (var transaction = session.BeginTransaction()) {
session.Save(model);
transaction.Commit();
}
}
return true;
}
}

public class PageContext : BaseContext<Page>, IPageContext {

public PageContext(ICore core, NHibernateSession nHibernateSession) : base(core) {
NHibernateSession = nHibernateSession;
}
}

最佳答案

在您的 global.asax 中,您可以像这样注册 ICore:

builder.Register(c =>
{
if (HttpContext.Current == null)
{
throw new Exception("no httpcontext available");
}

ICore core = HttpContext.Current.Session["ICore"] as ICore;
if (core == null)
{
core = new Core();
HttpContext.Current.Session["ICore"] = core;
}

return core;
}).As<ICore>();

在这种情况下,每次 Autofac 将解析一个 ICore 时,它会在 session 中查找并在需要时向其添加一个新实例。

关于c# - 每个 session ASP.net 4.6 MVC 5 个 Autofac 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32421882/

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