TypeLoadException using custom web.config section

108 views
Skip to first unread message

bgs264

unread,
May 5, 2009, 7:12:15 AM5/5/09
to NUnit-Discuss
Dear all,

Re: NUnit version 2.4.8
CLR version: 2.0.50727.832

I am writing some unit tests for an ASP.NET Web Site written in Visual
Studio 2005, .NET 2.0.

My web site stores some values in the web.config file like this:

<configuration>
<configSections>
<section name="sapConfig"
type="SCC.WebInvoicing.Utils.SAPConfigManager"/>
</configSections>

.... web.config file snipped for brevity ...

<sapConfig>
<sapVatCodes>
<add code="a" description="Standard rated VAT: 17.5%"
ratePercent="17.5"/>
<add code="b" description="Standard rated VAT: 15%"
ratePercent="15" default="true"/>
<add code="c" description="Reduced rated VAT: 5%" ratePercent="5"/>
<add code="d" description="Zero rated VAT" ratePercent="0"/>
</sapVatCodes>
<sapUnitsOfMeasure>
<add name="XXX" description="First description"/>
<add name="YYY" description="Second description"/>
</sapUnitsOfMeasure>
</sapConfig>
</configuration>

I have a class called "SAPConfigManager" which derives from
ConfigurationSection; which exists in the SCC.WebInvoicing.Utils
namespace in the web site.

The web site runs perfectly fine... and works as expected.

For unit testing, I have written some test classes within another
namespace in the same project. I have then used the Web Deployment
Projects facility to deploy the project to a local folder on the c:\
drive and I'm pointing nunit to the merged DLL created by web
deployment project.

I have copied web.config from the root into the /bin folder and used
the "Project Editor" in nunit to point the "Configration file name" to
"web.config".

When I try to run some simple tests, the error message that I am
receiving is:

SCC.WebInvoicing.UnitTests.PurchaseOrderTest.ConstructUrbanVisionPO:
System.Configuration.ConfigurationErrorsException : An error occurred
creating the configuration section handler for sapConfig: Could not
load type 'SCC.WebInvoicing.Utils.SAPConfigManager' from assembly
'System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'. (C:\temp\WebInvoicingDeploy\bin
\Web.config line 12)
----> System.TypeLoadException : Could not load type
'SCC.WebInvoicing.Utils.SAPConfigManager' from assembly
'System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.

Stack trace:
at
System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord
(String configKey, Boolean& isRootDeclaredHere)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive
(String configKey, Boolean getLkg, Boolean checkPermission, Boolean
getRuntimeObject, Boolean requestIsHere, Object& result, Object&
resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String
configKey, Boolean getLkg, Boolean checkPermission)
at System.Configuration.BaseConfigurationRecord.GetSection(String
configKey)
at
System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection
(String sectionName)
at System.Configuration.ConfigurationManager.GetSection(String
sectionName)
at SCC.WebInvoicing.Utils.SAPConfigManager.get_SapConfig() in C:
\Visual Studio Working Copy\WebInvoicing\App_Code\ConfigurationHandlers
\SAPConfigManager.vb:line 47
at SCC.WebInvoicing.Utils.SAPConfigManager.UOM_Get(String uom) in C:
\Visual Studio Working Copy\WebInvoicing\App_Code\ConfigurationHandlers
\SAPConfigManager.vb:line 60
at SCC.WebInvoicing.SapProxy.PurchaseOrder..ctor(String PONumber) in C:
\Visual Studio Working Copy\WebInvoicing\App_Code
\PurchaseOrder.vb:line 307
at SCC.WebInvoicing.UnitTests.PurchaseOrderTest.ConstructUrbanVisionPO
() in C:\Visual Studio Working Copy\WebInvoicing\App_Code\UnitTests
\PurchaseOrderTest.vb:line 17
--TypeLoadException
at System.Configuration.TypeUtil.GetTypeWithReflectionPermission
(IInternalConfigHost host, String typeString, Boolean throwOnError)
at
System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init
(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
at
System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions
(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
at
System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory..ctor
(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
at System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory
(FactoryRecord factoryRecord)
at
System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord
(String configKey, Boolean& isRootDeclaredHere)


I am uncertain why it is trying to load type
SCC.WebInvicing.Utils.SAPConfigurationManager from namespace
System.Configuration in the first place.

Any thoughts on how I can correct?

Thanks in advance
Ben.

Charlie Poole

unread,
May 5, 2009, 11:28:40 AM5/5/09
to nunit-...@googlegroups.com
When playing around with configs, it's useful to have a test
that verifies the config is being used. You can do this with
a simple AppSetting set to some known value.

The config you specify must be in the AppBase directory.

Charlie
Message has been deleted

bgs264

unread,
May 6, 2009, 4:51:45 AM5/6/09
to NUnit-Discuss
Hi Charlie,
Thank you for taking the time to respond.

I can confirm that I have written a basic web.config test that gets a
value out of an AppSetting and uses Assert.AreEqual to validate the
string which is being retreived. The test was successful.

It is the following test which fails:

''' <summary>
''' This method tries to construct purchase order and ensures
the order lines are properly set.
''' </summary>
<Test()> _
Public Sub ConstructPO()
' Construct a PO based on a known valid number, this
should construct and have four order lines.
testingPO = New PurchaseOrder("1548660")

' Make sure that there are 4 order lines
Assert.AreEqual(4, testingPO.OrderLines.Count,
String.Format("There should have been 4 lines forming PO 1548660,
but the collection contains {0}.",
testingPO.OrderLines.Count.ToString))
End Sub

It fails with the error message in my initial posting. Part of the
constructor for the PurchaseOrder object gets a code from our
accounting system (e.g. BOX) and looks it up in the web.config file to
get a more verbose meaning, using the the "SapConfigManager" class as
explained above.

So any idea how I can fix the error?

Thanks,
Ben.
Reply all
Reply to author
Forward
0 new messages