locked
"The type Database cannot be constructed. You must configure the container to supply this value." RRS feed

  • Question

  • Working on creating some unit tests for the DAL classes on a project. Getting the following error:

    Test method Unit_Tests.<test name> threw exception:
    Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type Database, key "<database name>" ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "<database name>".
    Exception occurred while: while resolving.
    Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.
    -----------------------------------------------
    At the time of the exception, the container was:

      Resolving Microsoft.Practices.EnterpriseLibrary.Data.Database,<database name>
     ---> System.InvalidOperationException: The type Database cannot be constructed. You must configure the container to supply this value.

     

    I have tried adding a .config file with the correct values and I have tried linking it to the web.config file in the other project, but I still get this error. Any suggestions?

    Monday, April 18, 2011 4:46 PM

Answers

All replies

  • This is the Enterprise Library rearing its ugly head.  There are a whole lot of discussions about this particular problem, and the best place to ask is on the discussion page on the EntLib CodePlex site.

    Check this thread and see if it helps you:

    http://entlib.codeplex.com/discussions/215290

     

    Evan

    Tuesday, April 19, 2011 7:16 PM
  • Providing the connection string constructor argument as an array of object in your Bootstrapper might resolve the issue - if you are using unity for DI.

    You may not get a syntax or even build error when you register the Database as follows.

    .RegisterType<Database, SqlDatabase>(connectionString)

    But, it will fail and end up getting with the above error when you run your app.

    Instead, if you pass the parameter as an array of objects - even when you have only one parameter to pass (in this case connection string), it will solve the problem.

    .RegisterType<Database, SqlDatabase>(new object[] { connectionString })

    Hope it helps.

    Bsharper

     

     


    • Proposed as answer by Hammer2 Friday, November 6, 2015 4:36 PM
    Thursday, November 17, 2011 8:32 PM
  • It was damn $%@#$#^% frustrating to get this resolved. Took couple of days to fix this, guys. Urrggghhhh.

    Error: Activation error occured while trying to get instance of type Database, key ""

    Inner Exception: Resolution of the dependency failed, type = Microsoft.Practices.EnterpriseLibrary.Data.Database

    Specifics: I was running VS 2010 on Windows 7, Enlib 5.0.

    The following worked for me. Wanted to spread the word around. It may work for you guys.

    1. Make sure you have proper reference to Microsoft.Practices.Unity.dll

    2. Get the latest service pack for VS 2010


    TSP


    • Edited by Senthil1 Wednesday, February 22, 2012 9:38 AM
    • Proposed as answer by Mohamadk Thursday, June 7, 2012 4:43 AM
    Wednesday, February 22, 2012 9:31 AM
  • actually spend more time with issues like this than writing or debugging my own code. No exaggeration. This is why i like to stick to database work when possible.
    Thursday, March 1, 2012 9:36 PM
  • Finally,

     when I try to access Ent Lib 5.0 Caching methods from a custom web part (SP2010)  i got the same error, all Ent Lib 5.0 DLLs were moved to GAC and added all assemblies to web application's web.config( <add assembly="Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> ) and of course caching settings were added to web.config

    Still it was not working. when I moved dlls to web application's bin folder, web part started working!! 

    Monday, August 13, 2012 11:24 AM
  • Yes,

    I was also facing the same issues, though i had all the correct entries in web.config.

    By moving all referred EntLib dlls into Web bin folder, it started working !!

    Wednesday, December 5, 2012 4:40 AM