0

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?

Udgin
  • 119
  • 2
  • 9

1 Answers1

1

Did you verify that the Microsoft.Practices.Unity.Interception.dll and Microsoft.Practices.Unity.Interception.Configuration.dll are located in your app base folder or that they are registered in the GAC?

Did you check that your App.config file is still named App.config after compilation and not renamed to something like MyApp.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.config or Web.config anyway you can drop the ExeConfigurationFileMap and directly call ConfigurationManager.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.

Community
  • 1
  • 1
Sebastian Weber
  • 6,766
  • 2
  • 30
  • 49
  • Thanks! I have just add Microsoft.Practices.Unity.Interception.Configuration.dll to output folder. But one more, can you help me with section system.diagnostic. How i can load this section to my application ? Thanks! – Udgin Jun 01 '12 at 10:09
  • @user1337137 I'm not 100% sure what you are trying to achieve. If you call `Trace.WriteLine(...)` inside your interception behavior the `ConsoleTraceListener` should receive all messages. What do you want to do with the `TraceSource`? – Sebastian Weber Jun 01 '12 at 11:14
  • OK, sorry me. I want to use settings "system.diagnostics" from my configuration file (App.config). It is configuration file in my library (not in console application). I think it is impossible. Thanks in advance! Your help is very useful! – Udgin Jun 04 '12 at 07:29