C# Autofac:在不活动后,我得到;对象引用未设置为对象的实例;
我的解决方案中有两个项目:BusinessLibrary和Web(asp.NETMVC4)。我将autofac用于DI,似乎在一些不活动之后,控制器中的注入对象将变为null。我是否在autofac中的生命周期配置中做错了什么 网络项目 AccountController引用BusinessLibrary中的服务类,如下所示:C# Autofac:在不活动后,我得到;对象引用未设置为对象的实例;,c#,asp.net,autofac,C#,Asp.net,Autofac,我的解决方案中有两个项目:BusinessLibrary和Web(asp.NETMVC4)。我将autofac用于DI,似乎在一些不活动之后,控制器中的注入对象将变为null。我是否在autofac中的生命周期配置中做错了什么 网络项目 AccountController引用BusinessLibrary中的服务类,如下所示: public class AccountController : Controller { private readonly IAccountService ac
public class AccountController : Controller
{
private readonly IAccountService accountService;
public AccountController(){}
public AccountController(IAccountService accountService)
{
this.accountService = accountService;
}
[HttpPost]
public ActionResult Logon(User model)
{
if (accountService.AuthenticateUser(model.Username, model.Password))
{
//... some logic here
}
}
}
这是Global.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
//some other logic here
IoCConfig.RegisterDependencies();
}
}
最后,IoCConfig中的autofac配置:
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
// register services
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerHttpRequest();
// register controllers
builder.RegisterControllers(typeof(MvcApplication).Assembly).InstancePerHttpRequest();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
商业图书馆项目
会计服务类别:
public interface IAccountService
{
bool AuthenticateUser(string username, string password);
}
public class AccountService : IAccountService
{
private readonly IAuthenticationService authenticationService;
public AccountService(IAuthenticationService authenticationService)
{
this.authenticationService = authenticationService;
}
public bool AuthenticateUser(string username, string password)
{
//some logic that uses authenticationService
}
}
AccountService接受IAuthenticationService的一个实例,我从引用的dll获取该实例,该dll执行一些AD身份验证逻辑
问题
在VS2012中重建后,当我在本地机器(IIS 7.5)上运行它时,我能够登录,一切正常。
现在,如果我让选项卡保持打开状态并执行其他操作,在我进入页面并尝试执行任何操作之后,我会得到一个异常:对象引用未设置为对象的实例。此处控制器中的accountService为空:
public class AccountController : Controller
{
// constructors...
[HttpPost]
public ActionResult Logon(User model)
{
if (accountService.AuthenticateUser(model.Username, model.Password)) // accountService is null
任何帮助都将不胜感激
另外,我在SO上检查了类似的问题,但这些问题都是关于web.config文件中的私有构造函数或双autofac配置。根据您最后的评论,您的web项目需要引用所有服务项目 基本上,你应该可以这样注册-
builder.RegisterType<AccountService>().As<IAccountService>()
.InstancePerHttpRequest();
builder.RegisterType<AuthenticationService>().As<IAuthenticationService>()
.InstancePerHttpRequest();
builder.RegisterType().As()
.InstancePerHttpRequest();
builder.RegisterType().As()
.InstancePerHttpRequest();
如果删除builder.registerasemblytypes…
并像这样手动注册builder.RegisterType().As().InstancePerHttpRequest()
@Win,我可以从BusinessLibrary将AccountService注册为IAccountService
,但无法将AuthenticationService注册为IAAuthenticationService
,后者在BusinessLibrary中引用。但是你的回答实际上让我觉得Asp.web不应该负责注册BL使用的类。我将尝试重新设计关系,因为这里有一些根本性的错误。谢谢!赢FTW!虽然我非常希望autofac自动完成所有注册,但这似乎是唯一可行的解决方案。我还向Web项目添加了一个AuthenticationService引用,以便正确注册。