「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というエラー メッセージが表示されます。UserRepos リポジトリを使用しようとすると。質問は、アプリケーション (ASP.NET MVC) の開始時にユーザー リポジトリをどのように解決できるかです。何が問題なのですか?
public class MyApplication : HttpApplication
{
public IUserRepository UserRepos;
public IWindsorContainer Container;
protected void Application_Start()
{
Container = new WindsorContainer();
// Application services
Container.Register(
Component.For<IUserRepository>().ImplementedBy<UserRepository>()
);
UserRepos = Container.Resolve<IUserRepository>();
}
private void OnAuthentication(object sender, EventArgs e)
{
if (Context.User != null)
{
if (Context.User.Identity.IsAuthenticated)
{
//Error here "Object reference not set to an instance of an object."
var user = UserRepos.GetUserByName(Context.User.Identity.Name);
var principal = new MyPrincipal(user);
Thread.CurrentPrincipal = Context.User = principal;
return;
}
}
}
}
助けてくれてありがとう!