Problems when creating a new Database Provider

Topics: Building and extending application blocks, Data Access Application Block
Sep 17, 2010 at 10:39 AM

Hello,

i am trying to add a new Database Provider for PostgreSql. That's what I did:

  1. I have created a new class derviced from the Databas class (I have used an existing provider as model (from EntlibContrib)).
  2. Then I have created a new call derived from DatabaseData
  3. Set the ConfigurationElementType to my newly created class
  4. Last I have configured my Demo-App with config as follows
<configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />
    </configSections>
    <dataConfiguration>
        <providerMappings>
            <add databaseType="Data.PostgreSql.NpgsqlDatabase, Data.PostgreSql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="NpgsqlDatabase" />
        </providerMappings>
    </dataConfiguration>
    <connectionStrings>
        <add name="PostgreSql" connectionString="Server=127.0.0.1;Port=5432;Database=myDataBase;User Id=myUsername;Password=myPassword;"
            providerName="NpgsqlDatabase" />
    </connectionStrings>

Now when I try to create a new instance of my custom provider like this

var customerDb = EnterpriseLibraryContainer.Current.GetInstance<Database>("PostgreSql");

or

var customContainer = new UnityContainer()
                .AddNewExtension<EnterpriseLibraryCoreExtension>();

var npsqlDatabase = customContainer.Resolve<Database>("PostgreSql");

I get the following exception for creation with service locator: "Activation error occured while trying to get instance of type Database, key \"PostgreSql\""

Stack Trace:

   bei Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:Zeile 53.
   bei Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:Zeile 103.
   bei Test.Program.Main(String[] args) in C:\Projects\EnterpriseLibraryExtensions\Test\Program.cs:Zeile 21.
   bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   bei System.Threading.ThreadHelper.ThreadStart()

When I am trying the unity container directly the following error occurs:

Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "NpgsqlDatabase".
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,PostgreSql

Stack Trace:

   bei Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:Zeile 515.
   bei Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:Zeile 485.
   bei Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:Zeile 173.
   bei Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, String name, ResolverOverride[] overrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainerExtensions.cs:Zeile 518.
   bei Test.Program.Main(String[] args) in C:\Projects\EnterpriseLibraryExtensions\Test\Program.cs:Zeile 27.
   bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   bei System.Threading.ThreadHelper.ThreadStart()

What im doing wrong?

Thanks in advance!
Steve


Sep 20, 2010 at 3:15 AM

Have you added an entry in the  <system.data><DbProviderFactories> in your machine.config for your PostgreSQL provider?

If yes, have you then overriden the GetRegistrations method in your class which inherits from DatabaseData?  Can you post your code for that method?

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

Sep 20, 2010 at 5:56 AM

Hello,

the missing entry in the machine.config was the solution! I added the entry, and now everything runs fine ;-)

Thank you very much!

Steve

Sep 21, 2010 at 6:37 PM

Hi,

I am trying to use Logging with DB but somehow i am not able to find the resource to use it so can you please let me know how i can use it with DB Access?

Thanks,

Raj

Sep 22, 2010 at 3:05 AM

You just need to create the correct connection string for your MS Access database and use System.Data.OleDb as the Provider Name.  You'll then use this connection string as the value for the Database Instance property of your Database Trace Listener.

By the way, please post new topics in a separate discussion thread.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com