zcz527 / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Exception Occurs with InjectProperties and Collection Properties on Target #464

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
I've attached a code sample that demonstrates the problem. Essentially, if you 
have an object that has a property on it that's some kind of collection (e.g. 
ReadOnlyCollection<int>), Autofac will try to resolve it and fail, even though 
there's nothing in the scope that will match it.

What is the expected output? What do you see instead?
Autofac should just ignore the property. What I get is this exception:

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Object of type 'System.Int32[]' cannot be converted to type 'System.Collections.ObjectModel.ReadOnlyCollection`1[System.Int32]'.
  Source=mscorlib
  StackTrace:
       at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
       at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
       at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
       at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
       at Autofac.Core.Activators.Reflection.AutowiringPropertyInjector.InjectProperties(IComponentContext context, Object instance, Boolean overrideSetValues)
       at Autofac.ResolutionExtensions.InjectProperties[TService](IComponentContext context, TService instance)
       at AutofacIssue.Program.InjectPropertiesWithCollection() in d:\JasonBock\AutofacIssue\AutofacIssue\AutofacIssue\Program.cs:line 24
       at AutofacIssue.Program.Main(String[] args) in d:\JasonBock\AutofacIssue\AutofacIssue\AutofacIssue\Program.cs:line 11
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

What version of Autofac are you using? On what version of .NET/Silverlight?
3.1.3 on .NET 4.5. Note that 3.1.1 works just fine.

Please provide any additional information below.

Original issue reported on code.google.com by jason.bo...@gmail.com on 28 Oct 2013 at 4:57

Attachments:

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 28 Oct 2013 at 11:31

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
After upgrade to 3.1.3 same problem:

public partial class Controls_SectorSeatsControl : System.Web.UI.UserControl
{
    public List<SiteSectorSeatInfo> SectorSeats { get; set; }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;
        var cp = cpa.ContainerProvider;
        cp.RequestLifetime.InjectProperties(this);
    }
}

but when I change List to IEnumerable it works.

.net 4.5, autofac 3.1.3, 3.1.1 worked fine

Original comment by hackfaq on 29 Oct 2013 at 3:54

GoogleCodeExporter commented 8 years ago
This issue was closed by revision f6f3bd5e8a39.

Original comment by alex.meyergleaves on 29 Oct 2013 at 8:39

GoogleCodeExporter commented 8 years ago
I have pushed the Autofac 3.1.4 package to NuGet.

Original comment by alex.meyergleaves on 29 Oct 2013 at 8:44

GoogleCodeExporter commented 8 years ago
Unfortunately 3.1.4 still has issues. I've attached an updated version of my 
sample. If I have a List<object> as a property on the object that is passed to 
InjectProperties(), I get the same error as before:

Unhandled Exception: System.ArgumentException: Object of type 'System.Object[]'
cannot be converted to type 'System.Collections.Generic.List`1[System.Object]'.

Original comment by jason.bo...@gmail.com on 29 Oct 2013 at 1:06

Attachments:

GoogleCodeExporter commented 8 years ago
I just did some clean up moments before you posted this and found the exact 
same issue. Do you mind grabbing the latest NuGet package from the MyGet CI 
feed and giving that a quick test?

https://www.myget.org/Package/Details/autofac?packageId=Autofac&packageVersion=3
.1.4-CI-030

I can push another package to nuget.org if it looks alright.

Original comment by alex.meyergleaves on 29 Oct 2013 at 1:25

GoogleCodeExporter commented 8 years ago
Yep, that 3.1.4-CI-030 worked.

Original comment by jason.bo...@gmail.com on 29 Oct 2013 at 2:26

GoogleCodeExporter commented 8 years ago
Pushed 3.1.5 with the additional fixes to the NuGet.org gallery.

Original comment by alex.meyergleaves on 29 Oct 2013 at 2:29