开发者

Dependency injection into custom ViewPage generates weird error

i'm trying to inject stuff into a custom ViewPage (ModelViewPage, from MvcContrib)

public class ValidatedModelViewPage<T> : ModelViewPage<T> where T : class
{
    public ValidatedModelViewPage(IEnumerable<IBehavior<IMemberElement>> elementBehaviors)
       : base(elementBehaviors.ToArray()) { }
}

and my Autofac registrations look like this:

builder.RegisterCollection<IBehavior<IMemberElement>>().As<IEnumerable<IBehavior<IMemberElement>>>();
builder.Register<NotNullBehavior>().MemberOf<IEnumerable<IBehavior<IMemberElement>>>();
builder.Register<StringLenghBehavior>().MemberOf<IEnumerable<IBehavior<IMemberElement>>>();

builder.RegisterGeneric(typeof(ValidatedModelViewPa开发者_Go百科ge<>));

but i get this error when i try to access a view:

Compiler Error Message: CS1729: 'UKFS.Web.Views.ValidatedModelViewPage<UKFS.Data.Entities.Skadeanmälan>' does not contain a constructor that takes '0' arguments

Source Error:

Line 194:        private static object @__fileDependencies;
Line 195:        
Line 196:        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 197:        public views_skadeanmälan_edit_aspx() {
Line 198:            string[] dependencies;


Source File: c:\Users\Carl\AppData\Local\Temp\Temporary ASP.NET Files\root\be9ddc15\a84d5058\App_Web_edit.aspx.b2d4184a.thgwih90.0.cs    Line: 196 

i were clueless, but then i looked at App_Web_edit.aspx.b2d4184a.thgwih90.0.cs and found this:

Line 190:      public class views_skadeanmälan_edit_aspx : UKFS.Web.Views.ValidatedModelViewPage<Skadeanmälan>, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
Line 191:          
Line 192:          private static bool @__initialized;
Line 193:          
Line 194:          private static object @__fileDependencies;
Line 195:          
Line 196:          [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 197:          public views_skadeanmälan_edit_aspx() {

of course, the generated class views_skadeanmälan_edit_aspx inheritates from my UKFS.Web.Views.ValidatedModelViewPage, and when it tries to instance it with the default construct.. so can you solve it?


You won't be able to constructor inject into ViewPages because the aspx compiler generates an empty ctor and as it derives from your base-class, your base-class must also have a empty ctor.

I'd look for property injection instead, otherwise try to accomplish the following:

  • Find the place where viewpages are instantiated, and get the Autofact there to instantiate the page
  • get the aspx compiler to not generate the empty ctor

I don't know how to do those things, so I'd aim for property injection instead

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜