|
-
November 3rd, 2006, 11:21 AM
#1
get the current application using the assembly
I have and Windows Service and a couple of Web Applications all using the same assemblies.
When a call is made to one method in particular in one of these common assemblies I want to either pass it the current applications name or path or have the method itself determine what called it.
The reason being I have some System.Configuration stuff goin on within the method and if it's a Windows Service that called the method I want it to use the
Code:
System.Configuration.ConfigurationManager.OpenExeConfiguration(appnameandpath);
and if it's a website I want it to use the
Code:
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
"/websitename");
How can I achieve this?
The .config files contain some Custom Sections which I wish to access, I have no problem getting to the Web Applications but determining the Windows Service path and name and opening its app.config is proving difficult.
Code:
public static string GetMessageString(string server, string messagename)
{
string serverName = "/";
string message = string.Empty;
System.Configuration.Configuration configuration;
MessageSection section;
//Try the current Applications .config file first, if it exists, e.g. the ManagementServer.config
try
{
configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(server);
section = (MessageSection)configuration.GetSection("messagegroup/messagesection");
message = section.GetMessage(messagename);
}
catch//if the exe's config fails then try the Web application web.config
{
configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
"/" + server);
section = (MessageSection)configuration.GetSection("messagegroup/messagesection");
message = section.GetMessage(messagename);
}
return message;
}
-
November 3rd, 2006, 03:30 PM
#2
Re: get the current application using the assembly
Well, I encountered the same situation but I took a short cut to solve this problem. I let the user specify whether the calling application is windows (actually, windows service) or web application (.NET version 2.0, not sure about 1.1)
Code:
/// <summary>
/// <see cref="ApplicationType"/> is used to indicate whether the running application is
/// either a web or windows application
/// </summary>
public enum ApplicationType
{
/// <summary> Indicates that the application is of type web application </summary>
Web,
/// <summary>Indicates that the application is of type windows application</summary>
Windows
}
public static void WriteKeyValuePair(string sectionName, string key, string value, ApplicationType applicationType)
{
string methodName = "WriteKeyValuePair";
oFileLogger.LogEnter(methodName);
try
{
// Get the configuration object
System.Configuration.Configuration systemConfiguration = null;
if (applicationType == ApplicationType.Web)
{
// the following code will read the web.config from the root directory
systemConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
}
else
{
// the following code will read the xyz.exe.config from the working folder
systemConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
}
......
}
hope this helps a bit..
-
November 7th, 2006, 04:27 AM
#3
Re: get the current application using the assembly
Hi poochi,
that's useful but where are you getting the applicationType from?
A workaround I have found is to try to open the Windows application config file first, if it's a Web App this will fail, if it fails try the Web application config file
Or vicaversa, try to open the web app config with the servername passed in, if you were trying to open a Windows app config you would pass in an emptry string, this would fail in the Web app config open and the catch would try the Windows app config.
Code:
public static string GetMessageString(string server, string messagename)
{
string message = string.Empty;
System.Configuration.Configuration configuration;
//Try the web.config file first
try
{
configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
"/" + server);
}
catch//if the web.config fails then try the Windows application config e.g. the GalaxyManagementServer.config
{
configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
.........
return message;
}
Last edited by ireland; November 7th, 2006 at 04:29 AM.
-
November 7th, 2006, 01:56 PM
#4
Re: get the current application using the assembly
Relying on exception for decision making is discouraged in .NET (.NET introduced the "TryParse()" in int, double, datetime etc just for this reason)
The user of my library knows whether they are creating Web or Windows application. So, I let the user pass this information as one of the parameters to my method (the last parameter). There may be another way to find the type of of the application that is using the library that I am not aware of. I would also appriciate if anyone know how to do it.
Do you need to pass the server name to the "OpenWebConfiguration" method? I works fine for me with "~" which looks at the root folder of the web application.
[Added Later] I did a quick search on the web and found the following information.
"to differentiate between asp.net app
and otherwise, if System.Web.HttpContext.Current is null, it's is NOT an
asp.net app, otherwise it is.
"
Credit goes to Marina Google Group post
Last edited by poochi; November 7th, 2006 at 02:14 PM.
-
November 8th, 2006, 04:25 AM
#5
Re: get the current application using the assembly
Hey Poochi,
First of all how could I use TryParse in this case?
I may not need to open pass the web app name to OpenWebConfiguration, if I pass in nothing it opens the present web apps config, this may not be always what I want as the settings I'm looking for are at present only in one web app's web.config, so I always force it to open that particular web.config.
I've had a look at the System.Web.HttpContext.Current, it looks good, I'll have to test it some more with the Windows app but it is probably the solution.
BTW and idea why I shouldn't use the Try/Catch as a decision maker, would it be ok if I also rethrew the original exception and then continued with the second piece of functionality in the Catch.
Thanks for your help.
Eire.
-
November 8th, 2006, 11:12 AM
#6
Re: get the current application using the assembly
 Originally Posted by ireland
Hey Poochi,
First of all how could I use TryParse in this case?
TryParse() is nothing to do with your problem. I mentioned it to make a point. Ever wondered the difference between Parse() and TryParse()?
BTW and idea why I shouldn't use the Try/Catch as a decision maker, would it be ok if I also rethrew the original exception and then continued with the second piece of functionality in the Catch.
Thanks for your help.
Eire.
1. Creating and raising exception is a time consuming process. In your case you can find a solution without raising the exception.
2. "Error Raising and Handling Guidelines" is here. One of the guidelines is "Do not use exceptions for normal or expected errors, or for normal flow of control. ". Raising exceptions may work but there are good, bad and ugly way of programming and it is up to us to chose what is good .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|