aspnet / AspNetWebFormsDependencyInjection

Dependency injection support for ASP.NET Web Forms 4.x

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Webforms, ObjectDataSource, TypeName Issues

fudgeboy2000 opened this issue · comments

Hey there! I'm trying to introduce DI to a legacy asp.net webforms app. Now that framework version 4.7.2 is here, I thought this would be the right time to have a crack at it!

I've followed this general guide - https://blogs.msdn.microsoft.com/webdev/2018/06/05/use-dependency-injection-in-webforms-application/

I've refactored some business logic classes to allow for constructor injection and registered their types in global.asax. Everything went well and DI works fine for basic cases (classes, webforms, etc). However, I have an issue with using the refactored business logic classes with ObjectDataSources placed onto each page. Each ODS has a TypeName (defined when using the ODS wizard). When asp.net runs each page, I get the error:

"No parameterless constructor defined for this object".

Obviously, this used to work back when the BL classes didn't have constructors at all, but now they have (and they expect a bunch of interfaces to other resources as parameters).

There is a similar issue here - https://stackoverflow.com/questions/3663380/unity-and-asp-net-webforms-no-parameterless-constructor-defined-for-this-objec

I tried the advice about manually changing the ODS's typename to the interface and then using OnObjectCreating and e.ObjectInstance, but that didn't help.

Any advice is appreciated!