We have a problem with autofac webforms listed here https://github.com/autofac/Autofac/issues/1410 if you can take a look and give us an answer? they asked us to write here. Thank you
AutoFac WebForms object[]
in WebForms in page if you have any property of type object[] it is always set to empty array on page load or any event in page. even if we set it somewhere else it reset to empty after that event. this behavior only for properties of type object[] .(may be also for others but I discovered this only) it makes my WebForms app not work properly. in my WebForms app i used the follwing:
- Autofac
- Autofac.Multitenant
- Autofac.Integration.Web
- Autofac.Integration.WebApi
- Autofac.Integration.Mvc
- AutoMapper.Contrib.Autofac.DependencyInjection
also in Global.ascx.cs I have scanned for all assemblies referenced in my large project to be registered in RegisterControllers and RegisterApiControllers
example:
public partial class MyPage : Equipe.WebControls.Page
{
public IService Service { get; set; }
private object[] _myArray;
public object[] MyArray
{
get => this._myArray;
----> set => this._myArray = value; // her it always set to empty object[]
}
}
Expected behavior
Expected behavior is that MyArray shouldn't injected or set to empty array
Autofac replayed with answer here https://github.com/autofac/Autofac/issues/1410 and I answered her: from the documentation as you highlighted is that "The enumerable support will return an empty set if no matching items are registered in the container" this behavior will affect the programming logic in case of webforms applications for those reasons:
- the webforms pages or controls can use the ViewState[] or SessionState[] to store data came from outside of page and usually used inside a properties like this:
public partial class MyControl: UserControl
{
public object[] SelectParameters
{
get => (object[])this.ViewState["SelectParameters"];
set => this.ViewState["SelectParameters"] = value;
}
}
// the code of the page MyPage.aspx. using this control as the following :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyPage" %>
<%@ Register TagPrefix="Controls" TagName="Comunicazioni" Src="~/MyControl.ascx" %>
<Controls:MyControlID="MyControlId" runat="server" />
// the MyPage.aspx.cs
public partial class MyPage: Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.MyControlId.SelectParameters= new object[]{1,"A", new User()};
}
}
what is the problem her?
- the problem is that MyPage set the property SelectParameters of the control MyControl and this set happened before the Page_Load in the page lifecycle . it means the setting happened before the page lifecycle start.
- the Autofact start doing its setting for MyControl return an empty set if no matching items are registered in the container, it set SelectParameters to empty array causing the value that have been set from page to be empty again.
- the problem will be more bigger when we create pages and control programmatically and setting its properties in advance. the return an empty set if no matching items are registered in the container behavior will reset all again
the suggestion (maybe non practical)
- the resolver should do its injection before any step in page lifecycle and before any property set outside the page.
- types not registered shouldn't be set or resolved.
- the programmatically created page or controls should have special treatment considering those problems