Building Microsoft Enterprise Applications from the Command Line

Background

Reviewing an Application that makes heavy use of Microsoft’s Enterprise Library.

Unfortunately, my installed copy of Microsoft’s Visual Studio is not working and everything has to be done through the command line.

Here is how I stumbled through.

Compilation

When we invoked csc, encountered the error pasted below.

Error


Daily.cs(11,17): error CS0234: The type or
namespace name 'Practices' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Daily.cs(18,17): error CS0246: The type or namespace name 'Database' could not be found (are you missing a using
directive or an assembly reference?)

Source Code

Here is what lines 11 and 18 look like.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.Common;
using System.Configuration;


using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Transactions;

namespace reporting
{
    public class reportManager : ReportManager<DailyPaymentTransactionData>
    {
        private Database db;


Explanation

The cited lines are:

  1. Using Microsoft.Practices.EnterpriseLibrary.Data;
  2. private Database db;

 

Download Enterprise Library

Download Microsoft’s Enterprise Library from https://msdn.microsoft.com/en-us/library/ff632023.aspx.

Install Enterprise Library

Here we installed Microsoft Enterprise Library unto our machine.

CustomSetup

I will urge you to keep in mind the target folder.

Code Revision

Compile Code

Here is the original & revised compiler invocation statements.

Original


C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /define:DEBUG /optimize /out:reporting.exe *.cs 

Revised


C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /define:DEBUG /optimize /out:reporting.exe *.cs /r:"Microsoft.Practices.EnterpriseLibrary.Data.dll" /r:"Microsoft.Practices.EnterpriseLibrary.Common.dll"

Explanation

  1. Added a reference ( using /r )  to Microsoft.Practices.EnterpriseLibrary.Data.dll
  2. Added reference to Microsoft.Practices.EnterpriseLibrary.Common.dll

Execution

Error

Missing Dlls

 

 

Error Missing
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified. Microsoft.Practices.EnterpriseLibrary.Data
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system
cannot find the file specified.
Microsoft.Practices.EnterpriseLibrary.Common
 Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.InnerCreateDatabase(String name)
 Microsoft.Practices.ServiceLocation
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Practices.Unity, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.CreateDefaultContainer
( IConfigurationSource configurationSource )
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.SetCurrentContainerIfNotSet()
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.InnerCreateDatabase(String name)
Microsoft.Practices.Unity
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Practices.Unity.Interception, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.
Unity.UnityContainerConfigurator..ctor(IUnityContainer container)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.CreateDefaultContainer(IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibra
ryContainer.SetCurrentContainerIfNotSet()
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.InnerCreateDatabase(String name)
‘Microsoft.Practices.Unity.Interception

 

Missing Application Configuration File

Here is one of the errors we saw:


Unhandled Exception: Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type Database, key "DEDB"

 ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "DEDB".

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,DEDB
 ---> System.InvalidOperationException: The type Database cannot be constructed.
 You must configure the container to supply this value.
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstruct
or)
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey)


Explanation

  1. In our case the Application configuration’s file was missing all together
  2. Yes, we had App.Config file
  3. In the IDE, msbuild uses the App.Config file to produce the bin\Debug\<app>.exe.config file

Sample Configuration File


<?xml version="1.0" encoding="utf-8" ?>
<configuration>

   <connectionStrings>
    
	<add name="DEDB" connectionString="Data Source=HRDB;Initial Catalog=HRDBDev;Persist Security Info=True;User ID=reporter;Password=123TRS" providerName="System.Data.SqlClient" />
    
   </connectionStrings>  
  
</configuration>


MSBUILD

Rather than using csc we should have used msbuild.

Pasted below are the screenshots from invoking msbuild; along with a brief description of what is being accomplished.

Build

Enterprise Library Files

Here we have the Enterprise Library files copied from our installed folder to the destination folder ( bin\debug):

msbuild-EnterpriseLibrary-CopyingFile

App.config File

Here the app.config file copied to bin\debug\[application].exe.config

appConfigFile

EXE and PDB

Here the executable and the PDB, a program database file which holds debugging and project state information and allows for incremental linking of a Debug configuration of our program, are copied to the bin\debug folder.

ExeAndPDB

 

BIN FOLDER

Here is what our bin\debug folder look like

bin-debug-folder

Summary

Really should have issued msbuild as it will have made use of the existing msproj file.

Issuing msbuild would have copied the Enterprise Library Dlls and mirrored the app.config as [app].exe.config file.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s