Activation error occured while trying to get instance of type LogWriter, key “”?

后端 未结 3 1421
长情又很酷
长情又很酷 2021-02-20 06:46

I have asp.net web site. I added reference of Microsoft.Practices.EnterpriseLibrary.Logging.dll to the site.

in web.cofig file, I defined like below.

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-20 07:21

    EDIT: Under EL 6.0 there is no longer a dependency Unity no longer At the time of last edit (AUG 2013) Unity 3 was the current product. Unit 3.0

    Some of the tips below around the App.config editor for EL is still useful. Some code changes for el5 to el6 are also shown.

    Here is my attempt to clarify. the el5.0 error The error example

    Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException The type LogWriter cannot be constructed. You must configure the container to supply this value.

    The Executive summary: Unity can not see the libraries or unity cant find the app.config entries , the app config entries are incomplete for the library being used, or there are some targeting issues. Pay attention to all of these points.

    Enterprise Library 6 download site Down load the vsix file

    Microsoft.Practices.EnterpriseLibrary.ConfigConsoleV6.vsix

    you use this to edit the app.config properly.

    Still use nuget to install packages, BUT you need the above download to edit the app.config properly

    PM > Install-Package EnterpriseLibrary.Logging
    PM> Install-Package EnterpriseLibrary.Common
    Pm> Install-Package Unity         
    PM> Install-Package EnterpriseLibrary.ExceptionHandling
    

    There are a few more ENT Libraries you may be interested in. http://nuget.org/packages?q=entlib

    using System;  
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Unity;
    using Microsoft.Practices.Unity;  // << SEPARATE since EL6 , unity 3 has own install.
    
    public class abc
    {
        /// 
        /// Unity Container with key dependencies registered
        /// 
        public static UnityContainer UC { get; private set; }
    
        private static IUnityContainer _IUContainer;// not sure if this is needed
    
        public static LogWriter LogWtr;
        public static ExceptionManager ExcMgr;
    
        /// 
        /// Controller Bootstrap to manage as singleton via static properties and therefore ONLY 1 Unity Container
        /// 
        static bootstrap()
        {
            UC = new UnityContainer(); // one container per work process. managing and resolving dependencies
    
            //  DO NO DO THIS WITH NEW VERSION OF Enterprise LIBRARY this was v5, v6 does not need this
            // now we tell unity about the container manager inside EntLib.
            // we dont want 2 containers, so we tell UNity look after EntLib as well please
            //  UC.AddNewExtension();
    
            //No need to add The extensions individually.
            //no longer required and indeed Library documents this as obselete
            //    UC.AddNewExtension();** 
            //================ END OF OLD V5 approach  ========================
    
            LogWtr = UC.Resolve();
            ExcMgr = UC.Resolve();
    
            // other initializations here
        }
    }
    

    Use the config console downloaded ie the VSIX. right click on app.config .

    EntLib Config Console

    The APP.CONFIG file should have a section with the ENtLibraries in use. You need to make sure the app.config web config of the running project has The entries are added when you use the Enterprise library console to configure Logging or exception handling.

     
    

提交回复
热议问题