Unity: The type LogWriter cannot be constructed

穿精又带淫゛_ 提交于 2019-11-29 14:02:12

You need to add the Enterprise Library extension to your container. Without it, the container doesn't read the config file and therefore doesn't know how to create Entlib objects, like the LogWriter.

user1005462

Activation error occured while trying to get instance of type LogWriter, key ""

check your config file it must have correct default 'loggingConfiguration' section

In order for Unity to construct your Performance class, it needs to know how to construct the implementation of ILogWriter.

I cannot see anywhere in your code where you tell Unity what class to create for the ILogWriter interface, so I suspect you may need to add this.

Try this:

protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType<ILogWriter, LogWriter>();
                container.RegisterType<ExceptionManager>();
                container.RegisterType<Performance>(new InjectionConstructor(typeof(ILogWriter), typeof(ExceptionManager));                    
                Performance p = container.Resolve<Performance>();
            }
            catch (Exception ex)
            {

            }
        }
Tallmaris

LogWriter does not have an empty constructor or a constructor with all concrete types parameters: LogWriter Constructor.

So Unity fails in building it and as it says, it will need your help by configuring the container to provide an implementation.

As a confirmation, ExceptionManager will probably be resolved ok, since it has only one constructor, parameterless as well :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!