|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article is intended as a brief introduction to some of the more basic elements comprising .NET development. I will create and describe five simple applications for .NET, using nothing more than the .NET Framework SDK and a text editor. I will be playing off of the ever popular, "hello, world" theme. The language that I will be using in the article is C#. I have written a number of articles on .NET programming recently, and a common feedback that I have received is that .NET is overly complicated to develop in. That it is much easier to code with language X and Notepad. On the face of it, this might seem like a valid criticism given Visual Studio's size and complexity, but it is not the language or even the framework that is to blame. Microsoft's C#.NET and Sun's Java are remarkably similar in their semantics and high level design - generally what the developer is acquainted with, as opposed to the language purists, who can tell you the difference between the CLR and JVM. The pain (and pleasure) of the development process that is most difficult and different with .NET, is the IDE, Visual Studio. Powerful? Definitely, but the ramp is steep and even simple things drag along a considerable amount of unnecessary cruft when the IDE is used. I decided to strike back against the complex and visually breathtaking IDE based development using only "primitive" tools - a command-line and text editor. Kernighan and Ritchie's ubiquitous "The C Programming Language" is considered by many to be the most influential programming book of our time. Written in 1978, it immortalized the now well known phrase - "hello, world". The book states that "hello, world" is the basic hurdle to learning all languages:
You need to have some perspective to truly appreciate this statement or you might be tempted to consider it overreaching. Dennis Ritchie designed the C programming language, and it was the C language that made portability possible for the Unix operating system. Unix and Linux, Minix, BSD, OSX, YouNameItOS and even Windows owe a great debt of gratitude to the language. Even the language C# is a derivative of Ritchie's C. So when in his book, it says that "hello, world" is important, we should listen. So it is that I have taken my inspiration from K&R's work. I can think of no better method to expose the foundations of the development environment even in the year 2004 than the old, "hello, world" approach. This article is divided into five "hello, world" tutorials covering command-line development using .NET:
BackgroundA basic understanding of .NET, the command-line, and working in Windows is assumed. Here are some good resources to help you get up to speed on these topics: OK, I will admit that when I first sat down to write this section, I was intending to simply state the requirements and leave it to the reader as an exercise to actually do the getting and installation. However, when I sat down at my wife's computer and tested out the process, it quickly became apparent that more was required. There are basically four required pieces to doing .NET development using the command-line:
You should now be set up to develop with the command-line. It is time to begin the tutorials. HelloConsoleThis tutorial is the easiest of the five. When you have finished it, you will have the pleasure of seeing the phrase "hello, world", printed on the command-line. OK, let's get started. What are the goals? Borrowing directly from Kernighan and Ritchie, these will be our goals for all of the tutorials:
Create the program textIn order to create the program text, you will need to use a text editor of some sort. I prefer using UltraEdit, although Textpad or Notepad will work as well. Do not even bother with Word or Wordpad, these are arguably word processors and they tend to do funny things to text files. Fire up the editor and you will be ready to begin. Here is K&R's "hello, world" C program in all its splendor, it is the model I am replicating, so I feel it is fitting to include it here: //reader Ryan Beesley noticed that the original quote
//lacked an include needed to compile
//so I added the following include - it is not part of the
//original K&R hello, world program
#include "stdio.h"
main()
{
printf("hello, world\n");
}
The C#.NET code that is required to print "hello, world" to the console is similarly simple: 1 using System;
2
3 namespace mynamespace {
4 public class HelloWorld {
5 public static void Main(string [] args) {
6 Console.WriteLine("hello, world");
7 }
8 }
9 }
Save the file to your hard drive as HelloConsole.cs and open a command prompt. Compile the programCompile the application using the C# compiler: csc HelloConsole.cs
This should produce output similar to the following, without error. Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
Compiling the application will generate an executable named HelloConsole.exe. Load and run the programLoading and running the program are accomplished in a single step. Load and run the application from the command-line by typing: HelloConsole.exe
Locate the OutputConsole output is incredibly easy to find. Generally, it appears on the line immediately following the command itself. Running the program should produce the following output: hello, world
Congratulations, you have completed the HelloConsole tutorial. Now, I know what some of you are thinking, "It took nine lines to produce the same output K&R did in four? Ha! Gotcha, I knew .NET was lame." Well, it is not that simple. Here is a minimalist C#.NET HelloWorld console example for the purist: 1 class HelloWorld {
2 public static void Main(string [] args) {
3 System.Console.WriteLine("hello, world");
4 }
5 }
It is still bigger than the K&R version, but not by much. HelloFileHelloFile is going to be a near duplicate of HelloConsole with the addition of the ability to write to a file. When you have finished this tutorial, you will have a file named Hello.txt in the same directory as the program, that has the phrase, "hello, world" as the contents. Create the program textHere is the code for the application: 1 using System;
2 using System.IO;
3
4 namespace mynamespace {
5 public class HelloWorld {
6 public static void Main(string [] args) {
7 FileInfo fi = new FileInfo("Hello.txt");
8 StreamWriter sw = fi.CreateText();
9 sw.WriteLine("hello, world");
10 sw.Close();
11 }
12 }
13 }
Save the file to your hard drive as HelloFile.cs and open a command prompt. Compile the programCompile the application using the C# compiler: csc HelloFile.cs
Compiling the application will generate an executable named HelloFile.exe. Load and run the programHelloFile.exe
There will not be any discernable output on the console, remember that we are directing the output to the file Hello.txt. Locate the OutputWhere did the output go? We told the program to send its output to the file Hello.txt. The file should be located in the same directory that the HelloFile.exe file was run from. The easiest way to view the file's contents is to use the type command: type Hello.txt
You should see the following output: hello, world
That is all there is to HelloFile. HelloBrowserWith this tutorial, we begin to move into the 21st century. At the end of the tutorial, you will have a browse-able web application that is running on a web server that, when called, will return HTML with the phrase, "hello, world", as the contents. Create the program textHelloBrowser is different than the console applications in that the program will run in the context of the local web server and its output will be sent to a browser as HTML. I will be using code-behind. Code-behind is new to .NET programming, and for our purposes, refers to the fact that algorithms and program logic reside in a separate file from the HTML that comprises the graphical user elements. I will not belabor it, but code-behind is the only way to go, mixing HTML and code is a nightmare waiting to prey on the weak minded. What this means is that there will be two files, one for HTML and one for C#.NET code. Here is the HTML file, called HelloBrowser.aspx. 1 <%@ Page language="c#" Codebehind="HelloBrowser.aspx.cs"
AutoEventWireup="false" Inherits="mynamespace.HelloWorld" %>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3 <HTML>
4 <body>
5 <form id="Form1" method="post" runat="server">
6 <DIV id="divHelloWorld" runat="server"></DIV>
7 </form>
8 </body>
9 </HTML>
Save the file to disk as HelloBrowser.aspx. Here is the C#.NET file, called HelloBrowser.aspx.cs. 1 using System;
2 using System.Web.UI.HtmlControls;
3
4 namespace mynamespace
5 {
6 public class HelloWorld : System.Web.UI.Page
7 {
8 protected HtmlGenericControl divHelloWorld;
9
10 private void Page_Load(object sender, System.EventArgs e)
11 {
12 divHelloWorld.InnerText = "hello, world";
13 }
14
15 override protected void OnInit(EventArgs e)
16 {
17 this.Load += new System.EventHandler(this.Page_Load);
18 }
19 }
20 }
using directive for the System.Web.UI.HtmlControls namespace. If we did not include it, it would require that we type System.Web.UI.HtmlControls.HtmlGenericControl to instantiate the DIV tag in our code.
HelloWorld class declaration that derives from the System.Web.UI.Page class that .NET provides. The Page class is pre wired with events related to web server operation. We are going to make use of this functionality when we tap into the Page_Load() event to set the text of the DIV tag.
System.Web.UI.HtmlControls.HtmlGenericControl object for our DIV tag (declared in the HTML, line 6 previously).
System.Web.UI.Page Page_Load() event. It is where we will set the text of the DIV tag.
InnerText property of the DIV tag to "hello, world".
OnInit method is overridden to give us a place to add our event handler to the HelloWorld Page_Load() event.
EventHandler Page_Load() to HelloWorld.Load().
Save the file to disk as HelloBrowser.aspx.cs. Compile the programThis is where things get interesting. The .aspx file will be compiled at run time by the web server. The .aspx.cs file will need to be compiled into a .dll. First, though, let's prepare for the files. This is a web application after all - it needs a little more in the way of deployment than a simple console application. Create a directory to contain the .aspx file: md HelloBrowser
Copy HelloBrowser.aspx to the HelloBrowser directory. copy HelloBrowser.aspx HelloBrowser
Create a directory to contain the .aspx.cs assembly .dll file: md HelloBrowser\bin
Compile the .aspx.cs file. Open a command prompt and type: csc /t:library /out:Hellobrowser\bin\HelloBrowser.dll HelloBrowser.aspx.cs
You should see something like the following: Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
This should create a file HelloBrowser.dll in the HelloWorld\bin directory. Create a virtual directory in the web server.
Load and run the programThese steps will be managed by the web server when the page is requested. To test, type the address below into your browser's address bar: http://localhost/HelloB/HelloBrowser.aspx
Locate the OutputThe output will be in the browser's display area. I have chosen to use the Mozilla Browser 1.6 available here as the browser for this article. Mozilla is a much more advanced browser than Microsoft's Internet Explorer and it can be a developer's best friend. However, this is one big caveat - some sites on the web are not Mozilla friendly and you will definitely need to keep IE around. It is also a good idea to use IE to develop UI elements for web pages - it is the most popular browser. Feel free to use IE for the tutorials - I tested with IE and Mozilla. Here is the output:
That is it for HelloBrowser. HelloWebServiceThis is the most difficult of the tutorials, but when you are finished, you will be pleased to find that you have created an XML web service that exposes a Create the program textThe web service will be written using code-behind and will operate in the context of the web server. There will be two files, one for the Here is the 1 <%@ WebService Language="c#" Codebehind="HelloWebService.asmx.cs"
Class="mynamespace.HelloWebService" %>
Save the file to disk as HelloWebService.asmx. Here is the C# file, HelloWebService.asmx.cs, that defines the web service logic: 1 using System;
2 using System.Web.Services;
3
4 namespace mynamespace
5 {
6 [WebService(Namespace="http://sentech.com")]
7 public class HelloWebService : WebService
8 {
9 [WebMethod]
10 public string SayHelloWorld()
11 {
12 return "hello, world";
13 }
14 }
15 }
Save the file to disk as HelloWebService.asmx.cs. Compile the programThe .asmx file will be compiled at run time by the web server. The .asmx.cs file will need to be compiled into a .dll. First, though, let's prepare for the files. This is a web service - it needs more in the way of deployment. Create a directory to contain the .asmx file: md HelloWebService
Copy the HelloWebService.asmx file to the HelloWebService directory: copy HelloWebService.asmx HelloWebService
Create a directory to contain the .asmx.cs file: md HelloWebService\bin
Compile the .asmx.cs file. Open a command prompt and type: csc /t:library /out:HelloWebService\bin\HelloWebService.dll
HelloWebService.asmx.cs
You should see something like the following: Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
This should create a file HelloWebService.dll in the HelloWebService\bin directory. Create a virtual directory in the web server.
Load and run the programThese steps will be managed by the web server when the page is requested. To test it out, type the address below into your browser's Address bar: http://localhost/HelloWS/HelloWebService.asmx
Locate the OutputIn order to locate the output of a web service, you will need to perform a few steps. The browser page shows the web methods that are available to use.
You can choose one of two paths here. One is the web method
Next, go back and select the This brings you to a page that shows some sample SOAP code for those of you that want to delve deeper. There is also a button that says, Invoke, press the button.
This simulates a client of the web service requesting the
There is one additional page that we might be interested in, and that is the .disco file, or Discovery document of the web service. To display the .disco file, browse to: http://localhost/HelloWS/HelloWebService.asmx?DISCO
That is it for HelloWebService. HelloWebServiceClientWhat good would a web service be without a client application? None, so here is that application. It is a simple web application that calls the web service created in the previous tutorial and displays the phrase, "hello, world", returned by the web service in the browser window. Create the program textThis is another code-behind web application, but with a twist - we need to create a proxy. Web services use a pretty sophisticated communication mechanism. Thankfully, the .NET Framework provides the ability to auto-generate the proxy code and lets us concentrate on the productive stuff. First, some prep work - create a directory to contain the web service client web application: md HelloWebServiceClient
We need a directory for the code-behind DLL: md HelloWebServiceClient\bin
Create a virtual directory in the web server.
To generate the proxy code, open a command prompt, and from the same directory as the .aspx.cs file: wsdl /out:HelloWebService_proxy.cs
http://localhost/HelloWS/HelloWebService.asmx?WSDL
This should result in some semblance of the following output: Microsoft (R) web services Description Language Utility
[Microsoft (R) .NET Framework, Version 1.1.4322.573]
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
Writing file 'HelloWebService_proxy.cs'.
Here is the code for the .aspx web application file: 1 <%@ Page language="c#" Codebehind="HelloWebServiceClient.aspx.cs"
AutoEventWireup="false" Inherits="mynamespace.HelloWorld" %>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3 <HTML>
4 <body>
5 <form id="Form1" method="post" runat="server">
6 <DIV id="divHelloWorld" runat="server"></DIV>
7 </form>
8 </body>
9 </HTML>
Save the file to disk as HelloWebServiceClient.aspx. Here is the C# code-behind source: 1 using System;
2 using System.Web.UI.HtmlControls;
3
4 namespace mynamespace
5 {
6 public class HelloWorld : System.Web.UI.Page
7 {
8 protected HtmlGenericControl divHelloWorld;
9
10 private void Page_Load(object sender, System.EventArgs e)
11 {
12 HelloWebService hello = new HelloWebService();
13 divHelloWorld.InnerText = hello.SayHelloWorld();
14 }
15
16 override protected void OnInit(EventArgs e)
17 {
18 this.Load += new System.EventHandler(this.Page_Load);
19 }
20 }
21 }
This file is remarkably similar to the web application's code-behind.
Save the file to disk as HelloWebServiceClient.aspx.cs. Compile the programCompile the proxy: csc /t:library /out:HelloWebServiceClient\bin\HelloWebService_proxy.dll
HelloWebService_proxy.cs
The output should resemble: Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
Compile the code-behind: csc /t:library /r:HelloWebServiceClient\bin\HelloWebService_proxy.dll \
/out:HelloWebServiceClient\bin\HelloWebServiceClient.dll HelloWebServiceClient.aspx.cs
The output will resemble: Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
Copy the HelloWebServiceClient.aspx file to the HelloWebServiceClient directory: copy HelloWebServiceClient.aspx HelloWebServiceClient
Load and run the programThese steps will be managed by the web server when the page is requested. To test it out, type the address below into your browser's Address bar: http://localhost/HelloWSC/HelloWebServiceClient.aspx
Locate the OutputThe output will be in the browser window and will appear as it did in the HelloBrowser application.
ConclusionIf you have come this far, you are serious about your command-line .NET experience. My hope is that you have found this article interesting and useful. Please let me know what you think. Email the author: Will Senn. References
ResourcesBackground Resources
General Resources
HistoryVersion 1.0 - This is the first version of this article.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||