asp.net-mvc – Structuremap没有为此对象定义的无参数构造函数
发布时间:2020-12-16 06:26:07 所属栏目:asp.Net 来源:网络整理
导读:我是StructureMap的新手,我一直试图解决这个错误已经有一段时间了. 只是无法弄清楚如何解决它以及我在哪里做错了. 我甚至设置了一个模板MVC4网站,其中没有任何内容,但仍然出现错误. 有人可以帮帮我吗? public static class IoC { public static IContainer
我是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(); } } 解决方法
您必须配置依赖项解析程序以处理Controller的构造函数中的参数.你可以在这里找到如何:
http://ardalis.com/How-Do-I-Use-StructureMap-with-ASP.NET-MVC-3
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- asp.net-mvc – ASP.Net MVC在使用Ajax.ActionLi
- 一步一步创建ASP.NET MVC5程序[Repository+Autof
- asp.net-mvc – ASP.net MVC CheckBoxFor转换错误
- 谈谈基于SQL Server 的Exception Handling[上篇]
- 使用StateServer的ASP.NET会话混合(SCARY!)
- asp.net-mvc – Angular 2:如何在不使用路由的情
- asp.net – 分析器错误消息:无法生成代码.抛出了
- asp.net-mvc – ASP.NET MVC中的动态(运行时生成
- asp.net-mvc – HiddenInput(DisplayValue = fal
- asp.net-mvc – ExceptionContext.ExceptionHand
热点阅读