473,739 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class Library Configuration Files

OK. A bit behind the times, I know; but we're just moving over to .NET 2.0.
How on earth do you manage configuration settings in a class library in .NET
2.0? In version 1.1, we used a handy class called AssemblySetting s that
someone (I forget his name) had written. When the class library was
finished, you deployed it to the GAC and put the configuration files in the
GAC with the class library assembly. This no longer works. In fact trying
to get configuration data from anywhere but the calling executable falls
over.

This can't be correct. Our n-Tier architecture uses class libraries to
access data from the database and needs to obtain a connection string from a
configuration file. It's no good putting the configuration in the calling
executable because the calling executable does not know anything about the
database, and anyway the whole point of a class library is that it can be
called by many other objects. We don't want the same configuration data
spread all over the place.
Surely there's a way to fix this. Does anyone know how?


Peter (spitting blood and calling Microsoft all sorts of things you don't
want to hear)
Mar 28 '07 #1
7 10322
On Mar 28, 3:03 am, "Peter Bradley" <pbrad...@uwic. ac.ukwrote:
OK. A bit behind the times, I know; but we're just moving over to .NET 2.0.
How on earth do you manage configuration settings in a class library in .NET
2.0? In version 1.1, we used a handy class called AssemblySetting s that
someone (I forget his name) had written. When the class library was
finished, you deployed it to the GAC and put the configuration files in the
GAC with the class library assembly. This no longer works. In fact trying
to get configuration data from anywhere but the calling executable falls
over.

This can't be correct. Our n-Tier architecture uses class libraries to
access data from the database and needs to obtain a connection string from a
configuration file. It's no good putting the configuration in the calling
executable because the calling executable does not know anything about the
database, and anyway the whole point of a class library is that it can be
called by many other objects. We don't want the same configuration data
spread all over the place.

Surely there's a way to fix this. Does anyone know how?

Peter (spitting blood and calling Microsoft all sorts of things you don't
want to hear)
There's another school of thought that says you should keep
configuration parameters out of the class library precisely because it
can be reused by multiple applications. Different applications might,
and often do, need to inject different parameters.

There are certainly cases where it makes sense to have a class libary
specific configuration file. I'm just not convinced this one of
them. And unfortunately I don't have an answer to your specific
question.

Brian

Mar 28 '07 #2

"Brian Gideon" <br*********@ya hoo.comwrote in message
news:11******** **************@ o5g2000hsb.goog legroups.com...
<snip />
There's another school of thought that says you should keep
configuration parameters out of the class library precisely because it
can be reused by multiple applications. Different applications might,
and often do, need to inject different parameters.

There are certainly cases where it makes sense to have a class libary
specific configuration file. I'm just not convinced this one of
them. And unfortunately I don't have an answer to your specific
question.

Brian
Thanks for that Brian.

Perhaps I should expand a bit. The configuration file is for the data
access component of an n-Tier application. It is a class library that is
called by another class library (the business logic layer component). The
business logic layer component is a remote component hosted in a Windows
service. Therefore the nearest executable is the Windows service.

The real executable is the one on the client machine (or, more likely, Web
server). One of the main points about n-Tier development is that the client
is completely unaware of implementation details in respect of things like
data accesss. It just wants the data. We might, for example, decide to
move away from SqlServer to MySql. Such a move should leave the client
entirely untouched. The last thing we would want to do would be to pass a
connection string to the remote object: it's one of the things we're trying
to keep off the client - especially if the client is a Web server.

Whether or not configuration can be retrieved from the Windows service
AppConfig file, I don't know. I'll have to do some tests. It's not great,
though, doing it that way because it takes config away from the thing you
actually want to configure (the data access code).
Peter
Mar 28 '07 #3
Peter,

If you really need such a change, you may need to look into web
services or remoting. The client would get its data from the web
service or remoting interface. Of course the host (IIS) would need to
know what connection string to use, but your Windows Service would no
longer need to know.

Of course what would happen now if you needed to change databases?
Would you have to redeploy the data access layer to the client? If
the answer is yes, then just put the connection string into
the .config for the service. No need to add the overhead of remoting
or web services if you would need to redeploy your data layer anyway..

Mar 28 '07 #4
Hi Peter,

I have to admit that I belong to the school of thought that says config's
should generally be more closely aligned to the calling exe's - however I
recently had to build a system that supported the use of plug-in's (for
loose-coupling purposes) that could be dropped into a folder and be
immediately available for use by the rest of the system. These plug-ins were
contained in assemblies and so using config files that travelled with the
assembly was what I opted for.

I also use .NET v2.0 and also did find some v1.1 code on the web that
roughly did what I wanted, although I remember having to make a few changes
to it to get it to work (the changes I made were minimal) - like you, I can't
remember the name of the author though (who really ought to get credit for
the code)... Just in case this wasn't the same code you have, I'll quickly
cover the usage scenario...

To use it, you need to:
(a) name the config file the same as the assembly, but with ".config"
appended to the end ofthe name - so e.g., if your assembly is called
"Hello.World.dl l" then the config should be named "Hello.World.dl l.config"
(b) make sure the config is in the same folder as the assembly
(c) have your config look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuratio n>
<configSections >
<section name="assemblyS ettings"
type="System.Co nfiguration.Dic tionarySectionH andler" />
<section name="otherAsse mblySettings"
type="System.Co nfiguration.Dic tionarySectionH andler" />
</configSections>
<assemblySettin gs>
<add key="SomeParam1 " value="SomeValu e1"/>
<add key="SomeParam2 " value="SomeValu e2"/>
<add key="SomeParam3 " value="SomeValu e3"/>
</assemblySetting s>
<otherAssemblyS ettings>
<add key="SomeOtherP aram1" value="SomeOthe rValue1"/>
<add key="SomeOtherP aram2" value="SomeOthe rValue2"/>
<add key="SomeOtherP aram3" value="SomeOthe rValue3"/>
</otherAssemblySe ttings>
</configuration>

(d) from your code, you can get the two separate config sections doing
something like (not compiled/tested):

AssemblyConfig m_assemblyConfi g = new
AssemblyConfig( Assembly.GetCal lingAssembly()) ;
AssemblyConfig m_otherConfig = new
AssemblyConfig( Assembly.GetCal lingAssembly(), @"otherAssembly Settings");

string someSetting = m_assemblyConfi g[@"SomeParam1 "];
string someOtherSettin g = m_otherConfig[@"SomeOtherPara m1"];
Here's what the main code looks like (again not compiled/tested):
using System;
using System.Reflecti on;
using System.Collecti ons;
using System.Xml;
using System.Configur ation;
using System.Runtime. CompilerService s;

namespace Put.Your.Namesp ace.Here
{
public class AssemblyConfig
{
[MethodImpl(Meth odImplOptions.N oInlining)]
public AssemblyConfig( ) : this(Assembly.G etCallingAssemb ly())
{
}

public AssemblyConfig( Assembly assembly)
{
m_settings = GetConfig(assem bly);
}

public AssemblyConfig( string nodeName)
{
m_settings = GetConfig(Assem bly.GetCallingA ssembly(), nodeName);
}

public AssemblyConfig( Assembly assembly, string nodeName)
{
m_settings = GetConfig(assem bly, nodeName);
}

public string this[string key]
{
get
{
string settingValue = null;
if (m_settings != null)
{
settingValue = m_settings[key] as string;
}
return (settingValue == null ? "" : settingValue);
}
}

public IDictionary Settings
{
get { return m_settings; }
}
protected IDictionary m_settings;

public static IDictionary GetConfig()
{
return GetConfig(Assem bly.GetCallingA ssembly());
}

public static IDictionary GetConfig(Assem bly assembly)
{
return GetConfig(assem bly, @"assemblySetti ngs");
}

public static IDictionary GetConfig(Assem bly assembly, string
nodeName)
{
IDictionary dictionary = null;
string configFile = assembly.CodeBa se + @".config";
XmlDocument configDocument = new XmlDocument();
configDocument. Load(new XmlTextReader(c onfigFile));
XmlNodeList nodes = configDocument. GetElementsByTa gName(nodeName) ;
foreach (XmlNode node in nodes)
{
if (node.LocalName == nodeName)
{
DictionarySecti onHandler sectionHandler = new
DictionarySecti onHandler();
dictionary = (IDictionary)se ctionHandler.Cr eate(null,
null, node);
break;
}
}
return dictionary;
}
}
}

One last thing to mention - I have definitely got this working in .NET v2.0
with no probs, however this was in a non-GAC scenario (the plug-ins that used
this code were not hosted in the GAC) - if tihs code doesn't work in your
setup, then it may well have something to do with OS/.Net
permissioning/security and/or the fact that your assemblies are being hosted
inside of the GAC (i'm too tired to think why this late in the day :-))..

Hope this helps you out...

Kind regards,
Patrick
--
Patrick Simpe-Asante
MCAD, MSCD.Net
"Peter Bradley" wrote:
OK. A bit behind the times, I know; but we're just moving over to .NET 2.0.
How on earth do you manage configuration settings in a class library in .NET
2.0? In version 1.1, we used a handy class called AssemblySetting s that
someone (I forget his name) had written. When the class library was
finished, you deployed it to the GAC and put the configuration files in the
GAC with the class library assembly. This no longer works. In fact trying
to get configuration data from anywhere but the calling executable falls
over.

This can't be correct. Our n-Tier architecture uses class libraries to
access data from the database and needs to obtain a connection string from a
configuration file. It's no good putting the configuration in the calling
executable because the calling executable does not know anything about the
database, and anyway the whole point of a class library is that it can be
called by many other objects. We don't want the same configuration data
spread all over the place.
Surely there's a way to fix this. Does anyone know how?


Peter (spitting blood and calling Microsoft all sorts of things you don't
want to hear)
Mar 28 '07 #5
Peter,

That's exactly what you have to do. A windows service is just an EXE.
If you create an EXE in .NET that is run as a windows service, then you can
create a config file where you place all of your configuration settings.

I am definitely of the same mind as what Brian described, that because
it is a class library, you should be specifying in the EXE that calls the
class library what the configuration is. It's just a matter of knowing
where your configuration file is going to be.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Peter Bradley" <pb******@uwic. ac.ukwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>
"Brian Gideon" <br*********@ya hoo.comwrote in message
news:11******** **************@ o5g2000hsb.goog legroups.com...
<snip />
>There's another school of thought that says you should keep
configuratio n parameters out of the class library precisely because it
can be reused by multiple applications. Different applications might,
and often do, need to inject different parameters.

There are certainly cases where it makes sense to have a class libary
specific configuration file. I'm just not convinced this one of
them. And unfortunately I don't have an answer to your specific
question.

Brian

Thanks for that Brian.

Perhaps I should expand a bit. The configuration file is for the data
access component of an n-Tier application. It is a class library that is
called by another class library (the business logic layer component). The
business logic layer component is a remote component hosted in a Windows
service. Therefore the nearest executable is the Windows service.

The real executable is the one on the client machine (or, more likely, Web
server). One of the main points about n-Tier development is that the
client is completely unaware of implementation details in respect of
things like data accesss. It just wants the data. We might, for example,
decide to move away from SqlServer to MySql. Such a move should leave the
client entirely untouched. The last thing we would want to do would be to
pass a connection string to the remote object: it's one of the things
we're trying to keep off the client - especially if the client is a Web
server.

Whether or not configuration can be retrieved from the Windows service
AppConfig file, I don't know. I'll have to do some tests. It's not
great, though, doing it that way because it takes config away from the
thing you actually want to configure (the data access code).
Peter


Mar 28 '07 #6
Hmm. I thought I'd made this clear.

We *are* using Web services and remoting. That's why the difficulty arises.

The actual architecture is like this:

* A SQL table records transactions (students enrolling, withdrawing,
transferring courses, changing personal details, etc)
* A BizTalk SQL adapter feeds the transaction data, in order, to BizTalk
* BizTalk processes the transactions one-by-one and calls a SOAP adapter to
manipulate Active Directory
* The Web service called by the SOAP adapter connects to a remote object to
obtain lookup data from a remote database
* The remote object (a SAO hosted in a Windows service) obtains lookup data
and makes some changes to the remote database (because some of the lookup
data, such as the next mail server to allocate on a round robin basis, must
be looked up once and then incremented).
* Using the lookup data, the remote object manipulates AD and returns
status information to the Web service
* The Web service returns the status, or a SOAP Fault, to BizTalk
* BizTalk completes its transaction or takes compensating action (if any
errors have occurred)

That's why the config information cannot be with the executable (except,
perhaps, the Windows service). It's at several remote hops away, using an
SOA philosophy (i.e. the client (BizTalk) asks for a service (update AD),
but has no knowledge of how that service is performed. It doesn't even know
that the remote object has to get data from a database. Similarly, the
remote object's client (the Web service) simply asks the remote object to
manipulate AD. It has no idea how this might be carried out.

Sorry if I'd not been clear enough before.

Cheers
Peter

"Andy" <an***@med-associates.comw rote in message
news:11******** *************@y 66g2000hsf.goog legroups.com...
Peter,

If you really need such a change, you may need to look into web
services or remoting. The client would get its data from the web
service or remoting interface. Of course the host (IIS) would need to
know what connection string to use, but your Windows Service would no
longer need to know.

Of course what would happen now if you needed to change databases?
Would you have to redeploy the data access layer to the client? If
the answer is yes, then just put the connection string into
the .config for the service. No need to add the overhead of remoting
or web services if you would need to redeploy your data layer anyway..

Mar 29 '07 #7
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:uG******** ******@TK2MSFTN GP05.phx.gbl...
Peter,

That's exactly what you have to do. A windows service is just an EXE.
If you create an EXE in .NET that is run as a windows service, then you
can create a config file where you place all of your configuration
settings.
Yep. We're going with that, I think.

Thanks to everyone for their help.
Peter
Mar 29 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2881
by: Florida Coder | last post by:
I have the need to store some application specific configuration data to be used by a class library and or a windows service. I would like to do this in a fashion similar to the way we do with applications and web services without having to use the machine.config file. I know that I could use the code below to do this but the problem then becomes that when using a shared assembly both the assembly and the config file need to be put...
2
1322
by: No One | last post by:
I have a web project that uses a C# class library for some of the backend functionality. So of these provider classes have XML configuration files. I have a reference from the web project to the class library, but the configuration are not copied into the web application space for running. Is it possible to have VS .Net copy the files over or do I need to continue to hand copy them?
9
8330
by: craig.overton | last post by:
All, I am currently developing an FTP class in VB.NET. It's kid tested, mother approved when trying to access an FTP Server on a Windows box meaning I can connect, run commands, upload and download a file no problem. My issues come when I try to use the same class with the same commands to access an FTP server on a UNIX box. I can connect and login just fine, but after that all my commands come back "500 'PWD': command not understood."....
3
3942
by: ad | last post by:
I have a web application and a class library in a solution. The class library is make of typed datasets, and include many Table in it. The connection strings of the TableAdapters is come from the app.config of that class library. The Web application refer the class library, and the connection strings of WebApp is come from the web.config of it. It is ok in developing step, I can make both connection string identical in
0
6611
by: Herman Jones | last post by:
I'm getting the following error when I build a Class Library project: Embedding manifest... Project : error PRJ0002 : Error result 1 returned from 'C:\WINDOWS\system32\cmd.exe'. It happens with every the of C++ project I try to build. Not just Class Libraries, but a plain Windows Form Application as well. I've tried creating new projects with nothing but shell built by the Wizard, but I still get the error.
3
18534
by: Fernando Chilvarguer | last post by:
Hello! I created a Class Library project in VS2005. Then, using VS, I was able to add a connection string to the project settings, which automaticaly created an app.config file for me. If I try to access the configuration using System.Configuration.ConfigurationManager, I get a NullException. The code: string connectionString =
0
2508
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
16
1789
by: Robert Dufour | last post by:
Here's the class code snippet Imports System.Configuration.ConfigurationManager Public Class Class1 Public _strTestSetting As String Public Sub SetTestsetting()
5
8614
by: Rainer Queck | last post by:
Hello NG, Is it possible to share the settings of an application with a class libreary? In my case I have a application and a set of different reports (home made) put into a class library. The plan is to delivere different report.dlls with the main app. But it is essentially importent, that the reports and the app use the same settings.
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.