As you all know I hate visual basic, so I know very little about it. I am trying to convert this tiny piece of code from visual basic to C#. I think I got it correct but I wanted to make sure.
Prevent two different web pages from trying to update the Application object as the same time, which would lead to corruption.
The application object is set in the case of the connection string in your example. It is not being updated as you are specifying the value at instantiation. I have never locked a connection string in an application object and have never had issues. Doing
so would only impose a performance constraint. Now, say you were doing the below, then it would make sense to do a lock to ensure that no more than one client at a time can update it:
I'm not sure whats going on in that book, I read on MSDN more about using it for locking when you are incromenting too. I didn't read the entire book from front to back so I might be missing something there.
Sometimes those books do things one step at a time, then 5 pages later add the rest of the code that would require the locking.
Either way, I decided to put my connection string in the web.config file. I think its a wiser choice in the end.
Well I guess my main problem is where to store the connection string then. In the communityserver source they store the connection string in the web.config file instead of the global file.
I want to also store my connection string in the web.config file. Then I tried to access it with the ConfigurationSettings.AppSettings["ConnectionString"] statement. (I'm not sure if that is the exact syntax, but you should understand). I'm trying to use
the specialized class to retrieve information from the web.config file.
However, if the config file is in Application1 directory, and I need to access it from a DLL in the component project. It just returns a empty string. I think because it doesn't know where to find the corrrect web.config file.
How can I access the web.config file when I don't know what its exact path is, or is there some place else I can store the connection string?
I tried figuring out how communityserver is accessing it, but there is so much code and so many source files I don't know where to start.
Yes, you are correct that the component assembly doesn't know where the web.config file is. However, it is *possible* for your component to read from the web.config if it exists in Application1 directory. Just do the same as the ConfigurationSettings.AppSettings["ConnectionString"]
and it will work.
If these two things don't exist in the same AppDomain, then you could find the web.config file and use XPath to query up on the value. Just do a little of research into the XmlDocument class for basic, basic implementation (not optimized though).
There's also one tip I would like to offer you: whenever you do access to the configuration like that, create another class that just accesses it like the following:
public sealed class ConfigurationAccess {
public static string ConnectionString {
get {
return ConfigurationSettings.AppSettings["ConnectionString"];
}
}
}
On top of that, for more 'professional' looking types of configuration files, have a look at implementing IConfigurationSectionHandler.
PS: if you are using .NET 2, I think with the ConfigurationManager class (or something like that; mind me - I haven't coded in .NET 2 yet) you can access the connection strings set by you...
I put the IConfigurationSectionHandler in my log for the application. I will look into it but right now I have so much stuff I'm trying to learn I can't take on another thing, lol. Besides this is just beta 1 so I can move something small like that around
later.
I will make sure I put the method for retreiving the connection string in a sealed class. I can see the importance of that.
My files however don't exist in the same directory, my asp.net application is installed in the root/[application]/www/ folder and the web.config file is in that folder, and the component that needs to read the file is in the root/[application]/components
directory.
I have used the XMLDocument before so I can look up an example on that again.
So I guess I would know the name of the path when I think about it because no matter what web server this application is installed on, the web.config file will always be in the ?.../[application]/www folder.
I'm assuming there is a way to move up one directory with the XPath? For Example: "XPath(../www)" ?? This would point the path to the www folder that's located in the application path. Or is there a different way?
Will this work? The components should always be stored in the [components] folder inside the [application] folder. If the admin changes this configuration, which they have no logical reason to that I can see, then I guess its their problem. I can only do
so much.
Bluebarry
Member
486 Points
126 Posts
Convert this code VB to C#
Dec 23, 2005 11:43 PM|LINK
As you all know I hate visual basic, so I know very little about it. I am trying to convert this tiny piece of code from visual basic to C#. I think I got it correct but I wanted to make sure.
Visual Basic
[pre]
<script runat="server">
Sub Application_OnStart()
Application("conNwind") = "connectionstring"
End Sub
</script>
[/pre]
C#
[pre]
protected void Application_Start(Object sender, EventArgs e)
{
Application.Lock();
Application.Contents["conIDB"] = "connectionstring";
Application.UnLock();
}
[/pre]
This is going in the global.asax file.
Thanks very much.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: Convert this code VB to C#
Dec 24, 2005 12:00 AM|LINK
It should be (notice the 2nd line):
protected void Application_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["conIDB"] = "connectionstring";
Application.UnLock();
}
Note To Moderator: For some reason this got posted 2 times, please delete this post.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: Convert this code VB to C#
Dec 24, 2005 12:01 AM|LINK
It should be (notice the 2nd line):
protected void Application_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["conIDB"] = "connectionstring";
Application.UnLock();
}
Also, since, Application["conIDB"] isn't something that is modified like you are incrementing or decrementing it like a count, I wouldn't lock it.
Bluebarry
Member
486 Points
126 Posts
Re: Convert this code VB to C#
Dec 24, 2005 12:29 AM|LINK
hmm.. I'm not sure.
They are used in my book "Web Based Development with .Net" on page 392.
This is the code from my book, this is in visual basic.
[pre]
'Prevent two different web pages from trying to update the Application object as the same time, which would lead to corruption.
Application.Lock
'Add the connection string, so all pages can access it.
Application.Contents("northwind") = "Data Source=localhost;UserID=northwind;password=ydniw;Initial Catalog=northwind"
'Release the lock
Application.Unlock
[/pre]
I just dont understand why they are using it in the book then...
Can someone please explain?
Thanks.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: Convert this code VB to C#
Dec 24, 2005 01:29 AM|LINK
The application object is set in the case of the connection string in your example. It is not being updated as you are specifying the value at instantiation. I have never locked a connection string in an application object and have never had issues. Doing so would only impose a performance constraint. Now, say you were doing the below, then it would make sense to do a lock to ensure that no more than one client at a time can update it:
protected void Application_Start(object sender, EventArgs e)
{
Application.Lock();
m_UserCountOnline++;
Application.Unlock();
}
Bluebarry
Member
486 Points
126 Posts
Re: Convert this code VB to C#
Dec 24, 2005 02:13 AM|LINK
I'm not sure whats going on in that book, I read on MSDN more about using it for locking when you are incromenting too. I didn't read the entire book from front to back so I might be missing something there.
Sometimes those books do things one step at a time, then 5 pages later add the rest of the code that would require the locking.
Either way, I decided to put my connection string in the web.config file. I think its a wiser choice in the end.
Thanks.
master4eva
Star
13635 Points
2719 Posts
Re: Convert this code VB to C#
Dec 26, 2005 12:30 PM|LINK
Bluebarry
Member
486 Points
126 Posts
Re: Convert this code VB to C#
Dec 26, 2005 08:25 PM|LINK
Well I guess my main problem is where to store the connection string then. In the communityserver source they store the connection string in the web.config file instead of the global file.
I want to also store my connection string in the web.config file. Then I tried to access it with the ConfigurationSettings.AppSettings["ConnectionString"] statement. (I'm not sure if that is the exact syntax, but you should understand). I'm trying to use the specialized class to retrieve information from the web.config file.
However, if the config file is in Application1 directory, and I need to access it from a DLL in the component project. It just returns a empty string. I think because it doesn't know where to find the corrrect web.config file.
How can I access the web.config file when I don't know what its exact path is, or is there some place else I can store the connection string?
I tried figuring out how communityserver is accessing it, but there is so much code and so many source files I don't know where to start.
thanks again.
master4eva
Star
13635 Points
2719 Posts
Re: Convert this code VB to C#
Dec 27, 2005 05:25 AM|LINK
If these two things don't exist in the same AppDomain, then you could find the web.config file and use XPath to query up on the value. Just do a little of research into the XmlDocument class for basic, basic implementation (not optimized though).
There's also one tip I would like to offer you: whenever you do access to the configuration like that, create another class that just accesses it like the following:
public sealed class ConfigurationAccess {
public static string ConnectionString {
get {
return ConfigurationSettings.AppSettings["ConnectionString"];
}
}
}
On top of that, for more 'professional' looking types of configuration files, have a look at implementing IConfigurationSectionHandler.
PS: if you are using .NET 2, I think with the ConfigurationManager class (or something like that; mind me - I haven't coded in .NET 2 yet) you can access the connection strings set by you...
Bluebarry
Member
486 Points
126 Posts
Re: Convert this code VB to C#
Dec 27, 2005 05:56 AM|LINK
Thanks for the reply!
I put the IConfigurationSectionHandler in my log for the application. I will look into it but right now I have so much stuff I'm trying to learn I can't take on another thing, lol. Besides this is just beta 1 so I can move something small like that around later.
I will make sure I put the method for retreiving the connection string in a sealed class. I can see the importance of that.
My files however don't exist in the same directory, my asp.net application is installed in the root/[application]/www/ folder and the web.config file is in that folder, and the component that needs to read the file is in the root/[application]/components directory.
I have used the XMLDocument before so I can look up an example on that again.
So I guess I would know the name of the path when I think about it because no matter what web server this application is installed on, the web.config file will always be in the ?.../[application]/www folder.
I'm assuming there is a way to move up one directory with the XPath? For Example: "XPath(../www)" ?? This would point the path to the www folder that's located in the application path. Or is there a different way?
Will this work? The components should always be stored in the [components] folder inside the [application] folder. If the admin changes this configuration, which they have no logical reason to that I can see, then I guess its their problem. I can only do so much.
Thanks!