Configuring Unity 3.0

Nov 26, 2012 at 10:53 PM
Edited Nov 27, 2012 at 8:09 AM

I am trying to configure the unity 3.0 preview framework in a app.config, can you please give an example of how to set this up please?

Trying to achieve something like this as you would use in Enterprise Library 5.0. When I run this type of config I get error

An error occurred creating the configuration section handler for unity: Could not load file or assembly 'Microsoft.Practices.Unity.Configuration, Version=3.0.1026.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) (C:\Projects\Code Detox Ltd\Common\LoggingTestHarness\CommonLoggingTest\CommonLoggingTest\bin\Debug\CommonLoggingTest.vshost.exe.Config line 6)

 

Any help would be appreciated

 

<configSections>
    <section name="unity" type=" Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=3.0.1026.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </configSections>

 

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <container>

      <register type="CodeDetox.Common.Logging.Interface.ICdLogger, CodeDetox.Common.Logging" mapTo="CodeDetox.Common.Logging.WinLogger.Log, CodeDetox.Common.Logging.WinLogger" name="EventLog"/>

      <register type="CodeDetox.Common.Logging.Interface.ICdLogger, CodeDetox.Common.Logging" mapTo="CodeDetox.Common.Logging.TextFile.Log, CodeDetox.Common.Logging.TextFile" name="TextFile"/>      <register type="CodeDetox.Common.Logging.Interface.ICdLogger, CodeDetox.Common.Logging" mapTo="CodeDetox.Common.Logging.Console.Log, CodeDetox.Common.Logging.Console" name="Console"/>
      <register type="CodeDetox.Common.Logging.Interface.ICdConsoleLogger, CodeDetox.Common.Logging" mapTo="CodeDetox.Common.Logging.Console.ConsoleLog, CodeDetox.Common.Logging.Console" name="Console"/>
    </container>

Nov 27, 2012 at 5:42 AM

Perhaps you have a different version of Unity than is specified in the configuration file?  The latest package available via Nuget is 3.0.1208 which is different than the 3.0.1026.0 specified in the configuration file.  The latest package can be downloaded using: "Install-Package Unity.Interception -Pre" from the package manager console.

Probably if you remove the version from the configuration it will work fine:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type=" Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <container>

      <register type="CodeDetox.Common.Logging.Interface.ICdLogger, CodeDetox.Common.Logging" mapTo="CodeDetox.Common.Logging.WinLogger.Log, CodeDetox.Common.Logging.WinLogger" name="EventLog"/>

      <register type="CodeDetox.Common.Logging.Interface.ICdLogger, CodeDetox.Common.Logging" mapTo="CodeDetox.Common.Logging.TextFile.Log, CodeDetox.Common.Logging.TextFile" name="TextFile"/>
      <register type="CodeDetox.Common.Logging.Interface.ICdLogger, CodeDetox.Common.Logging" mapTo="CodeDetox.Common.Logging.Console.Log, CodeDetox.Common.Logging.Console" name="Console"/>
      <register type="CodeDetox.Common.Logging.Interface.ICdConsoleLogger, CodeDetox.Common.Logging" mapTo="CodeDetox.Common.Logging.Console.ConsoleLog, CodeDetox.Common.Logging.Console" name="Console"/>
    </container>
  </unity>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

Here's an example that works for me:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type=" Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

    <namespace name="Unity3" />
    <assembly name="Unity3" />

    <container>

      <register type="ICdLogger" mapTo="ConsoleLogger" name="Console" />
      <register type="IProcessor" mapTo="Processor">
        <constructor>
          <param name="logger" dependencyName="Console" />
        </constructor>
      </register>

    </container>

  </unity>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

 

    public interface IProcessor
    {
        void Process();
    }

    public class Processor : IProcessor
    {
        private ICdLogger logger;

        public Processor(ICdLogger logger)
        {
            this.logger = logger;
        }

        public void Process()
        {
        }
    }

    public interface ICdLogger
    {
        void Write(string msg);
    }

    public class ConsoleLogger : ICdLogger
    {
        public void Write(string msg)
        {
            Console.WriteLine(msg);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer().LoadConfiguration();

            var processor = container.Resolve<IProcessor>();
        }
    }

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Nov 27, 2012 at 8:10 AM

Hi Randy,

 Thanks for your speedy response. :o)

 I try this, I think I have the latest version but how can I get the version number of the assembly I have?

I had a feeling it was the version number by the stack trace error and I was trying to find the version number before posting, how do you work this out?

I could not see a version number in the source code and there was nothing in the assemblyinfo file. I was trying to see may be a way this could be done using sn.exe.

Look forward to your response and I will try the above later and will let you know if it works. ;o)

 

Cheers

 

Bobby

Nov 27, 2012 at 8:36 AM

Easiest way to see the version is to view the properties of the reference in Visual Studio.  You can also view the properties of the assembly in explorer: right click->Properties->Details.  There you can see File Version and Product Version (which is the assembly version).  See Assembly Versioning.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Nov 27, 2012 at 10:41 AM
Edited Nov 27, 2012 at 10:46 AM

Hi Randy,

Thanks for your response. And I was able to see the version based on your response. Was obvious as soon as you mentioned this.

The code works but I see the following response in the debugger, my untiy references are pointing a compiled version of source; But if I ignore the error and f5 the console logger works through Unity v3.0

 

Locating source for 'e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs'. Checksum: MD5 {8a c3 5d 38 74 60 75 9b 73 d2 8e 51 f2 9c 9c 1a}
Determining whether the checksum matches for the following locations:
1: C:\Unity3\Source\Unity\Src\UnityContainer.cs Checksum: MD5 {25 81 3b 53 86 f0 c4 ec cf 8a 7c fd 7 2 8b 45} Checksum doesn't match.
The file 'e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs' does not exist.
Looking in script documents for 'e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs'...
Looking in the projects for 'e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs'.
The file was found in a project: 'C:\Unity3\Source\Unity\Src\UnityContainer.cs'.
Determining whether the checksum matches for the following locations:
1: C:\Unity3\Source\Unity\Src\UnityContainer.cs Checksum: MD5 {25 81 3b 53 86 f0 c4 ec cf 8a 7c fd 7 2 8b 45} Checksum doesn't match.
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\crt\src\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\crt\src\vccorlib\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\src\mfc\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\src\atl\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include'...
Looking in directory 'C:\Unity3\Source\'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs.
The debugger could not locate the source file 'e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs'.

Any ideas why I see this error?

Cheers

Bobby

Nov 28, 2012 at 7:41 AM

Perhaps there are some rogue assemblies or PDBs being loaded?  Try removing all files from the bin/debug and obj directories.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com