Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
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
 
Old April 12th, 2011, 11:14 AM
Authorized User
 
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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
 
Old April 12th, 2011, 01:26 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 13th, 2011 at 05:38 AM..
 
Old April 13th, 2011, 05:06 AM
Authorized User
 
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
 
Old April 13th, 2011, 05:14 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 13th, 2011, 05:24 AM
Authorized User
 
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
 
Old April 13th, 2011, 05:37 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 13th, 2011, 06:11 AM
Authorized User
 
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
Default

That's got it ... thank you, very much!
__________________
Regards,

Sean





Similar Threads
Thread Thread Starter Forum Replies Last Post
Impersonate with windows service for Service A/C vinod_yadav1919 C# 0 October 18th, 2008 02:29 PM
Accessing Windows service from a windows app sajid08 C# 1 October 6th, 2006 10:25 AM
Windows based Problem Design Solution Book ricpue BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 September 1st, 2006 07:47 AM
Design Concept shazza Biztalk 1 August 23rd, 2006 09:54 AM
Windows Service hunghung General .NET 3 January 20th, 2005 01:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.