- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers
C#
CREATE A WEB SERVICE
he next evolution of distributed programming, Web TServices, allows for your applications to provide
component-based services over the Internet. That is, you can call a .NET component from one machine on the Internet to another. Web Services are made available through standards like Simple Object Access Protocol (SOAP), eXtensible Markup Language (XML), and HyperText Transport Protocol (HTTP). This mechanism allows for calls to be made over known communication ports, like port 80, the standard port for HTTP. For Microsoft, Web Services are considered the basic building blocks for distributed applications.
Because Microsoft has a SOAP Toolkit that allows remote procedure calls on COM+ components over HTTP, you do
not need .NET or VS .NET for building Web services, but having VS .NET and .NET makes life much easier when you are creating or using a Web Service.
VS .NET has a project type of ASP.NET Web Service to assist in creating Web services. Creating a Web service involves the use of a few new file types, which you may not find familiar, including the *.asmx and *.vsdisco. When you first start creating Web services, you need to be primarily concerned with the *.asmx file. The *.asmx file is where you will place your Web methods. The syntax is similar to how you create methods in classes. The only major difference is the use of the attribute WebMethod for methods that need to be exposed by the Web service and the class must be derived from system.Web.Services.WebService.
CREATE A WEB SERVICE
ASP.Net Web Service
› Type a name for the Web service.
ˇ Click to select http://localhost for the Location.
Á Click OK.
‡ Double-click Web service file created in the Solution Explorer.
° Scroll down to the
[WebMethod] and select the text for the sample Web service.
· Delete the comment lines.
‚ Right-click Web service file in the Solution Explorer.
— Click Build and Browse.
CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14
The Web service that you created in the sample task was a simple "Hello World" for a Web service. If you want to go a step further, you can test out the below sample Web method. You will need to create a wellformed XML document,
Favorites1.xml, in the same directory as the Web service file.
TYPE THIS:
using System.IO; [WebMethod]
public string GetFavoritesList(int UserID) { string sServerPath = Server.MapPath("");
// Here you could make a database call to get XML. string sFilePath = sServerPath+ "\\" + "Favorites1.xml"; string sList = GetXMLAsString(sFilePath);
return sList;
}
private string GetXMLAsString(string XMLDocumentPath) { FileStream fsFavorites = new FileStream
(XMLDocumentPath,FileMode.Open,FileAccess.Read);
StreamReader srFavorites = new StreamReader(fsFavorites); return srFavorites.ReadToEnd();
}
RESULT:
The xml string in favorites.xml is returned from the
WebMethod.
■ The
is loaded
± Click
C#
USING A WEB SERVICE
y using a Web service in your client application or Bserver application, you can utilize resources across the
Internet and open up new possibilities of truly distributed architectures.
When you build an ASP.NET Web Service, it automatically supports clients using the SOAP, HTTP-GET, and HTTP-POST protocols to invoke Web Service methods. HTTP-GET and HTTP-POST send information via named value pairs, but do not allow for complex data types to be passed. However, SOAP, or Simple Object Access Protocol, allows for more complex data types to be passed due to SOAP’s support of XML and XSD schemas.
Consuming a Web service is well supported in VS .NET, which includes a wizard-based approach to discovery and configuration of the proxy you need to make the SOAP call. The interface provided for discovering the Web service enables you to browse to the URL of the service. When you access the service, you can test the services from the interface, view WSDL (Web Services Description Language), contract, and view any documentation that exists. After you have a Web reference, you can import the namespace and then use it like a local component.
USING A WEB SERVICE
References
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
› Type a name. |
|
‡ In the Solution Explorer, |
° Click Add Web Reference. |
|||||||
|
|
|
|
|
||||||||||
|
|
|
|
|
ˇ Click to select a location |
right-click the class file. |
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
for the console application. |
|
|
|
|
|
|||||
|
|
|
|
Á Click OK. |
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14
Heavily investing into the future of Web Services, one of Microsoft’s current initiatives, HailStorm, addresses common programming needs such as personal profile information and contacts. Below is a list of some of the HailStorm Services.
|
HAILSTORM SERVICES |
|
|
|
|
|
|
|
myAddress |
Electronic and geographic address for an identity |
|
|
|
|
|
|
myProfile |
Name, nickname, special dates, picture |
|
|
|
|
|
|
myContacts |
Electronic relationships/address book |
|
|
|
|
|
|
myLocation |
Electronic and geographical location and rendezvous |
|
|
|
|
|
|
myNotifications |
Notification subscription, management, and routing |
|
|
|
|
|
|
myInbox |
E-mail and voice mail, including existing mail systems |
|
|
|
|
|
|
myCalendar |
Time and task management |
|
|
|
|
|
|
myDocuments |
Raw document storage |
|
|
|
|
|
|
myApplicationSettings |
Application settings |
|
|
|
|
|
|
myFavoriteWebSites |
Favorite URLs and other Web identifiers |
|
|
|
|
|
|
myWallet |
Receipts, payment instruments, coupons, and other transaction records |
|
|
|
|
|
|
myDevices |
Device settings and capabilities |
|
|
|
|
|
|
myServices |
Services provided for an identity |
|
|
|
|
|
|
myUsage |
Usage report for above services |
|
|
|
|
|
|
|
|
|
· Type the URL to the Web Service.
Note: See page 270 for more information on creating a Web Service.
‚ Click OK.
— Open the class file.
± Add an alias to the Web service namespace.
¡ Rename the namespace to HelloConsoleApplication.
™ Create a new variable of type Service1.
£ Write the result of the call to the WebMethod to the console.
¢ Set a debug stop.
∞ Press F5.
■ The output to the
console appears.
273