私はStructureMapの初心者であり、しばらくの間そのエラーを修正しようとしています。それを修正する方法と私が間違っているところを理解することはできません。テンプレートMVC4サイトをセットアップしましたが、何も含まれていませんが、それでもエラーが発生します。
誰かが私を助けてくれますか?
public static class IoC {
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IDbSession>().Use(() => MvcApplication.DbSession);
x.For<IDbService>().Use<DbService>();
});
return ObjectFactory.Container;
}
}
public class HomeController : Controller
{
protected readonly IDbService _dbService;
public HomeController(IDbService dbService)
{
_dbService = dbService;
}
...
}
public interface IDbSession : IDisposable
{
void Commit();
void Rollback();
}
public interface IDbService
{
StudentsService Students { get; }
CoursesService Courses { get; }
...
}
public class DbService : IDbService
{
private readonly IDbSession _dbSession;
public StudentsService Students { get; }
public CoursesService Courses { get; }
...
public DbService(IDbSession dbSession)
{
_dbSession = dbSession;
}
}
public class MvcApplication : System.Web.HttpApplication
{
private static readonly string _connectionString;
private static readonly IDbSessionFactory _dbSessionFactory;
public static IDbSession DbSession
{
get { return (IDbSession)HttpContext.Current.Items["Current.DbSession"]; }
private set { HttpContext.Current.Items["Current.DbSession"] = value; }
}
static MvcApplication()
{
_connectionString= ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
_dbSessionFactory = new DbSessionFactory(_connectionString);
}
protected MvcApplication()
{
BeginRequest += delegate
{
DbSession = _dbSessionFactory.Create();
};
EndRequest += delegate
{
if (DbSession != null)
DbSession.Dispose();
};
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}