Faqs on asp.net,c#,vb.net and sqlserver2005

this blog covers all the Faqs related to .net technologies like asp.net,vb.net,c#.net,ajax,javascript and sqlserver2005.

Feb 22, 2008

Enterprise Library and .Net 2.0

Using Enterprise DAAB from ASP.NET 2.0 project
Enterprise Library and .Net 2.0

Error parsing application configuration file at line 47. Type '{http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration}ReadOnlyConfigurationSectionData' is not found in Schema. Please correct before processeding. (You might rename the current web.config and add a new one.)

I tossed up a quick example of using the
Enterprise Library Configuration Application Block in your applications, and thought I would give an example of using the Cache Application Block as well. In this case, I am actually using the two application blocks together so we can seem them side-by-side. As always, the code will run in Snippet Compiler, but you need to download and install the Enterprise Library and move over the appropriate DLL's shown in the directory.

In this example, I am using the XML File Storage Provider to save my configuration settings in my sitesettings.config file and using the default storage provider for caching, called the NullBackingStore, which stores the cache information into a Hashtable in memory.

This time when I run the Enterprise Library Configuration tool I need to add both the Caching Application Block and Configuration Application Block to my application (shown below). I keep the default settings, but I do have to specify that sitesettings.config will be the XML file to store my global application settings. If you don't do this, the application will toss an error reminding you to do so.

At first I was a bit confused as to why there is an XML File Storage Provider and XML Serializer Transformer under cachingConfiguration. I didn't add it. It turns out that there is a separate XML file, called by default cachingConfiguration.config, created to store the caching configuration information. It specifies that I am using the NullBackingStore as well as a few other settings.



Here is the code that reads and writes to both the configuration file and cache for testing:



Testing Application Blocks
using System;

using Microsoft.Practices.EnterpriseLibrary.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Caching;



public class SiteSettings

{

#region Private Members



private string _name;

private string _url;

private string _emailAddress;



#endregion



#region Properties



public string Name

{

get { return _name; }

set { _name = value; }

}



public string Url

{

get { return _url; }

set { _url = value; }

}



public string EmailAddress

{

get { return _emailAddress; }

set { _emailAddress = value; }

}



#endregion

}



public class TestClass

{

public static void Main()

{

// Create initial "siteSettings" object

SiteSettings siteSettings = new SiteSettings();



siteSettings.Name
= "DavidHayden.com";

siteSettings.Url
= "http://www.davidhayden.com/";

siteSettings.EmailAddress
= "nospam@nospam.com";





// Save "siteSettings" to the XML config file

ConfigurationManager.WriteConfiguration("SiteSettings", siteSettings);





// Retrieve settings from the XML config file

SiteSettings mySettings = ConfigurationManager.GetConfiguration("SiteSettings") as SiteSettings;



if (mySettings != null)

{

Console.WriteLine(
"Name = {0}", mySettings.Name);

Console.WriteLine(
"Url = {0}", mySettings.Url);

Console.WriteLine(
"Email Address = {0}", mySettings.EmailAddress);

Console.ReadLine();

}





// Get the CacheManager

CacheManager myCacheManager = CacheFactory.GetCacheManager();





// Save the settings to the Cache

myCacheManager.Add("SiteSettings",mySettings);





// Retrieve settings from the cache

SiteSettings cachedSettings = (SiteSettings)myCacheManager.GetData("SiteSettings");



if (cachedSettings != null)

{

Console.WriteLine(
"Name = {0}", cachedSettings.Name);

Console.WriteLine(
"Url = {0}", cachedSettings.Url);

Console.WriteLine(
"Email Address = {0}", cachedSettings.EmailAddress);

Console.ReadLine();

}

}

}




Although the configuration settings might be a bit ugly in the backend, the code is pretty straight forward. I have added comments above so you can see what is happening. Here are the configuration files:

cachetest.exe.config
<configuration>

<configSections>

<section name="enterpriselibrary.configurationSettings"

type
="Microsoft.Practices.EnterpriseLibrary.Configuration.

ConfigurationManagerSectionHandler,

Microsoft.Practices.EnterpriseLibrary.Configuration,

Version
=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

</configSections>

<enterpriselibrary.configurationSettings

xmlns:xsd
="http://www.w3.org/2001/XMLSchema"

xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"

applicationName
="EntLibTest"

xmlns
="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration">

<configurationSections>

<configurationSection xsi:type="ReadOnlyConfigurationSectionData"

name
="cachingConfiguration" encrypt="false">

<storageProvider xsi:type="XmlFileStorageProviderData"

name
="XML File Storage Provider" path="cachingConfiguration.config" />

<dataTransformer xsi:type="XmlSerializerTransformerData"

name
="Xml Serializer Transformer">

<includeTypes />

</dataTransformer>

</configurationSection>

<configurationSection name="SiteSettings" encrypt="false">

<storageProvider xsi:type="XmlFileStorageProviderData"

name
="XML File Storage Provider" path="sitesettings.config" />

<dataTransformer xsi:type="XmlSerializerTransformerData"

name
="Xml Serializer Transformer">

<includeTypes />

</dataTransformer>

</configurationSection>

</configurationSections>

<keyAlgorithmStorageProvider xsi:nil="true" />

<includeTypes />

</enterpriselibrary.configurationSettings>

</configuration>




cacheConfiguration.config
<?xml version="1.0" encoding="utf-8"?>

<cachingConfiguration>

<xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings,

Microsoft.Practices.EnterpriseLibrary.Caching, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">

<enterpriseLibrary.cacheSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" defaultCacheManager="Cache Manager"

xmlns
="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/cache">

<cacheManagers>

<cacheManager name="Cache Manager" expirationPollFrequencyInSeconds="60"

maximumElementsInCacheBeforeScavenging
="1000" numberToRemoveWhenScavenging="10">

<cacheStorage xsi:type="CustomCacheStorageData" name="Null Storage"

type
="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore,

Microsoft.Practices.EnterpriseLibrary.Caching, Version=1.0.0.0, Culture=neutral,

PublicKeyToken
=null">

<extensions />

</cacheStorage>

</cacheManager>

</cacheManagers>

</enterpriseLibrary.cacheSettings>

</xmlSerializerSection>

</cachingConfiguration>


sitesettings.config
<?xml version="1.0" encoding="utf-8"?>

<SiteSettings>

<xmlSerializerSection type="SiteSettings, cachetest, Version=0.0.0.0,

Culture=neutral, PublicKeyToken=null">

<SiteSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance">

<Name>DavidHayden.com</Name>

<Url>http://www.davidhayden.com/</Url>

<EmailAddress>nospam@nospam.com</EmailAddress>

</SiteSettings>

</xmlSerializerSection>

</SiteSettings>




As you can see, there is certainly a lot of flexiblity with the Enterprise Library as literally you could change the configuration in the backend to specify different storage providers and theoretically never change a single line of code. There has got to be some price to this flexiblity, which is a couple of XML files to specify settings. It looks a bit daunting at first if you aren't used to XML and the plug-in type architecture, but by playing with it more and more one can get comfortable with it pretty quickly.

0 Comments:

Post a Comment

<< Home