Problem using resource app block

Topics: User Discussion
Feb 21, 2010 at 2:12 PM
Edited Feb 21, 2010 at 2:13 PM
Hi all,
I am having problem using resource app block. I created a database named "MyDataBase" and I added a data resource provider to my project. All configurations looks fine but when I try to create instance of my manager :
 

IResourceManager rm = ResourceFactory.GetResourceManager("Content Resourse Manager");

I get the following error:

{"The current build operation (build key Build Key[EntLibContrib.Resource.IResourceManager, Content Resourse Manager]) failed: data source=........\\sqlexpress;initial catalog=MyDataBase;persist security info=true;\r\nContentResource\r\n (Strategy type ConfiguredObjectStrategy, index 2)"}

Pleaseeeeee help me.
Thanks.
Coordinator
Feb 22, 2010 at 6:08 PM

Hi Payamgerai,

Could you send me your config please? And while you are there can you clarify that your instance name is "Content Resourse Manager" and not "Content Resource Manager". The probem appears to be in getting at your configuration data.

Thanks...   Steve

Feb 22, 2010 at 6:54 PM
Edited Feb 22, 2010 at 6:58 PM

Hi Steve,

Thanks for your reply.

Here is part of my config file that I think is relevant to this issue:

<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="resourceConfiguration" type="EntLibContrib.Resource.Configuration.ResourceSettings, EntLibContrib.Resource, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null" />
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <dataConfiguration defaultDatabase="ApplicationServices" />
  <resourceConfiguration defaultResourceManager="Sitemap Resource Manager">
    <resourceManagers>
      <add resourceProviderName="Content Resource Provider" type="EntLibContrib.Resource.ResourceManager, EntLibContrib.Resource, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Content Resource Manager" />
      <add resourceProviderName="Lable Resource Provider" type="EntLibContrib.Resource.ResourceManager, EntLibContrib.Resource, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Lable Resource Manager" />
      <add resourceProviderName="Message Resource Provider" type="EntLibContrib.Resource.ResourceManager, EntLibContrib.Resource, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Message Resource Manager" />
      <add resourceProviderName="Sitemap Resource Provider" type="EntLibContrib.Resource.ResourceManager, EntLibContrib.Resource, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Sitemap Resource Manager" />
    </resourceManagers>
    <resourceProviders>
      <add databaseInstanceName="ApplicationServices" resourceBaseName="ContentResource"
        type="EntLibContrib.Resource.Providers.DataResourceProvider, EntLibContrib.Resource.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Content Resource Provider" />
      <add resourcePathName="C:\Users\Payam\Documents\Visual Studio 2008\Projects\SiamakAghaei\SiamakAghaei\App_GlobalResources"
        resourceBaseName="Lable" type="EntLibContrib.Resource.Providers.XmlResourceProvider, EntLibContrib.Resource, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Lable Resource Provider" />
      <add resourcePathName="C:\Users\Payam\Documents\Visual Studio 2008\Projects\SiamakAghaei\SiamakAghaei\App_GlobalResources"
        resourceBaseName="Message" type="EntLibContrib.Resource.Providers.XmlResourceProvider, EntLibContrib.Resource, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Message Resource Provider" />
      <add resourcePathName="C:\Users\Payam\Documents\Visual Studio 2008\Projects\SiamakAghaei\SiamakAghaei\App_GlobalResources"
        resourceBaseName="SiteMap" type="EntLibContrib.Resource.Providers.XmlResourceProvider, EntLibContrib.Resource, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Sitemap Resource Provider" />
    </resourceProviders>
  </resourceConfiguration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="Data Source=DPAYAM\SQLEXPRESS;Initial Catalog=SiamakAghaei;User ID=SiamakAghaeiUser;Password=*************;Persist Security Info=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

and here is instantiation:

IResourceManager rm = ResourceFactory.GetResourceManager("Content Resource Manager");

 

Thanks for your help.

Best Regards

Payam

Coordinator
Feb 22, 2010 at 8:01 PM

Hi Payam,

I notice now that the spelling of "Resource" in the instance name has been corrected.  Did this fix the issue or is it still outstanding? Otherwise the config is fine.  If you are still getting the error then I will try to recreate the problem here.

Cheers...   Steve

Feb 22, 2010 at 8:08 PM

No it did not, I still get the same error:

{"The current build operation (build key Build Key[EntLibContrib.Resource.IResourceManager, Content Resource Manager]) failed: data source=dpayam\\sqlexpress;initial catalog=siamakaghaei;persist security info=true;\r\nContentResource\r\n (Strategy type ConfiguredObjectStrategy, index 2)"}

Feb 23, 2010 at 8:08 PM

The error you are receiving is from inside the DataResourceManager

I had the same exact problem and tracked it down and made the following changes below.  It is essentially saying there are no records in the database.  For me it is a chicken and the egg problem.  I don't have anything in the database so the ResourceSet is zero but instead of returning an empty ResourceSet it is throwing an error.  I want the error to come from the fact that I am missing an entry instead of not having any entries at all.

protected override ResourceSet InternalGetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents)
		{
			DataResourceSet resourceSet = null;

			// check the resource set cache first
			if (ResourceSets.Contains(culture.Name))
				resourceSet = (DataResourceSet)ResourceSets[culture.Name];
			else
			{
				// create a new resource set
				resourceSet = new DataResourceSet(database, baseName, culture);
				// check the number of resources returned
				if (resourceSet.Count == 0)
				{
					// try the parent culture if not already at the invariant culture
					if (tryParents)
					{
                        if (!culture.Equals(CultureInfo.InvariantCulture)) {
                            //throw new MissingManifestResourceException(database.ConnectionStringWithoutCredentials + Environment.NewLine + this.baseName + Environment.NewLine + culture.Name);
                        
                            // do a recursive call on this method with the parent culture
                            resourceSet = this.InternalGetResourceSet(culture.Parent, createIfNotExists, tryParents) as DataResourceSet;
                        }
					}
				}
				else
				{
					// only cache the resource if the createIfNotExists flag is set
					if (createIfNotExists)
						ResourceSets.Add(culture.Name, resourceSet);
				}
			}
			return resourceSet;
		}

 

protected override ResourceSet InternalGetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents)
        {
            DataResourceSet resourceSet = null;

            // check the resource set cache first
            if (ResourceSets.Contains(culture.Name))
                resourceSet = (DataResourceSet)ResourceSets[culture.Name];
            else
            {
                // create a new resource set
                resourceSet = new DataResourceSet(database, baseName, culture);
                // check the number of resources returned
                if (resourceSet.Count == 0)
                {
                    // try the parent culture if not already at the invariant culture
                    if (tryParents)
                    {
                        if (!culture.Equals(CultureInfo.InvariantCulture)) {
                            //throw new MissingManifestResourceException(database.ConnectionStringWithoutCredentials + Environment.NewLine + this.baseName + Environment.NewLine + culture.Name);
                        
                            // do a recursive call on this method with the parent culture
                            resourceSet = this.InternalGetResourceSet(culture.Parent, createIfNotExists, tryParents) as DataResourceSet;
                        }
                    }
                }
                else
                {
                    // only cache the resource if the createIfNotExists flag is set
                    if (createIfNotExists)
                        ResourceSets.Add(culture.Name, resourceSet);
                }
            }
            return resou
protected override ResourceSet InternalGetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents)
		{
			DataResourceSet resourceSet = null;

			// check the resource set cache first
			if (ResourceSets.Contains(culture.Name))
				resourceSet = (DataResourceSet)ResourceSets[culture.Name];
			else
			{
				// create a new resource set
				resourceSet = new DataResourceSet(database, baseName, culture);
				// check the number of resources returned
				if (resourceSet.Count == 0)
				{
					// try the parent culture if not already at the invariant culture
					if (tryParents)
					{
                        if (!culture.Equals(CultureInfo.InvariantCulture)) {
                            //throw new MissingManifestResourceException(database.ConnectionStringWithoutCredentials + Environment.NewLine + this.baseName + Environment.NewLine + culture.Name);
                        
                            // do a recursive call on this method with the parent culture
                            resourceSet = this.InternalGetResourceSet(culture.Parent, createIfNotExists, tryParents) as DataResourceSet;
                        }
					}
				}
				else
				{
					// only cache the resource if the createIfNotExists flag is set
					if (createIfNotExists)
						ResourceSets.Add(culture.Name, resourceSet);
				}
			}
			return resourceSet;
		}
rceSet;
        }
Feb 23, 2010 at 8:11 PM

oops my bad didnt see those were XmlResourceProviders.  I imagine it is the same exact problem though.

 

Feb 24, 2010 at 2:42 AM

Hi cberthold

Thank you so much, I just replaced the code and problem solved! :)
Coordinator
Feb 24, 2010 at 8:54 AM
Edited Feb 24, 2010 at 8:55 AM

Thank you very much for your help cberthold.  It is wonderful to see the user community working in this way and helping each other.  This is exactly why we have Open Source projects. 

Well done and many thanks again.  I will now put the changes into the main source code and yes, this problem will be the same for all of the providers, so I will make the changes across the board.

Great stuff guys...   Steve

Coordinator
Feb 24, 2010 at 9:22 AM
Edited Feb 24, 2010 at 9:23 AM

Ok, that fix is now in the main source code (but not the release yet).  The two files fixed were DataResourceManager and FileResourceManager.  To clarify the fix, the Resource Application Block will no longer throw a MissingManifestResourceException when it finds no resource records. 

This allows you to build your application with no resource records and to then enter them in as you go. Of course RAB will still throw an exception if you request a resource entry that does not exist.

Cheers...   Steve

Feb 24, 2010 at 11:35 AM

Thank you so much guys,

It's great to be the part of this community.

Best regards

Payam

Feb 24, 2010 at 11:17 PM

Steve,

I don't know if I should continue it here or elsewhere but I'm wondering if there was a better way to do the exceptions for when a resource entry doesn't exist.  Right now it throws an out of bounds exception that is none intuitive.  I'm guessing it is because the internal structure is using a hash table and the hash table is throwing the error.  I'm not sure if it would be worth hooking the get to check if it exists and if not throw a different exception which could be got and handled in a different way.  Right now I'm handling it with a Facade which extends the IResourceManager and has additional Get parameters on it to allow me to set defaults at run time (I'm a bit of a cheater at the expense of some a reasonable amount of performance).  For example I have a public string GetString(string key, string text)  which checks to see if the field exists and if it doesn't goes ahead and casts the GetResourceSet() as CommonResourceSet and then CreateDefaultWriter and pass the default.  What is nice about that is you can put a #if DEBUG and have your program build all of the keys that you need to start with and then disable it for performance reasons in the #else.  The only downside of course is it works well for the strings but not all the other object types.  Maybe I just answered my own question, but food for thought.

 

 

Chris