views:

50

answers:

1
    public ApplicationView(string[] args)
    {
        InitializeComponent();

        string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
        FileInfo configFileInfo = new FileInfo(configFilePath);
        XmlConfigurator.ConfigureAndWatch(configFileInfo);

        IConfigurationSource configSource = ConfigurationManager.GetSection("ActiveRecord") as IConfigurationSource;
        Assembly assembly = Assembly.Load("Danel.Nursing.Model");
        ActiveRecordStarter.Initialize(assembly, configSource);         

        WindsorContainer windsorContainer = ApplicationUtils.GetWindsorContainer();
        windsorContainer.Kernel.AddComponentInstance<ApplicationView>(this);
        windsorContainer.Kernel.AddComponent(typeof(ApplicationController).Name, typeof(ApplicationController));
        controller = windsorContainer.Resolve<ApplicationController>(); // exception is thrown here

        OnApplicationLoad(args);
    }

The stack trace is this:

Castle.MicroKernel.ComponentActivator.ComponentActivatorException was unhandled
Message="ComponentActivator: could not instantiate Danel.Nursing.Scheduling.Actions.DataServices.NurseAbsenceDataService" Source="Castle.MicroKernel"
StackTrace: at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, Object[] arguments, Type[] signature) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context) at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context) at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context) at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveServiceDependency(CreationContext context, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context, Type[]& signature) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context) at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context) at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context) at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveServiceDependency(CreationContext context, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context, Type[]& signature) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context) at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context) at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context) at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveServiceDependency(CreationContext context, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context, Type[]& signature) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context) at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context) at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context) at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context) at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments) at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service) at Castle.MicroKernel.DefaultKernel.get_Item(Type service) at Castle.Windsor.WindsorContainer.Resolve(Type service) at Castle.Windsor.WindsorContainer.ResolveT at Danel.Nursing.Scheduling.ApplicationView..ctor(String[] args) in E:\Agile\Scheduling\Danel.Nursing.Scheduling\ApplicationView.cs:line 65 at Danel.Nursing.Scheduling.Program.Main(String[] args) in E:\Agile\Scheduling\Danel.Nursing.Scheduling\Program.cs:line 24 at System.AppDomain._nExecuteAssembly(Assembly 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.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.ArgumentNullException Message="Value cannot be null.\r\nParameter name: types" Source="mscorlib" ParamName="types" StackTrace: at System.Type.GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.FastCreateInstance(Type implType, Object[] arguments, Type[] signature) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, Object[] arguments, Type[] signature) InnerException:

It actually says that the type that I'm trying to initialize does not exist, I think. This is the concreate type that it complains about:

   namespace Danel.Nursing.Scheduling.Actions.DataServices
    {
        using System;
        using Helpers;
        using Rhino.Commons;
        using Danel.Nursing.Model;
        using NHibernate.Expressions;
        using System.Collections.Generic;
        using DateUtil = Danel.Nursing.Scheduling.Actions.Helpers.DateUtil;
        using Danel.Nursing.Scheduling.Actions.DataServices.Interfaces;

        public class NurseAbsenceDataService : AbstractDataService<NurseAbsence>, INurseAbsenceDataService
        {
            NurseAbsenceDataService(IRepository<NurseAbsence> repository)
                : base(repository)
            {
            }

            //...
        }
    }

The AbstractDataService only holds the IRepository for now. Anyone got an idea why the exception is thrown?

EDIT:
The problem lays here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Danel.Nursing.Scheduling.Actions.Interfaces;
using Rhino.Commons;
using System.Reflection;
using Castle.Windsor;

namespace Danel.Nursing.Scheduling.Actions.Helpers
{
    public class DataServicesInitiator : IDataServicesInitiator
    {
        public void AddToIocContainer(ref WindsorContainer windsorContainer)
        {
            if (windsorContainer.Kernel.HasComponent(typeof(IRepository<>)) == false)
                windsorContainer.AddComponent(typeof(ARRepository<>).FullName, typeof(IRepository<>), typeof(ARRepository<>));
            foreach (Type type in Assembly.Load("Danel.Nursing.Scheduling.Actions").GetTypes())
            {
                if (type == null)
                    continue;

                if (type.Namespace != "Danel.Nursing.Scheduling.Actions.DataServices")
                    continue;

                if (type.IsAbstract || type.IsInterface)
                    continue;

                if (windsorContainer.Kernel.HasComponent(type.FullName))
                    continue;

                if (type.GetInterfaces().Length == 0)
                    windsorContainer.AddComponent(type.FullName, type);
                else
                    windsorContainer.AddComponent(type.FullName, type.GetInterfaces()[0], type);
            }
        }
    }
}

For some reason type is null although GetTypes return no null types.

+1  A: 

What does the inner exception say? It looks like the constructor for the type threw an exception or it was unable to properly call the constructor for some other reason...

Update

You can get everything - ActiveRecord, Windsor with MicroKernel, DynamicProxy and NHibernate by downloading ActiveRecord package. It's all there.

If you don't want to update you may take alternative approach - write a custom Activator for this component (inherit from the default one and override the method which exposes the bug.) This should be the safest workaround.

Krzysztof Koźmic
NullArgumentException: types parameter is null
the_drow
Which version do you have?
Krzysztof Koźmic
The last version (don't know about previous ones) does explicit check for null and initializes the parameter before calling the `type.GetConstructor` which is what I believe is causing the exception in your case. So I reckon you have some old version.
Krzysztof Koźmic
I have version 1.0.3.0
the_drow
@Krzysztof Koźmic: Edited. Please take a look
the_drow
Current version of Windsor is 2.1, and 2.5 is just round the corner. I really suggest you to upgrade.
Krzysztof Koźmic
Are you able to reproduce this outside of Windsor?
Krzysztof Koźmic
@Krzysztof Koźmic: Upgrading it breaks my project completely.What do you mean outside of Windsor?
the_drow
By outside of Windsor I mean can you reproduce the issues w/o using Windsor.I'd suggest you to take the time to upgrade to later version where I'm pretty sure the issue does not exist
Krzysztof Koźmic
@Krzysztof Koźmic: I have no idea how to start reproducing it w/o using Windsor. What exactly do you mean? If I don't add it to the WindsorContainer I get another exception. 'Waiting for dependencies'
the_drow
ok, scratch it. It makes more sense for you to update to later version of Windsor and the problem should go away.
Krzysztof Koźmic
@Krzysztof Koźmic: Is there no easy fix here? Upgrading might take a week.
the_drow
Honestly I don't know. Like you said it's very bizarre, and I don't really know how to help you, since I don't know the real cause of this.There's a massive number of updates The version you have is 3 years old, and there were two big releases in the meantime and another is approaching.I haven't seen RC3 version for a while, but there weren't too many breaking changes so update should be pretty straightforward. Shoot me an email if you need any assistance with it.
Krzysztof Koźmic
I'm marking this as the accepted answer although I got almost none. I will contact you through your blog. I'm saying I will have problems since ActiveRecord apearently depeands on MicroKernel which is upgraded as well. And then I have to upgrade it and NHibernate with many many breaking changes.
the_drow
@Krzysztof Koźmic: Email sent :/
the_drow
Updating breaks my code and it will take weeks to fix it. What do you mean write an Activator and expose the bug?
the_drow
I mean, since the bug is in `DefaultComponentActivator` then swap the activator for your own implementation that does not have the bug.
Krzysztof Koźmic