Wish you all good mood!
Please help me with Unity.
My App.config file contains:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<system.diagnostics>
<sources>
<source name="TraceTest" switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<add name="console" />
<remove name ="Default" />
</listeners>
</source>
</sources>
<switches>
<add name="SourceSwitch" value="All" />
</switches>
<sharedListeners>
<add name="console"
type="System.Diagnostics.ConsoleTraceListener"
initializeData="false"/>
</sharedListeners>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="console" />
</listeners>
</trace>
</system.diagnostics>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="ContextDownloader"/>
<namespace name="ContextDownloader.Log"/>
<namespace name="System.Diagnostics"/>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,
Microsoft.Practices.Unity.Interception.Configuration"/>
<container>
<extension type="Interception"/>
<register type="ILogWorker" mapTo="FileLogWorker">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="TraceBehavior"/>
</register>
<register type="TraceSource" name="interception">
<constructor>
<param name="name" type="System.String" value="TraceTest" />
</constructor>
</register>
<register type="TraceBehavior">
<constructor>
<param name="source" dependencyName="interception" />
</constructor>
</register>
</container>
</unity>
</configuration>
I load configuration from App.config in my code:
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "App.config" };
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
var container = new UnityContainer();
So it throws exception
Unhandled Exception: System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for unity: The type name or alias Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigu
rationExtension, Microsoft.Practices.Unity.Interception.Configuration could not be resolved. Please check your configuration file and verify this type name.
in line var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");. How I can load Microsoft.Practices.Unity.Interception.Configuration assembly?
More details about application. I have console application and library. I have just call one method in my console application and then all logic are in library.
Thanks for your answer about copy this Microsoft.Practices.Unity.InterceptionExtension.Configuration.dll to output library. Can you help me to load system.diagnostic section too?
Did you verify that the
Microsoft.Practices.Unity.Interception.dllandMicrosoft.Practices.Unity.Interception.Configuration.dllare located in your app base folder or that they are registered in the GAC?Did you check that your
App.configfile is still namedApp.configafter compilation and not renamed to something likeMyApp.Foo.dll.config?Could you please post the complete config file. The above snippet works perfectly on my machine so I guess there is something else missing.
Btw: If you want to use the default
App.configorWeb.configanyway you can drop theExeConfigurationFileMapand directly callConfigurationManager.GetSection("unity")Update
So you have an application which is not a console app (e.g. WinForms or WPF) and want to write trace output to a console? Then maybe this article can help. It shows how to allocate a console window using native Win32 calls from managed code.
If you want to use the configuration file that is bundled with the library and independent of the configuration file of your application this article here on StackOverflow might be of interest.