|
Hi,
I have a web application and a windows service written in C# and I want to share the config file between the two. At the moment it looks like I would have to copy them each time I make an update. I would rather not do this.
Any ideas of how I may achieve this?
Cheers,
Andy.
|
|
|
|
|
AFAIK, you will have to do this. Unless, of course, you launch both the WebService and your Windows app out of the same directory (not likely...)
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
That wouldn't work, either. ASP.NET uses Web.config and applications (using the loader information in the PE/COFF executable that mscoree.dll uses) use <appname>.exe.config.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Smack! That's right! Ouch... Now I need some Advil.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I've found an attribute on appSettings called file which can point to another file and I was hoping to use that as an alternative, but I can't seem to get that to work either. I just don't see where I am going wrong.
In the web.config I've got:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="general.config"/>
...
</configuration> And in the general.config I've got
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="encryption_level" value="2"/>
<add key="timer_interval" value="60" />
</appSettings>
But, in my tester application which just writes out the app settings it returns no information. If the appSettings section is copied into the web.config file it works as expected.
|
|
|
|
|
Try using an absolute path rather than a relative path. e.g.
<br />
<appSettings file="C:\inetpub\wwwroot\MyWebApp\general.config"/><br />
Does this help?
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
|
|
|
|
|
As Dave said, that really isn't possible.
There is one way, though it's a pretty big hack and won't really solve your problem. You can create a launcher with it's own config file that specifies the path of the executable (optional) and the path to the .config file. This launcher is executable as normal but then grabs the path to your current Windows Service executable (or hard-code it, though I recommend against that), sets up a second AppDomain (see AppDomain.CreateDomain ) and sets the AppDomain.SetupInformation.ConfigurationFile using the path you configured.
This isn't recommended, though. Those .config files are separate for security reasons and because a true application boundary exists (they're obviously both separate executables). Also, A Windows Service and an ASP.NET web application really share very little of the .config in common, such as the appSettings and assemblyBinding (as well as other runtime sections).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hmmmm... That solution is... ummmm... interesting.
Heath Stewart wrote:
A Windows Service and an ASP.NET web application really share very little of the .config in common
I realise that the Web.cofig file contains many settings that the Service won't need and vice versa, but there is a lot of commonality. The database connection strings, the directory paths for the data files, timeout settings and so on. In total there are about 40 settings that are shared between the web application, the service and some unix processes. Things like the database connection strings are actually split up into parts because of the different technologies.
|
|
|
|
|
Interesting or not, it's fact. The worker process for ASP.NET sets up the AppDomain to use Web.config. The loader information in the PE/COFF executable bootstraps mscoree.dll, which uses the executable filename + .config.
You might consider writing a custom configuration section (see IConfigurationSectionHandler ) using something as simple as XML Serialization to get an object. You could also reference a common filename in your config section where your handler gets that file and then deserializes that (since a .config file is nothing more than XML) or parse the DOM yourself.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
Interesting or not, it's fact.
I realise that. I meant that it was "interesting" in a "I really don't want to be implementing major hacks" kind of way.
What about the <appsettings file="some_other.config">? I'm stumpt at why that doesn't work either. Any ideas?
If not it looks like I'm either going to have to code around it, or duplicate the settings.
Cheers,
Andy
|
|
|
|
|
I already answered your question, unless you're talking about a different one. ASP.NET and Windows Forms applications (or even console applications - anything that is self-executable) use a different way to refer to their .config files. As I mentioned, though, just make your own IConfigurationSectionHandler implementation and implement it in such a way that it can pull "global" settings from a file or some other backing store.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a listbox that is periodically recieving lines of text. The listbox eventually gets more info than is needed. I would like to have it so that for each new line of text that is added to the top of the listbox, the oldest line is removed from the bottom (once the form is "full"), so that there can be a finite number of lines of information, with the new constantly replacing the old. I haven't been able to find out or figure out how to do that.
Thanks very much indeed!
|
|
|
|
|
You should be able to use
ListBox1.Items.Insert(0, "MyNewItemGoesHere")
Try
ListBox1.Items.RemoveAt(25)
Catch
End Try
The Try catch block is in there because I don't know what would happen if you tried to RemoveAt an index that didn't exist yet.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
What's all this VB.NET code you're writing in the C# forum lately, Dave? Have you converted to the "dark side"?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I float back and forth, dark-side...light-side...
You really have to taste them both before you commit to one power or the other...
I was actually only trying on the black suit and cape with the funny respirator noise. I can't get the James Earl Jones voice to work though! Oh well...back to C# I go!
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave Kreskowiak wrote:
I can't get the James Earl Jones voice to work though!
You won't sound like him with your helmet off.
C# vs. VB.NET: really, I don't care. Good developers (that includes you, of course) can switch back and forth realizing that it's only a difference in syntax (although knowing a little of that syntax is necessary, which I do I hate to admit). It's good to brush up on VB.NET syntax from time to time, but remember that even Luke stopped from killing his father before it was too late and he was completely turned!
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Oh! I found the problem! I forgot to change the batteries in my helmet!
I'm jumping back and forth between both forums... I just forget which one I'm in at the time. The same thing happens while I'm doing stuff in VS. I can be writing code in VB and suddenly start typing C# stuff or the other way around. Little red squiggly lines start poppin' up all over the place...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
|
OK. CHANGE THE CHANNEL!
--End of line
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Damn! VB code in the C# forum again!
OK...translated:
ListBox1.Items.Insert(0, "MyNewItemGoesHere")
try
{
ListBox1.Items.RemoveAt(25)
}
catch
{
}
What forum am I in again?? Damn! 35 years old and I can't remember which way is up!
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I like to extend the TreeNode class through Inderitance to add a string property to the class. The TreeNode Class has 5 constructors, do I need to provide 5 contructors in my TreeNode class ? Is there some way I can pass a variable set of paramters to the base class's constructor ?
|
|
|
|
|
Guinness4Strength wrote:
do I need to provide 5 contructors in my TreeNode class
No, and you'll still be able to call those 5 constructors on your inherited class.
Guinness4Strength wrote:
Is there some way I can pass a variable set of paramters to the base class's constructor ?
Why? You can provide your own constructors that call the base constructors, and optionally, set various properties of the TreeNode while in the body of your constructor.
#include "witty_sig.h"
|
|
|
|
|
You only need to define the constructors that you want, but be sure to call the base class's corresponding constructor like so:
public class MyTreeNode : TreeNode
{
public MyTreeNode(string text) : base(text)
{
}
} Also be sure that you cast the TreeNode from the tree to your class (ex: MyTreeNode ) when you need to access that string. The TreeNodeCollection will store a reference to your derivative class, but will only return a TreeNode since that's all it deals with. That object is still an instance of your derivative class, though.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok, thanks Heath.
I assumed I still needed to define the constructors in my class, but wasn't sure...
|
|
|
|
|
No, you don't have to provide 5 constructors for your TreeNode class. Since your extending the existing TreeNode class, all you have to do is supply your own constructor if one of the existing ones is not suitable. You could override the existing contructors to provide slightly different functionality, like filling in your extended fields with default data, or provide a completely new one so long as the signature of your constructor doesn't match an existing one.
If you wanted to provide a constructor that took two String parameters and an existing TreeNode object, instead of a single String and TreeNode, you could because there is no matching constructor in the base class constructor list.
public TreeNode( string text, string altData, TreeNode[] children );
Now, if you wanted to provide a new constructor that takes the same type and same number of parameters, like:
public TreeNode( string text, TreeNode[] children );
and
public TreeNode( string altData, TreeNode[] children );
Then you would have to rearrange your parameter list because you can't ahve two constructors with the same signature, in this case TreeNode(string,TreeNode[]) :
public TreeNode( TreeNode[] children, string altData );
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|