Tell don't ask

October 19, 2009

Fluent NHibernate Gotcha

Filed under: Fluent NHibernate,Gotchas,NHibernate,S#arp Architecture — telldontask @ 2:04 pm

It seems today’s the day for me to make daft mistakes and then blog about them.

If you’re using Fluent NHibernate (and there are many reasons why you should ;-) ) and see this error message…

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
  * Database was not configured through Database method.

System.IndexOutOfRangeException: Index was outside the bounds of the array.

at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices)
at System.Array.GetValue(Int32 index)
at NHibernate.Type.AbstractEnumType..ctor(SqlType sqlType, Type enumType)
at NHibernate.Type.EnumStringType..ctor(Type enumClass)
at FluentNHibernate.Mapping.GenericEnumMapper`1..ctor()

Do yourself a favour and check you haven’t left any empty Enums in your domain layer (the one that Fluent NHibernate is using to map to your database).

Funnily enough, if you do have an empty Enum, I don’t know, off the top of my head maybe something like this…

  1. public enum NoteType
  2. {
  3.     
  4. }

Then you’ll probably get the error above.

July 4, 2008

Shocking Gotcha – ASP.NET user controls not being referenced

Filed under: ASP.Net,Gotchas,User Controls — telldontask @ 9:04 am

Sometimes I just want to quit and take up kite flying or something…

I was extending a feature for a site that used a user control called that had a class name of controls_searchboxui embedded in an aspx page.

The new functionality meant that further user controls were to be created and only one of them would be programitcally added to the page based on the value of a querystring.

So I added a user control called advancedsearchui to a folder on my site called "controls" and it assumed the object name controls_advancedsearchui.

I then removed the embedded user control from my search.aspx page and added a place holder called searchPlaceHolder in its place.

The page would look at the value of the querystring "searchtype" on the url and run the following code to add the required control to the placeholder:

  1. string UsageMode = (string) Request.QueryString["searchtype"];
  2.             
  3.             switch (UsageMode)
  4.             {
  5.                 case "adv":
  6.                     controls_advancedsearchui AdvancedSearchBox = (controls_advancedsearchui) LoadControl("~/controls/advancedsearchui.ascx");
  7.                     searchPlaceHolder.Controls.Add(AdvancedSearchBox);
  8.                     AdvancedSearchBox.Search +=new AdvancedSearchRaised(doAdvancedSearch);
  9.                     break;
  10.                 default:
  11.                     controls_searchboxui SimpleSearchBox = (controls_searchboxui) LoadControl("~/controls/searchboxui.ascx");
  12.                     searchPlaceHolder.Controls.Add(SimpleSearchBox);
  13.                     SimpleSearchBox.Search += new SimpleSearchRaised(doSimpleSearch);
  14.                     break;
  15.             }

When i tried to build the solution, it failed and I got the following message :

The type or namespace name ‘controls_clientsearchui’ could not be found (are you missing a using directive or an assembly reference?)

say it with me…. WTF?

I invoked intelli(non)sense and sure enough, while controls_searchboxui was present, my new user control wasn’t.

The only difference between the two was that the controls_searchboxui object had been embedded on a page before now….

I dragged the control onto the page and immediately deleted it.

Hey Presto : the user control could now be referenced.

If anyone can explain this bizarre behaviour, please let me know… otherwise, I hope this helps and saves you some time.

January 15, 2008

Nmock Gotcha about Reference Parameters

Filed under: Gotchas,NMock — telldontask @ 11:04 am


I wrote a method in a view that cause me no end of grief when testing its presenter the other day.

I’ll elaborate on this a little more when I get the time but for now, here are the bones of the issue:

  1. public bool TrySetSummaryContent(string content, out Exception raisedError)
  2.         {
  3.             raisedError = null;
  4.             try
  5.             {
  6.                 webBrowser.DocumentText = content;
  7.             }
  8.             catch (Exception ex)
  9.             {
  10.                 raisedError = ex;
  11.                 return false;
  12.             }
  13.             return true;
  14.         }

This method is called by the presenter method below:

  1. bool ContentSet(string _summaryContent)
  2. {
  3.     // This loop is used to try setting the content of the view 3 times before erroring out.
  4.     int SetContentAttempt = 0;
  5.     Exception exRaised;
  6.     do
  7.     {
  8.         if (View.TrySetSummaryContent(_summaryContent, out exRaised))
  9.         {
  10.             return true;
  11.         }
  12.         SetContentAttempt++;
  13.     } while (SetContentAttempt<> LogError(exRaised, "The retention reason summary could not be displayed", "Summary Unavailable","Web Browser control raised an exception.");
  14.     return false;
  15. }

To write a unit test for the Content Set Method, I mocked out the view object and had to stipulate not only that the method would return a true or false value but I also had to state that the raisedError exception object in the TrySetSummaryContent method will be instantiated as so:

  1. string op = "Test Content";
  2.             Expect.Once
  3.             .On(_mockView)
  4.             .Method("TrySetSummaryContent")
  5.             .With(Is.EqualTo(op), Is.Out)
  6.             .Will(new SetNamedParameterAction("raisedError", new Exception()), Return.Value(true));

January 2, 2008

ASP.NET process identity does not have read permissions to the global assembly cache

Filed under: Gotchas,IIS — telldontask @ 1:43 pm

I just started playing around with WCF and stumbled upon an error while deploying a WCF host application to an IIS 5.1 instance running on XP-Pro.

My WCF app is held in a folder which is shared as a virtual folder in IIS on the target machine. It took a lot of googling to find a resolution and I had to take bits of information from a number of sources so for my sanity, I thought I’d blog it here for my future reference.

I got a server application unavailable error via the browser when trying to brows to a .svc file and the Event Viewer made three entries about the issue.

The first entry was a warning:

EventId : 1073 Failed to initialize the AppDomain:/LM/W3SVC/1/Root/ProductService Exception: System.IO.FileLoadException

Message: Could not load file or assembly ‘System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. Access is denied.

StackTrace: at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark) at System.Activator.CreateInstance(String assemblyName, String typeName) at System.AppDomain.CreateInstance(String assemblyName, String typeName) at System.AppDomain.CreateInstance(String assemblyName, String typeName) at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters) at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironmentAndReportErrors(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)

The second entry was an error

Failed to execute the request because the ASP.NET process identity does not have read permissions to the global assembly cache. Error: 0×80070005 Access is denied.

The third entry was an error

aspnet_wp.exe (PID: 3120) stopped unexpectedly.

To get around this I had to grant Read And Execute, Read & Write permissions to the application folder and propagate the permissions down to a child files and folders.

To access file and folder security in Windows XP, right click a folder and select properties.

If a security tab is not visible you must go into file explorer, Select Tools > Folder Options > View.

Deselect the check box for "Use simple file sharing". Select "Ok" and select properties on the app folder again.

This time, the Security tab will be visible and you can now select it and Add the ASPNET user to folder and grant them the rights listed earlier.

Select Advanced and ensure that "Inherit from the parent…." option is checked and hit "Ok".

Browsing to my WCF app now works fine and I can get back to work at last….. I hope this is useful.

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.