autofac / Autofac

An addictive .NET IoC container
https://autofac.org
MIT License
4.5k stars 835 forks source link

AutoFac WebForms object[] problem #1410

Closed WaleedRomaema closed 9 months ago

WaleedRomaema commented 9 months ago

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:

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

tillig commented 9 months ago

There is no Autofac integration with Winforms applications. If the object is being incorrectly set, it's due to something in your application code, not a fault with Autofac.

Do be aware that if you resolve any sort of IEnumerable<T> (which includes arrays, collections, lists, etc.) that if nothing is registered for that T you will get an empty collection.

The documentation explains this.

The enumerable support will return an empty set if no matching items are registered in the container. That is, using the above example, if you don’t register any IMessageHandler implementations, this will break:

// This throws an exception - none are registered!
scope.Resolve<IMessageHandler>();

However, this works:

// This returns an empty list, NOT an exception:
scope.Resolve<IEnumerable<IMessageHandler>>();
WaleedRomaema commented 9 months ago

thanks for your kind and useful replay, sorry for miss typing WinForms i meant Webforms I updated the question.

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 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" %>

// 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
tillig commented 9 months ago

If this is a web forms issue you should file it in the web forms repo. As noted on the issue template, this repo is for core Autofac issues.

You may also want to take the time to read the web forms integration docs if you're not familiar because it tells about some gotchas and ways you can configure the injection to, for example, only inject unset properties.

WaleedRomaema commented 9 months ago

we already read (https://autofac.readthedocs.io/en/latest/integration/webforms.html) make name="AttributedInjection" type="Autofac.Integration.Web.Forms.AttributedInjectionModule, Autofac.Integration.Web" preCondition="managedHandler" in config we still have same problem

WaleedRomaema commented 9 months ago

we posted here https://stackoverflow.com/questions/77926740/webforms-ienumerable-property-set-to-empty-and-reset-its-value , thank you