daiplusplus / AspNetDependencyInjection

`AspNetDependencyInjection` allows "Classic" ASP.NET Web Forms applications to use `Microsoft.Extensions.DependencyInjection` to compose application services using dependency injection.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

aspnet_compiler.exe complains of broken constructor calls when page dependencies span directories (more research needed)

daiplusplus opened this issue · comments

commented

In the original issue reported to developercommunity.visualstudio.com I just moved the file back.

But the issue happened again today while working with some UserControls (*.ascx) in a subdirectory. I found that if I set the UserControl's constructor to public instead of protected then aspnet_compiler.exe stopped complaining.

I can't remember if the constructor was public or protected or internal (or protected internal) in the original issue.

commented

Update: Sometimes the aspnet_compiler.exe complains even if all the relevant constructors are public instead of protected - my workaround for that situation (if moving the files around the filesystem doesn't work) is to remove the control's registration (from web.config and all <%@ Register directives) and use LoadControl() instead. In my current project I have a generic helper-method which works fine and looks something like this:

public static TControl LoadControl<TControl>( this TemplateControl ctrl, String virtualPath ) where TControl : UserControl => (TControl)ctrl.LoadControl( virtualPath );

But I've also added a static class UserControlRegistry which combines const String virtualPath values with TControl and is no-less maintenance work than having to maintain the registrations in web.config - plus as a bonus I added a file-exists and type-matches checks so we can get eager failure instead of lazy-failure (which is always a lovely surprise).

commented

I remember this issue (or a similar aspnet_compiler error message) being worse when I was using UserControls in .aspx/.ascx files using the <prefix:UserControlName runat="server" /> syntax (with and without the UserControl being registered in the root Web.config, a subdirectory Web.config, or inline in the .aspx file's header.

Side note: I wish I found this SO posting when I started upgrading my last WebForms project that really could have benefited from using Template Properties as a way to simulate Razor Helpers... but don't Templates have to use DataBinding syntax instead of normal render syntax? If so, that makes it pointless... https://stackoverflow.com/questions/11658053/how-to-add-a-templating-to-a-usercontrol