Other Files in the Project

Before we run headlong into the remaining examples, let's take a moment and see what other files got included in the project and where they sit on your hard drive. Looking at the Solution Explorer pane in Figure 1.3, you see the following files in your project:

  • AssemblyInfo.vb Contains version information related to the Visual Basic project.

  • [Project Name].vsdisco Contains the dynamic discovery information for this Web Service. The ASP.NET runtime uses this to discover and list all the Web Services hosted on a particular machine.

  • Global.asax Use this class to add handlers called by the ASP Application and Session objects. Web Services must derive from System.Web.Services.WebService to get access to these objects.

  • Web.config Configuration information related to the Web application. You can use this file to specify database connection strings, security settings, globalization settings, and other application-specific items.

By the time you are done reading this book, you will see the importance of Web.config because we will use it frequently. .NET uses config files extensively. These files are meant to replace static Registry settings. As an added bonus, they allow you to specify data that influences program behavior in an easy-to-use format. You can experiment with security, change databases, or edit application-specific settings without rebuilding the program. For example, let's say that we wanted to change the default string that our HelloWorld returned. To do this, we would open up Web.config and add the following section as a child element of the configuration root element:

<appSettings>
    <add key="ReturnString" value="I love config files!" />
</appSettings>

You should know that the elements not in quotes must appear as is. The add element has two attributes—key and value. When accessing application settings, you access them by key to get the associated value. Each key must be unique. After this XML is entered into Web.config, modify HelloWorld to read the ReturnString value from the configuration file.

<WebMethod()> Public Function HelloWorld() As String
    HelloWorld = System.Configuration.ConfigurationSettings. _
        AppSettings.Get("ReturnString")
End Function

Now, when you invoke HelloWorld via the ASP.NET–generated interface, it returns the following:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">I love config files!</string>

And yes, if you change the value attribute while the program is running, the program will get the new data the next time it goes to look. In the Web.config file, you will spend more time modifying appSettings than doing anything else. The first time you go into Web.config, the appSettings element does not exist. After you add it, if you need additional key/value pairs stored, just add them to the one appSettings element. For example, to add another element, the appSettings section would expand as follows:

<appSettings>
    <add key="ReturnString" value="I love config files!" />
    <add key="AnotherKey" value="Another Value" />
</appSettings>

Let's continue the example by having it handle a class object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.136