<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>C#</title>
        <link>http://geekswithblogs.net/PsychoCoder/category/6862.aspx</link>
        <description>Posts about working with C#</description>
        <language>en-US</language>
        <copyright>Richard McCutchen</copyright>
        <managingEditor>psychocoder@dreamincide.net</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Using WMI with C#</title>
            <link>http://geekswithblogs.net/PsychoCoder/archive/2008/01/25/using_wmi_in_csharp.aspx</link>
            <description>&lt;p&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;Recently I discovered the WMI (&lt;span id="header1"&gt;&lt;span id="Article1"&gt;&lt;span class="serif"&gt;&lt;span class="heading"&gt;Windows Management Instrumentation) and was stunned at what it can do. Granted, the discovery was completely by accident, but I found it none the less. The WMI can mistify a lot of developers, especially new ones, so granted I was a little nervous and leery when I dove further into it, but what I found is that it can be an invaluable tool.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;span class="serif"&gt;&lt;span class="heading"&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;I jumped into learning the System.Management Namespace (which is where the WMI resides by the way) and discovered that WMI can save a developer tons of research time, clock cycles (as it is faster than other methods) and hair (from pulling it out looking for a solution). &lt;font class="smallblack"&gt; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;span class="serif"&gt;&lt;span class="heading"&gt;&lt;font size="2"&gt;&lt;font class="smallblack"&gt;When developing Windows applications, developers often need information a system, either local or remote, that although commonplace, can be very tough to get. There is using the remote registry, but I myself do not allow remote registry access as do many network admins. WMI is usually wide open on networks, assuming you have the privelidges necessary to query it, just as it is with remote registry querying/editing.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;span class="serif"&gt;&lt;span class="heading"&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;There is another reason WMI appeals to so many developers nowadays, its called WQL (Windows Query Language). WQL allows us a developers to query &lt;font class="smallblack"&gt;WMI providers using a SQL-like query language. If you know the provider classes and the fields available, then you can get the info very easily. Lets say, for instance, that you wanted to see the capacity, used space and free space available on a CD-ROM, you WQL query would look like:&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;em&gt;&lt;strong&gt;"select * from Win32_LogicalDisk WHERE DriveType = 5"&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt; &lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;You can then take that query, plug it into a ManagementObjectSearcher (a member of the System.Management Namespace), then use that to retrieve that particular information. In my example I populate the returned data into a Generic Dictionary&amp;lt;string,string&amp;gt;  list, you may chose to do otherwise. But to retrieve the information I spoke about earlier, here is how you would find that information about the CD-Row:&lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt; &lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;  &lt;/p&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;public Dictionary&amp;lt;string, string&amp;gt; GetDriveInfo()&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;{&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;   &lt;font color="#99cc00"&gt; &lt;/font&gt;&lt;font color="#339966"&gt;//dictionary object to hold the values&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;    Dictionary&amp;lt;string, string&amp;gt; driveInfo = new Dictionary&amp;lt;string, string&amp;gt;();&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;   &lt;font color="#339966"&gt; //create our WMI searcher&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;    ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"select * from Win32_LogicalDisk WHERE DriveType = 5");&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;   &lt;font color="#339966"&gt; //now loop through all the item found with the query&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;    foreach (ManagementObject obj in searcher.Get())&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;    {&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;       &lt;font color="#339966"&gt; //create the used space by subtracting the size from the free space&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;        double used = (double)obj["Size"] - (double)obj["FreeSpace"];&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;      &lt;font color="#339966"&gt;  //add all the items to the collection&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;        driveInfo.Add("FreeSpace", obj["FreeSpace"].ToString());&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;        driveInfo.Add("Capacity", obj["Capacity"].ToString());&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;        driveInfo.Add("UsedSpace", used.ToString());&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;    }&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;   &lt;font color="#339966"&gt; //return the info&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;    return driveInfo;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;}&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;Notice how simple that was, and how &lt;em&gt;ordinary&lt;/em&gt; it looks, it almost looks like you're querying a Database, but in fact you're querying a computer, either the system the application is running on, or a remote machine you have access to.&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;&lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/aa394173.aspx"&gt;MSDN&lt;/a&gt; has a great writeup on WQL, what the DriveType values are you see me using in my queries, so dont let this scare you off, in the long run it will be quite the time saver when developing applications. Lets say you need to know how much free space is available on a network drive, before WMI and WQL you would have to rely on trying a remote registry query (testy at best, and it has to be open on the machine), but with WMI its a simple WQL query then a search.&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;The WQL Query for retrieving all network drives is:&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt; 
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;strong&gt;&lt;em&gt;"select name, FreeSpace from win32_logicaldisk where drivetype=4"&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Notice I altered this query, unlike the first one I showed, in this one I only want to return the name of the network drive and the freespace it contains, I seen no need to return all the information if all I really wanted was 2 pieces. That would be the equivilent of "SELECT * FROM Table_Name" when all you needed was the record ID's. Once again we will plug this query into a ManagementObjectSearcher, this time using a &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.management.selectquery.aspx"&gt;SelectQuery&lt;/a&gt;, another member of WQL and the System.Management Namespace:&lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt; &lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;In this example I chose to populate a HashTable with the data returned, you, as with before, may chose to do it in a different fashion:&lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt; &lt;/p&gt;
&lt;p style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;  &lt;/p&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt; &lt;font size="2"&gt;public Hashtable ReadFreeSpaceOnNetworkDrives()&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;        {&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;            &lt;font color="#339966"&gt;//create Hashtable instance to hold our info&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;            Hashtable driveInfo = new Hashtable();&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;         &lt;font color="#339966"&gt;   //query the win32_logicaldisk for type 4 (Network drive)&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;            SelectQuery query = new SelectQuery("select name, FreeSpace from win32_logicaldisk where drivetype=4");&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;           &lt;font color="#339966"&gt; //execute the query using WMI&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;           &lt;font color="#339966"&gt; //loop through each drive found&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;            foreach (ManagementObject drive in searcher.Get())&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;            {&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;               &lt;font color="#339966"&gt; //add the name &amp;amp; freespace to our hashtable&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;                driveInfo.Add("Drive", drive["name"]);&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;                driveInfo.Add("Space", drive["FreeSpace"]);&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;            }&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;            return driveInfo;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;        }&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div style="FONT-SIZE: 11pt; MARGIN: 0in"&gt;&lt;font size="2"&gt;So, as you can see the WMI can be an invaluable tool to learn, and in my opinion its worth the time to learn. WMI can save you time, it definately, as shown in the 2 methods, much simpler than trying a remote registry query, and I think as more and more developers discover and learn how to use WMI it will definately grow.&lt;/font&gt;&lt;/div&gt;
&lt;/font&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=118904"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=118904" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/PsychoCoder/aggbug/118904.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Richard McCutchen</dc:creator>
            <guid>http://geekswithblogs.net/PsychoCoder/archive/2008/01/25/using_wmi_in_csharp.aspx</guid>
            <pubDate>Fri, 25 Jan 2008 15:10:39 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/PsychoCoder/comments/118904.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/PsychoCoder/archive/2008/01/25/using_wmi_in_csharp.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/PsychoCoder/comments/commentRss/118904.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/PsychoCoder/services/trackbacks/118904.aspx</trackback:ping>
        </item>
        <item>
            <title>Introduction to Web Services in C#</title>
            <link>http://geekswithblogs.net/PsychoCoder/archive/2007/09/30/intro_to_web_services.aspx</link>
            <description>&lt;p&gt;I posted a tutorial on my favorite programming forum &lt;a target="_blank" href="http://dreamincode.net"&gt;&amp;lt;/dream.in.code&amp;gt;&lt;/a&gt;, and thought Id go ahead and share it on my blog as well. One question I get all the time in programming communities, always by young, new programmers, is how to work with web Services in .Net. It was these questions that lead me to writing the tutorial I posted on Dream.In.Code. &lt;/p&gt;
&lt;p&gt;I guess before you can show someone how to create and consume a Web Service, you need to ensure they know and understand what a Web Service actually is. The following is the easiest, simplest definition I've been able to come up for "What is a web service":&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;What is a Web Service you say? Well a Web Service is very general model for building applications that can be implemented for any operating system that supports communication over the web. To some this may sound a lot like a Web Site, but that is not the case, there are many differences between the two.&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Here are the main differences between a Web Service and a Web Site:&lt;/font&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;font face="Arial"&gt;Web Site has an interface - Web Service has no interface&lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font face="Arial"&gt;Web Site is designed to interact with people - Web Service is designed to interact with other applications&lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font face="Arial"&gt;Web Site is designed to work with web browser clients - Web Service is designed to work with any type of client or device&lt;/font&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So as you can see, a Web Service and a Web Application have almost the same role, they just go about fulfilling that role in vastly different ways. A Web Service uses &lt;a target="_blank" href="http://www.w3.org/Protocols/rfc2616/rfc2616.html"&gt;HTTP (Hypertext Transfer Protocol)&lt;/a&gt; and &lt;a target="_blank" href="http://www.w3.org/Protocols/rfc2616/rfc2616.html"&gt;SOAP (Simple Object Access Protocol)&lt;/a&gt; to transfer data to and from the clients. Data sent to and from the Web Service is rendered into XML so the Web Service can read it, then it is sent back to the slient, which then renders the returned XML into the format it was expecting.&lt;/p&gt;
&lt;p&gt;First, lets take a look at creating a simple Web Service. To do this, open Visual Studio, once it loads click &lt;strong&gt;File&lt;/strong&gt; &amp;gt; &lt;strong&gt;New Website&lt;/strong&gt;, when the &lt;strong&gt;New Project Dialog&lt;/strong&gt; opens, select Web Service from the list of available projects, then for &lt;strong&gt;Location &lt;/strong&gt;select either HTTP or File System (your own IIS), and lastly for &lt;strong&gt;Language&lt;/strong&gt; select C#.&lt;/p&gt;
&lt;p&gt;When the project is created it creates a Service.amsx and Sercive.cs file, you can either rename these, or delete and add new ones with the name of SampleService asmx and SampleService.cs.Now double-click on SampleService.cs to open the code file, then make sure you have the following references at the top of your class:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;using System;&lt;br /&gt;
using System.Web;&lt;br /&gt;
using System.Web.Services;&lt;br /&gt;
using System.Web.Services.Protocols;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Configuration;&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;If all the references aren't there, add the ones that are missing. Next, you will notice the first 2 lines are out of the ordinary, if you've never created a Web Service these two lines are downright weird:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;[WebService(Namespace = "http://tempuri.org/")]&lt;br /&gt;
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Heres whats going on here, line 1 declares the Web Service's &lt;strong&gt;Namespace,&lt;/strong&gt; and you might now be wondering &lt;em&gt;Well whats a namespace? &lt;/em&gt;Well luckily theres an answer to that: &lt;a target="_blank" href="http://tempuri.org"&gt;Tempuri.Org&lt;/a&gt; defines a Web Service Namespace as:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;Each XML Web Service needs a unique namespace in order for client applications to distinguish it from other services on the Web.  By default, ASP.Net Web Services use http://tempuri.org/ for this purpose.  While this suitable for XML Web Services under development, published services should use a unique, permanent namespace.&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;Your XML Web Service should be identified by a namespace that you control.  For example, you can use your company's Internet domain name as part of the namespace.  Although many namespaces look like URLs, they need not point to actual resources on the Web.&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The next line of the first section of the Web Service uses the &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.web.services.webservicebindingattribute.aspx"&gt;WebServiceBinding Method&lt;/a&gt; to create the Web Service's version of an &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/87d83y5b(VS.71).aspx"&gt;Interface&lt;/a&gt;. After those 2 lines then you have your Initializer, which is, of course, the same as any other class. Here you can instantiate any objects you need used in each instance of the service:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;public SampleService () &lt;br /&gt;
{&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;    //Uncomment the following line if using designed components &lt;br /&gt;
    //InitializeComponent(); &lt;br /&gt;
}&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Nothing new there, now we get to jump into the heart of a Web Service, which is, of course, to make it do something, to perform some service. When you create your methods, if you want them to be visible from the client thats consuming the service, you have to add the &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/byxd99hx(VS.80).aspx"&gt;[WebMethod()] Attribute&lt;/a&gt; to it. This tells the service that its ok to expose that method to the client which is consuming it.&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Since this is just a simple, basic Web Service it only has a single method, just to show you what can be done with a Web Service. Since this service is communicating with a Sql Database, it needs access to the &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.data.sqlclient(VS.71).aspx"&gt;System.Data.SqlClient Namespace&lt;/a&gt;. There are other Namespaces the service will use, we listed them above, but lets take a closer look at them:&lt;/font&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;System Namespace&lt;/strong&gt; - Base class for all Namespace's&lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.web.aspx"&gt;System.Web&lt;/a&gt; - Provides classes and interfaces required for server/browser communication&lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/System.Web.Services.aspx"&gt;System.Web.Services&lt;/a&gt; - Provides all the functionality required to create a Web Service&lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/System.Web.Services.protocols.aspx"&gt;System.Web.Services.Protocols&lt;/a&gt; - Provides all required protocols the Web Service needs to transfering data&lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/System.Configuration.aspx"&gt;System.Configuration&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Since the is an example for a beginner with Web Services, our sample service has but a single method in it, &lt;strong&gt;GetAllUserInfo&lt;/strong&gt;. This method is designed to:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Query a SQL Server&lt;/li&gt;
    &lt;li&gt;Retrieve all the information for all users in the system&lt;/li&gt;
    &lt;li&gt;Populate a &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.data.dataset.aspx"&gt;DataSet&lt;/a&gt; using a &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx"&gt;SqlDataAdapter&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;Pass the DataSet back to the client.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now lets take a look at the code for this method:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;
/// Here we're going to create a [WebMethod] that&lt;br /&gt;
/// can be called from an aspx page, then return&lt;br /&gt;
/// a populated DataSet to the calling aspx page&lt;br /&gt;
/// &amp;lt;/summary&amp;gt;&lt;br /&gt;
/// &amp;lt;returns&amp;gt;A dataset popululated with the Members informaiopn&amp;lt;/returns&amp;gt;&lt;br /&gt;
[WebMethod]&lt;br /&gt;
public DataSet GetAllUserInfo() &lt;br /&gt;
{&lt;br /&gt;
    //Build the SqlCilent Objects we need&lt;br /&gt;
    SqlConnection conn = new SqlConnection(Utlilties.GetConnectionString("WebService_Conn"));&lt;br /&gt;
    SqlCommand cmd = new SqlCommand();&lt;br /&gt;
    ///SqlDataAdapter to populate our DataSet&lt;br /&gt;
    SqlDataAdapter adapter = new SqlDataAdapter();&lt;br /&gt;
    ///DataSet to hold the users information&lt;br /&gt;
    DataSet dsInfo = new DataSet();&lt;br /&gt;
    ///String to hold our stored procedure&lt;br /&gt;
    string query = "RetrieveUserInfo";&lt;br /&gt;
    ///try...catch block to handle any unhandeled exceptions&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
        //set our SqlCommands Objects&lt;br /&gt;
        cmd.CommandText = query;&lt;br /&gt;
        cmd.CommandType = CommandType.StoredProcedure;  //tell it its a Stored Procedure we're executing&lt;br /&gt;
        //if using inline SQL change the CommandType to Text&lt;br /&gt;
        //cmd.CommandType = CommandType.Text;&lt;br /&gt;
        //set its connection property&lt;br /&gt;
        cmd.Connection = conn;&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;        //now handle the connection state &lt;br /&gt;
        ///of our SqlConnection Object&lt;br /&gt;
       /// Utlilties.HandleConnectionState(conn);&lt;br /&gt;
        //set the SelectCommand Property&lt;br /&gt;
        ///of our SqlDataAdapter Object&lt;br /&gt;
        adapter.SelectCommand = cmd;&lt;br /&gt;
        //now we fill our DataSet&lt;br /&gt;
        ///using the Fill Method of&lt;br /&gt;
        ///the SqlDataAdapter&lt;br /&gt;
        adapter.Fill(dsInfo, "Members");&lt;br /&gt;
        //return the DataSet to the calling aspx page&lt;br /&gt;
        return dsInfo;&lt;br /&gt;
    }&lt;br /&gt;
    catch (Exception ex)&lt;br /&gt;
    {&lt;br /&gt;
        System.Web.HttpContext.Current.Response.Write(ex.Message);&lt;br /&gt;
        return null;&lt;br /&gt;
    }&lt;br /&gt;
    finally&lt;br /&gt;
    {&lt;br /&gt;
        ///put everyting in the finally section&lt;br /&gt;
        ///of the try...catch block you want executed&lt;br /&gt;
        ///whether theres an exception or not, in this&lt;br /&gt;
        ///case we want to close the connection no&lt;br /&gt;
        ///matter what happens&lt;br /&gt;
        ///close the connection&lt;br /&gt;
      /// Utlilties.HandleConnectionState(conn);&lt;br /&gt;
    }      &lt;br /&gt;
}&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This operates as explained. It opens a connection to our database, retrieves all the user information from the users table, then populates a Dataset ans returns the DataSet to the client. Notice the &lt;strong&gt;[WebMethod()]&lt;/strong&gt; attribute at the start of the method, this says that any client that consumes this Web Service will have access to that method, and the data it returns.&lt;/p&gt;
&lt;p&gt;Believe it or not, our Web Service is complete. Now with a Web Service in a production environment will have much more &lt;em&gt;meat on its bones&lt;/em&gt;, but for our demonstration, this is really all we need to show how a Web Service works. Next question people ask is "&lt;em&gt;Ok, we have this service out there on our server, how do we access it and use it?". &lt;/em&gt;Well thats a good question, to use the Web Service we just created you need a consumer, a client that will connect to the service and gather the data.&lt;/p&gt;
&lt;p&gt;In this example we will create an ASP.Net Web Application that will consume our Web Service, then display the returned data in a &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx"&gt;GridView Control&lt;/a&gt;. So, in your Solution Explorer window, right-click on the very top node, the solution name, then select &lt;strong&gt;Add&lt;/strong&gt; &amp;gt; &lt;strong&gt;New Project.&lt;/strong&gt; Once the &lt;strong&gt;New Project Dialog&lt;/strong&gt; opens, you will this time select &lt;strong&gt;ASP.Net Web Site &lt;/strong&gt;as the project type, once again either HTTP or File System for the &lt;strong&gt;Location &lt;/strong&gt;and C# for the &lt;strong&gt;Language.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;When the project is created is automatically adds a Default.aspx, and Default.aspx.cs. It's the CS file we will be doing the programming in. You always want to keep your presentation seperate from your logic, thats why Microsoft offers both files for a single page in .Net.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Since this is just a sample, open Default.aspx in &lt;strong&gt;Design Mode&lt;/strong&gt;, add a table to the page, then a &lt;a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx]"&gt;GridView&lt;/a&gt; to the page. The &lt;strong&gt;GridView&lt;/strong&gt; is what we'll use to display the data returned from our Web Service. You are now finished designing our ASPX page for consuming our Web Service. Now open &lt;strong&gt;Default.aspx.cs&lt;/strong&gt;, and make sure you have the following references at the top of your class:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;using System;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Configuration;&lt;br /&gt;
using System.Collections;&lt;br /&gt;
using System.Web;&lt;br /&gt;
using System.Web.Security;&lt;br /&gt;
using System.Web.UI;&lt;br /&gt;
using System.Web.UI.WebControls;&lt;br /&gt;
using System.Web.UI.WebControls.WebParts;&lt;br /&gt;
using System.Web.UI.HtmlControls;&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Those are the only references you will need for this example. Now we need to add a reference to the Web Service we just created, to do this:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Right-click on the project name&lt;/li&gt;
    &lt;li&gt;Select &lt;strong&gt;Add Web Reference&lt;/strong&gt;&lt;/li&gt;
    &lt;li&gt;Once the dialog box opens select &lt;strong&gt;Web Services in this solution&lt;/strong&gt;&lt;/li&gt;
    &lt;li&gt;Once it finds your service, click on its name&lt;/li&gt;
    &lt;li&gt;Wait for it to connect to the Web Service (when this happens the TextBox in the right-hand side of the dialog will be populated with the word &lt;strong&gt;localhost&lt;/strong&gt;&lt;/li&gt;
    &lt;li&gt;Click the &lt;strong&gt;Add Reference&lt;/strong&gt; button&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;NOTE: &lt;/strong&gt;Before clicking the Add Reference button I renamed my from &lt;strong&gt;localhost&lt;/strong&gt; to &lt;strong&gt;Sample&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now we have a reference to our Web Service and can starting using it. In our ASPX page we have a single method, &lt;strong&gt;BindGrid(),&lt;/strong&gt; and this method will:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Create a referene to our Web Service&lt;/li&gt;
    &lt;li&gt;Create a new &lt;strong&gt;DataSet&lt;/strong&gt;&lt;/li&gt;
    &lt;li&gt;Set our DataSet to the &lt;strong&gt;GetAllUserInfo&lt;/strong&gt; method in our Web Service (remember, it returns a populated &lt;strong&gt;DataSet&lt;/strong&gt;)&lt;/li&gt;
    &lt;li&gt;Bind the returned &lt;strong&gt;DataSet&lt;/strong&gt; to our &lt;strong&gt;GridView&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The code for this method is:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;private void BindGrid()&lt;br /&gt;
{&lt;br /&gt;
     //create our DataSet to hold the member information returned&lt;br /&gt;
     DataSet dsMembers = new DataSet();&lt;br /&gt;
     //create our WebService object&lt;br /&gt;
     WebServiceConsumer.Sample.SampleService sample = new WebServiceConsumer.Sample.SampleService();&lt;br /&gt;
     //set our DataSet to the GetAllUsers Method of our web service&lt;br /&gt;
     dsMembers = sample.GetAllUserInfo();&lt;br /&gt;
     //now we need to set some properties for our data grid&lt;br /&gt;
     gvMembers.AutoGenerateColumns = true;&lt;br /&gt;
     gvMembers.DataSource = dsMembers.Tables["Members"].DefaultView;&lt;br /&gt;
     gvMembers.DataMember = "UserID";&lt;br /&gt;
     gvMembers.DataBind();              &lt;br /&gt;
}&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Simple enough isnt it? Now to use this, we will call this method from our &lt;strong&gt;Page_Load Event&lt;/strong&gt; in our page, and that looks like this:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
     BindGrid();&lt;br /&gt;
}&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Believe it or not, thats it, thats all the code required to consume our Web Service and sisplay the returned data. The one difference in our BindGrid method is this: &lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;em&gt;WebServiceConsumer.Sample.SampleService sample = new WebServiceConsumer.Sample.SampleService();&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;That is where we create the reference to our Web Service, then we can use &lt;em&gt;sample.WhateverMethodIsThere &lt;/em&gt;for utilizing the methods in the Web Service, instead of typing the long name out.&lt;/p&gt;
&lt;p&gt;I am also providing a links to the &lt;a target="_blank" href="http://psychocoder.net/BlogItems/SampleWebService.zip"&gt;Source Code&lt;/a&gt; for this example, all I ask is that you keep the header intact.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Thank you for reading, and I hope you found this helpful &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=115721"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=115721" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/PsychoCoder/aggbug/115721.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Richard McCutchen</dc:creator>
            <guid>http://geekswithblogs.net/PsychoCoder/archive/2007/09/30/intro_to_web_services.aspx</guid>
            <pubDate>Sun, 30 Sep 2007 14:43:46 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/PsychoCoder/comments/115721.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/PsychoCoder/archive/2007/09/30/intro_to_web_services.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/PsychoCoder/comments/commentRss/115721.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/PsychoCoder/services/trackbacks/115721.aspx</trackback:ping>
        </item>
    </channel>
</rss>