Friday, March 30, 2012

Detouring methods on base type and implementing type with Moles

I have been using the Moles framework recently in order to stub out dependencies on a third party library where the classes to be stubbed had some or all of the following characteristics:

  • No public constructor.
  • Methods to be stubbed are not virtual.
  • Methods to be stubbed are static.

The Moles framework is capable of handling all of the above and I won’t reprise the documentation here.

However, I did hit a quirk with this which is that when you generate the Moled type and set the detours for its methods, you can only detour the methods on that type and not the base type. Consider the following two classes.

public abstract class A
{
  protected string _type;
  /*
  This will be detoured in test
  */
  public string Type 
  {
  get 
  {
  return _type;
  }
  }
}
/*
Class to be moled. We want to detour both Type and Level
*/
public class B:A
{
  int _level =  1;
  internal B()
  {
    _type = "B";
  }
  
  public int Level
  {
    get
    {
      return _level;
    }
  }
}


The following code shows how to detour properties on both A and B:



public MA Build()
{
  //We want to return an instance of the sub type as the base type, detouring methods
  //on both the subtype and the base type (A)
  var moledB = new MB();
  //Detour method on subtype
  moledB.LevelGet = () => _level;
  //Create using instance of Moled Subtype
  var moledA = new MA(moledB);
  //Detour methods on base type
  moledA.TypeGet = () => _type;
  //return Moled basetype. As this was created using the constructor that took  an instance, we will
  //be able to cast this to A
  return moledA;
}

Friday, February 24, 2012

Friday, July 17, 2009

ORA-29701 when calling System.Data.OracleClient.OracleLob.Write

I recently experienced some unhandled errors when attempting to install a custom module in a DotNetNuke site that had an Oracle back end.  The DB provider I was using was AcuitiDB.

After some debugging, I was able to establish that the error was being raised from a call to AddDesktopModule, and the StackTrace showed that it was originating in a call to System.Data.OracleClient.OracleLob.Write:

[OracleException (0x80131938): ORA-29701: unable to connect to Cluster Manager

]

   System.Data.OracleClient.OracleConnection.CheckError(OciErrorHandle errorHandle, Int32 rc) +304889

   System.Data.OracleClient.OracleLob.Write(Byte[] buffer, Int32 offset, Int32 count) +587

   DotNetNuke.Data.OracleSqlHelper.CreateClobParameter(String ConnectionString, String ParameterName, String p) +229

I contacted Sanjay Mehrota at Acuiti and he was very helpful in assisting me to track down the cause of this.  Firstly, he suggested that I try some other areas of the site that created clobs, such as the text/html module.  This exhibited the same error.  He also provided some test code for creating a temporary clob which I added to a simple console app as a test harness.  This initially appeared to work correctly, but when I increased the size of the string being used to around 29,000 bytes, I got the same error.  It transpires that Temporary clobs are created in the temporary tablespace and that owing to some other testing work that was being performed on the DB, we had run out of tablespace.  I got the DBA to increase the size of the temporary tablespace and the problem disappeared.

Tuesday, May 19, 2009

Asp.Net Bundle from TypeMock

Unit Testing ASP.NET? ASP.NET unit testing has never been this easy.
Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers.
The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price.
Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit.
The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you'll get a license automatically (even if more than 60 submit) during the first week of this announcement.
Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends.
Go ahead, click the following link for more information on how to get your free license.

Thursday, August 07, 2008

Writing to Event log with ASP.Net and Enterprise Library

I recently had a problem writing to the event log from my application.  I had configured the application to use the Logging Application Block from the 3.1 version of the Microsoft Enterprise Library and followed the walk-through in the documentation to log exceptions to the Application Log, but nothing was appearing.

I had amended the code so that it always through an exception on clicking a test button, and stepping through the code, I could see that Log.Write was being called and no error was raised from this call.

A quick search on Google suggested that this was a permissions issue for the ASP.Net account, so I added the following key:

HKLM\System\CurrentControlSet\Services\EventLog\Application\<my app name>

I granted full control to the ASPNET account to this key and amended the source property of my trace listener to <my app name> using the Enterprise Library Configuration Manager.

I now get events recorded in the Application log and their source is set to <my app name> so it is easy to filter them for the events I want.

Tuesday, March 04, 2008

Code Access Security For Child AppDomain

I recently configured an existing application to use ClickOnce deployment. All seemed to go well until I tried to run the application, whereupon it threw an exception on a line that had previously been working perfectly:

AppDomain childDomain;

childDomain = AppDomain.CreateDomain(ChildAppDomainName);

//Following line throws exception

childDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);

The exception was:

{"Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."}

The application was configured to run as FullTrust. To cut a long and frustrating story short, It turned out that the security settings were not being propagated to the child AppDomain. I changed the code to the following:

AppDomain childDomain;

Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;

Evidence childEvidence = new Evidence(baseEvidence);

childDomain = AppDomain.CreateDomain(ChildAppDomainName, childEvidence, AppDomain.CurrentDomain.SetupInformation);

This allowed the child AppDomain to inherit the settings from the parent domain and everything worked as expected once more.