Upgrading to Enterprise Library 6.0 giving problems with "EnterpriseLibraryContainer"

After upgrading to Enterprise Library 6.0, I had the following problem:

closed static container IUnityContainer = EnterpriseLibraryContainer.Current.GetInstance ();

Cannot resolve EnterpriseLibraryContainer

I found another post that talks about EnterpriseLibraryCOntainer on stack overflow.site/questions/686947 / ...

The Enterprise Library update notes indicate:

"The name" EnterpriseLibraryContainer "does not exist in the current context

The boot code for all blocks has changed in version 6 of the corporate library. Blocks no longer use Unity to control initialization and configuration, and each block now includes its own boot code. Any calls to the EnterpriseLibraryContainer.Current.GetInstance method to solve the problem of a type from one of the Enterprise Library blocks should be replaced with a block-specific bootstrap code. For example, to create a LogWriter instance based on the configuration in the app.config file, you can now use the following code: LogWriterFactory logWriterFactory = new LogWriterFactory (); var logWriter = logWriterFactory.Create ();

But I do not know how to do this in the case of IUnityContainer. Can i just use

IUnityContainer container = new UnityContainer?

thanks for the help

+4
source share
2 answers

A typical approach is to load a block, register the corresponding objects with Unity, and add Unity dependencies.

For example, if you use logging, then you run the block:

LogWriterFactory logWriterFactory = new LogWriterFactory(); LogWriter logWriter = logWriterFactory.Create(); 

and register LogWriter using UnityContainer:

 IUnityContainer container = new UnityContainer(); // Register LogWriter as singleton container.RegisterInstance<LogWriter>(logWriter); 

If you used the EnterpriseLibraryContainer as a service locator and want to continue using the same approach, you can create / wrap an implementation of the service locator or create a static helper method. Unity comes with a UnityServiceLocator that can be reused.

If you are not using Unity, another approach would be to load the blocks and then replace the calls on EnterpriseLibraryContainer.Current.GetInstance<>() with static facade methods (e.g. Logger.Write() ).

+4
source

I really struggled with this, so here is my working upgrade from Enterprise 5 to v6 (free). I mention that the Codeplex Migration PDF file still displays an EnterpriseLibraryContainer, which is no longer valid.

//app.config

 <configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> </configSections> 

// my class is logger using the system; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging;

 /// <summary> /// Set up the Enterprise logging class /// </summary> public static class Logging { /// <summary> /// Define the logging parameters /// </summary> public static void DefineLogger() { var builder = new ConfigurationSourceBuilder(); string loggerPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); builder.ConfigureLogging() .WithOptions .DoNotRevertImpersonation() .LogToCategoryNamed("LogWriter") .WithOptions.SetAsDefaultCategory() .SendTo.RollingFile("Rolling Flat File Trace Listener") .RollAfterSize(1000) .FormatWith(new FormatterBuilder() .TextFormatterNamed("Text Formatter") .UsingTemplate(@"Timestamp: {timestamp}{newline}Message: {message},{newline}Category: {category},{newline}Severity: {severity},{newline}Title:{title},{newline}ProcessId: {localProcessId},{newline}Process Name: {localProcessName},{newline}Thread Name: {threadName}")) .ToFile(loggerPath + Properties.Settings.Default.LogFileFolder); // Reference app.config using (DictionaryConfigurationSource configSource = new DictionaryConfigurationSource()) { builder.UpdateConfigurationWithReplace(configSource); Logger.SetLogWriter(new LogWriterFactory(configSource).Create()); } } } 

Finally, in any old exception:

 Logger.Write("This is my log"); 

Hope this helps save a lot of time.

+1
source

Source: https://habr.com/ru/post/1488236/


All Articles