 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 12th, 2011, 11:14 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Windows Service - Design Concept Question
For the first time (how I have managed so long, I don't know) I have come across the need to write a windows service, which I have done (VS2010 C#) and the core functionality is working just fine (beginners luck!).
What I would like to know is what is considered to be the "preferred" methodology for entering and storing the service configuration?
My service monitors an input folder for XML data files, which it imports into a database, so I am in need of the following configurable variables: - Input folder (where the files are dropped for processing)
- Output folder (for processed files)
- Connection String (for the DB)
- Interval (how many minutes between polling)
Thoughts appreciated...
__________________
Regards,
Sean
|
|

April 12th, 2011, 01:26 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Sean,
I guess it depends on how often you expect this information to change, and whether you want your application pick it up automatically.
A simple way is to store your data in the application's App.config file and use the ConfigurationManager to read the data during the startup phase.
Alternatively, create your own XML schema and file, and read in a custom file during startup. Then setup a FileWatcher to monitor changes to this file at run-time and reload the configuration when changes are made.
The downside of the latter option is that you need a good way to deal with these changes at run-time. E.g. you don't want to read from one database connection and then try to save it to another. So while reloading the configuration data, you need to halt other operations, or make sure they complete first.
Hope this helps,
Imar
Last edited by Imar; April 13th, 2011 at 05:38 AM..
|
|

April 13th, 2011, 05:06 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
The app.config sounds like the way to go ... but,
I have added an app.config, which I have named as <program>.exe.config and inserted my fields as follows:
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
<connectionStrings>
<addname="myDB"providerName="SQL Server"connectionString="Server=localhost;Database=myDB;Trusted_Connection= True;"/>
</connectionStrings>
<appSettings>
<addkey="inputFolder"value="c:\test\input"/>
<addkey="outputFolder"value="c:\test\output"/>
<addkey="timerInterval"value="5"/>
</appSettings>
</configuration>
But, when I try to refer to the configuration from my application (below), everything returms null values.
inputFolder = ConfigurationManager.AppSettings["inputFolder"];
conString = ConfigurationManager.ConnectionStrings["DVLA"];
I have had a look around the net and can see that other people have had the same issue, but the only response seems to be "it worked on mine" and nothing which actually points to what the error might be.
Any ideas, appreciated..
__________________
Regards,
Sean
|
|

April 13th, 2011, 05:14 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Sean,
Are you deplying the app.config file along with the main executable? The file needs to be in the same location as the main service .exe or it won't be found. Also, it may not be copied automatically to the target location when it's changed, so the file may be there, but wth other, or no values.
Cheers,
Imar
|
|

April 13th, 2011, 05:24 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
At this stage, I am just debugging on my local machine and have checked the file is present (with values), which it is...
myApplication\bin\Debug\myApplication.exe
myApplication\bin\Debug\myApplication.exe.config
myApplication\myApplication.exe.config
myApplication\Service1.cs ..... etc.,
__________________
Regards,
Sean
|
|

April 13th, 2011, 05:37 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It's been a while since I wrote a Windows service, but I think the name of the application is changed when you're debugging. This in turn changes the name of the config file that the application looks for. Try looking at this path:
Code:
string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
Does it contain something like vshost? If so, your config may not be where you think it is and you may need to copy it to myApplication.vshost.exe.config as well.
Cheers,
Imar
|
|

April 13th, 2011, 06:11 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
That's got it ... thank you, very much!
__________________
Regards,
Sean
|
|
 |