Workflow compile error when using DAAB

Topics: Data Access Application Block
Jul 1, 2008 at 4:29 PM
Hi,

I have a problem when using DAAB in a simple workflow program. When I try to create a database object in the constructor of a workflow activity, the compiler shows the following message:

Error    1    Could not create activity of type 'WorkflowConsoleApplication1.Workflow1'. System.Configuration.ConfigurationErrorsException: The requested database Connection String is not defined in configuration.
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, ILifetimeContainer lifetimeContainer, String id, IConfigurationSource configurationSource)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](String id, IConfigurationSource configurationSource)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.NameTypeFactoryBase`1.Create(String name)
   at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(String name)
   at WorkflowConsoleApplication1.Workflow1..ctor()    I:\WorkflowConsoleApplication1\WorkflowConsoleApplication1    1    1   

It seems that the Visual Studio tries to create/run the workflow activity at compile time but the connection string in app.config is not available at that time. This problem only exists in workflow project type. If I use a property to encapsulate the database object and create it just before I use it, the program works normally.

I have tried in VS2005 + EntLib 3.1 and VS2008 + EntLib 4.0. Both give me similar results. Anyone can help? Thank you in advance.

Workflow1.cs
<!-- code formatted by http://manoli.net/csharpformat/ -->
   1:  using System;
   2:  using System.ComponentModel;
   3:  using System.ComponentModel.Design;
   4:  using System.Collections;
   5:  using System.Drawing;
   6:  using System.Linq;
   7:  using System.Workflow.ComponentModel.Compiler;
   8:  using System.Workflow.ComponentModel.Serialization;
   9:  using System.Workflow.ComponentModel;
  10:  using System.Workflow.ComponentModel.Design;
  11:  using System.Workflow.Runtime;
  12:  using System.Workflow.Activities;
  13:  using System.Workflow.Activities.Rules;
  14:  using System.Data;
  15:  using System.Data.Common;
  16:  using Microsoft.Practices.EnterpriseLibrary.Data;
  17:   
  18:  namespace WorkflowConsoleApplication1
  19:  {
  20:      public sealed partial class Workflow1: SequentialWorkflowActivity
  21:      {
  22:          private Database _db;
  23:   
  24:         
  25:   
  26:          public Workflow1()
  27:          {
  28:              InitializeComponent();
  29:   
  30:              //this line fail to compile
  31:              this._db = DatabaseFactory.CreateDatabase("Connection String");
  32:              
  33:          }
  34:   
  35:          //use property can work
  36:          public Database Db
  37:          {
  38:              get
  39:              {
  40:                  if (_db == null)
  41:                  {
  42:                      this._db = DatabaseFactory.CreateDatabase("Connection String");
  43:                  }
  44:                  return _db;
  45:              }
  46:   
  47:          }
  48:   
  49:          private void codeActivity1_ExecuteCode(object sender, EventArgs e)
  50:          {
  51:              //Console.WriteLine("Hello world");
  52:              DbCommand cmd = this.Db.GetSqlStringCommand("select top 10 * from Person.Contact");
  53:              using (IDataReader dr = this.Db.ExecuteReader(cmd))
  54:              {
  55:                  while (dr.Read())
  56:                  {
  57:                      Console.WriteLine(dr["EmailAddress"].ToString());
  58:                  }
  59:              }
  60:          }
  61:      }
  62:   
  63:  }
app.config
<!-- code formatted by http://manoli.net/csharpformat/ -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <connectionStrings>
    <add name="Connection String" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>


chauhm
Jul 1, 2008 at 6:46 PM
Hi,

You seem to have a solution already, which is to avoid initializing workflow state in its constructor. What's wrong with it?

Fernando
Jul 2, 2008 at 1:35 PM


fsimonazzi wrote:
Hi,

You seem to have a solution already, which is to avoid initializing workflow state in its constructor. What's wrong with it?

Fernando

Hi,

I have googled the reason of creating/running workflow activity at compile time, but I couldn't find any explaination about it. This is my first question. 

Actually I am facing another problem in my production project which is under VS2005 + EntLib 3.1. A DbCommand object created by DAAB is always timeout in my workflow activity. Again, the same DbCommand object works normally in normal class library project type. I don't know are the two problems related or not.

Thanks for reply.

chauhm


Jul 2, 2008 at 2:49 PM
Hi,

Try the following experiment: create a new sequential workflow app, open the workflow with the code editor, add Debugger.Break(); after the call to InitializeComponent(), save (important) and build; this crashes my VS 2008. I expected to be able to determine which build step involved instantiating the workflow, just out of curiosity, but was unable. In any case, I don't think you should manage your workflow's state in its constructor, but I don't know enough about WWF to know what's the best spot to manage it; there are some initialization methods and events that you might be able to use.

Regarding the timeout, I suggest you try with a plain ADO.NET query to see if it makes a difference.

Fernando



chauhm wrote:


fsimonazzi wrote:
Hi,

You seem to have a solution already, which is to avoid initializing workflow state in its constructor. What's wrong with it?

Fernando

Hi,

I have googled the reason of creating/running workflow activity at compile time, but I couldn't find any explaination about it. This is my first question. 

Actually I am facing another problem in my production project which is under VS2005 + EntLib 3.1. A DbCommand object created by DAAB is always timeout in my workflow activity. Again, the same DbCommand object works normally in normal class library project type. I don't know are the two problems related or not.

Thanks for reply.

chauhm