PDA

Click to See Complete Forum and Search --> : web.config appsettings


Arc
Jun 16th, 2003, 12:10 PM
Hi, this book i am reading says there is an "appsettings" section in the web.config file. I have been thru the web.config file and there is no such section.


Is the book wrong or is my web.config file missing something?

Cander
Jun 16th, 2003, 12:23 PM
Then put one in there

DevGrp
Jun 16th, 2003, 12:26 PM
Adding to what cander said. All the settings are not included in the web.config file when first created. Only the necessary parts, so you have to add the parts that you want to include.

Arc
Jun 16th, 2003, 12:30 PM
Yah i tried that...

<appSettings>
<add key="Northwind"
value="Provider=SQLOLEDB; Data Source=localhost;
Initial Catalog=Northwind; User ID=sa; Password=;"
/>
</appSettings>

but when i run the app i get an error that says something about unable to start debugging on the web server please make sure there are no syntax errors in the web.config...

Cander
Jun 16th, 2003, 12:34 PM
probably put it in the wrong place. <appSettings> element goes direcly inside the <configuration> element

Arc
Jun 16th, 2003, 12:39 PM
Ahhh yep that did it. I had it inside the system.web element.

Thanks!:D

Cander
Jun 16th, 2003, 12:44 PM
;)

hellswraith
Jun 16th, 2003, 06:39 PM
Does anyone have a web resource that lists all the elements that are recognized in the web config file?

It would be cool to be able to know all the different things we can put in it without having to hunt down each element on the web.

Mike Hildner
Jun 25th, 2004, 05:18 PM
I'm having a similar problem in a web service, but from what I can find, things look ok.

This is part of my web.config:

<appSettings>
<add key="database_connection_string" value="Persist Security Info=False;Integrated Security=SSPI;database=Patriot;server=MIKELAP"/>
</appSettings>

</configuration>


And after this line executes, myConnectionString is still null

myConnectionString = ConfigurationSettings.AppSettings["database_connection_string"];


What am I missing?

pvb
Jun 25th, 2004, 07:03 PM
MSDN has an overwhelming amount of info on this... (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfnetframeworkconfigurationfileschema.asp)

Mike Hildner
Jun 26th, 2004, 12:11 PM
MSDN has an overwhelming amount of info on this...

Sure they do, where do you think I copied and pasted my code from :) ?

Problem is, I can't get it to work. I must have read at least three different ways to do the same thing. But I still can't figure it out. Hence my post.

Ft. Collins, eh? I went to school at CSU. My sister lives there. Help me out and I'll buy you a beer next time I visit :)

Mike

pvb
Jun 26th, 2004, 12:29 PM
hmmm, well I DO love beer (http://www.newbelgium.com/)...

ok, since you've posted only a few pieces what's the big picture. Is this just a plain vanilla website, and your webservice is at the root level? Or is it buried in a virtual directory or something and there are multiple web.configs? how's your site set up.

How are you testing that that variable is nulll? are you stepping through the code and trying to watch it or something else? exception being thrown, etc...

The code you have should work so something else has to be happening somewhere else.

Oh, and you say you cut and pasted your code from MSDN, was there a specific example you were trying out or was it just a code snippet and not really an example? Maybe post that or a link to it. And post your web.config file if you can(without any sensitive info if there is any).

Mike Hildner
Jun 26th, 2004, 12:55 PM
LOL. I went and looked at your profile and saw that one of your interests were micro-brews. I was in Ft. Collins when New Belgium started up. I've drank hundreds of gallons of Fat Tire - still my favorite, but I'm getting off topic.

I guess it's a plain vanilla web service - this is a web service and not a web site - if there's a difference. I'm just starting this and went with the defaults, so nothing too complex. Attachment is a zipped screen shot and my web.config.

Yeah, it was a code snippet, you can find it at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfappsettingselement.asp

I just tried to mimic the example. I can tell because an exception is being thrown. Here's a couple of relevant code pieces:


public class PatriotClientWebService : System.Web.Services.WebService
{
string myConnectionString;

public PatriotClientWebService()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();

myConnectionString = ConfigurationSettings.AppSettings["database_connection_string"];



and then this:


[WebMethod]
public DataSet GetUserTables()
{
DataSet ds = new DataSet("dsUserTables");
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = myConnectionString;
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = new SqlCommand("SELECT * FROM SYSOBJECTS WHERE TYPE = 'U' AND OBJECTPROPERTY(id,'IsMSShipped') = 0 ORDER BY NAME", myConnection);
int i = myAdapter.Fill(ds, "dtUserTables");
return ds;
}


The InvalidOperationException happens on the line with myAdapter.Fill - saying the ConnectionString property has not been initialized. When I hover with the mouse over myConnectionString, the tool tip says it's null.

Thanks,
Mike

Mike Hildner
Jun 26th, 2004, 01:14 PM
Looking at this a little closer, my attempt at initializing myConnection string in the constructor does not work. Guess I'm doing something wrong there. Attachment is a zipped screen shot immediately after I try to set myConnectionString.

pvb
Jun 26th, 2004, 03:25 PM
I couldn't get your example to not work... weird. So I've rewritten your method a little differently, and not for any other reason than a shot in the dark, it does the exact same thing your other one did.
Here's the entire contents of the code behind for Service1.asmx:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace PatriotGames
{
/// <summary>
/// Summary description for Service1.
/// </summary>
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public DataSet GetUserTables()
{
string myConnectionString = ConfigurationSettings.AppSettings[ "database_connection_string" ];
if( myConnectionString == null || myConnectionString.Length == 0 )
{
throw new ApplicationException( "Connection string was missing or empty." );
}
string cmdText = "SELECT * FROM SYSOBJECTS WHERE TYPE = 'U' AND OBJECTPROPERTY(id,'IsMSShipped') = 0 ORDER BY NAME";
DataSet ds = new DataSet("dsUserTables");
using( SqlConnection myConnection = new SqlConnection( myConnectionString ) )
using( SqlDataAdapter myAdapter = new SqlDataAdapter( cmdText, myConnection ))
{
myAdapter.Fill( ds );
}
return ds;
}
}
}
and here's the entire web config i'm using:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="database_connection_string" value="Persist Security Info=False;Integrated Security=SSPI;database=Pubs;server=DeathAngel"/>
</appSettings>
</configuration>
...but note, i was able to get your example to work as you posted, with no modifications. The site i deployed the webservice is a virtual directory under the localhost site. the localhost has a web.config as does the virtual directory where the web services lives. I am running Windows 2003 Server which may or may not make a difference, I run WindowsXP at work and have noticed some differences. I use a fileshare and not frontpage extensions to access the project from visual studio. Um...I can't think of anything else that could be wrong with your set up though. Can you read anything from your config files(i.e. not through a webservice but just a regular web page)?

Mike Hildner
Jun 26th, 2004, 03:36 PM
Wait, hold on a second. That's the ENTIRE web.config file you're using? I took the default one and added the appSettings element. Should I have a stand-alone web.config? Throw away the default stuff?

pvb
Jun 26th, 2004, 04:04 PM
Shouldn't matter. I did it first with the config file you posted, but changed up the connection string so it worked and that still worked. Figured I'd remove all of the other settings to remove as many variables from the equation as possible. It should work fine with those few lines, give it a try.

Mike Hildner
Jun 26th, 2004, 04:05 PM
Tom Clancy fan, eh? I tried your code in place of mine, no joy. I'm going to do a quick (?) rewrite to try this again. I really don't know if this is an issue, but I turned on Visual Source Safe for this project. I did the same for a Compact Framework project I have and VSS deploys the files as read-only. Screwed the app up.

I'll try something clean - that is, no VSS and see if there is a difference.

pvb, I appreciate your time. Will post what I find, might take me a couple days before I get to it - can't code all weekend, you know.

Time for a beer.

Thanks,
Mike

Mike Hildner
Jun 28th, 2004, 09:18 AM
I finally figured out what was going on, although I'm not so sure how to fix it (nothing to do with VSS BTW).

Problem is, I have a solution that right now, has two projects - a web service and a windows forms app. I thought it would be smart to group related projects in a solution, but maybe not.

Apparently, the web service project is not looking at it's own web.config when the ConfigurationSettings.AppSettings line is executed. When I add a App.Config to the Windows form project and put the <appSettings> in there, things work fine.

Is this by design? Is there a way to get the web service to look at it's own web.config and the GUI app to look at it's own App.config? Maybe I should only have one project in each solution? My original thought was to have several projects in this solutions, because they are all related.

I'm not really sure how all this stuff should work, can anyone explain?

Thanks,
Mike

pvb
Jun 28th, 2004, 09:38 AM
only thing i can think of is that in this one instance, is that since your UI is the windows startup project that only one config file supporting that process would be loaded(that is the main process running is the UI and not the web service); so maybe in that instance you'll want to run the web service on it's own. Really, both of those processes(the UI part and web services part) are main processes, the web service is(can be) a standalone process and the UI as well. Not sure if that's an ideal scenario for grouping them together in one solution where only one can be the start up project. This is different than having a data layer project, business layer project, etc supporting the main app, and having all of those gouped into one solution. I'm a little low on caffeine this morning and might not be making sense yet but maybe there was a nugget of help in the above.

Mike Hildner
Jun 28th, 2004, 10:26 AM
Putting the web service in it's own solution solves the problem. Doesn't seem to make a difference which one is the startup project.

I don't quite understand all that, to me it makes sense to have in one solution, but whatever, at least it's working and I know why it wasn't working.

Thanks for your time, pvb, looks like I owe :)

Mike