- •Contents
- •What Is C#?
- •C# Versus Other Programming Languages
- •Preparing to Program
- •The Program Development Cycle
- •Your First C# Program
- •Types of C# Programs
- •Summary
- •Workshop
- •C# Applications
- •Basic Parts of a C# Application
- •Structure of a C# Application
- •Analysis of Listing 2.1
- •Object-Oriented Programming (OOP)
- •Displaying Basic Information
- •Summary
- •Workshop
- •Variables
- •Using Variables
- •Understanding Your Computer’s Memory
- •C# Data Types
- •Numeric Variable Types
- •Literals Versus Variables
- •Constants
- •Reference Types
- •Summary
- •Workshop
- •Types of Operators
- •Punctuators
- •The Basic Assignment Operator
- •Mathematical/Arithmetic Operators
- •Relational Operators
- •Logical Bitwise Operators
- •Type Operators
- •The sizeof Operator
- •The Conditional Operator
- •Understanding Operator Precedence
- •Converting Data Types
- •Understanding Operator Promotion
- •For Those Brave Enough
- •Summary
- •Workshop
- •Controlling Program Flow
- •Using Selection Statements
- •Using Iteration Statements
- •Using goto
- •Nesting Flow
- •Summary
- •Workshop
- •Introduction
- •Abstraction and Encapsulation
- •An Interactive Hello World! Program
- •Basic Elements of Hello.cs
- •A Few Fundamental Observations
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Essential Elements of SimpleCalculator.cs
- •A Closer Look at SimpleCalculator.cs
- •Simplifying Your Code with Methods
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Lexical Structure
- •Some Thoughts on Elevator Simulations
- •Concepts, Goals and Solutions in an Elevator Simulation Program: Collecting Valuable Statistics for Evaluating an Elevator System
- •A Deeper Analysis of SimpleElevatorSimulation.cs
- •Class Relationships and UML
- •Summary
- •Review Questions
- •Programming Exercises
- •The Hello Windows Forms Application
- •Creating and Using an Event Handler
- •Defining the Border Style of the Form
- •Adding a Menu
- •Adding a Menu Shortcut
- •Handling Events from Menus
- •Dialogs
- •Creating Dialogs
- •Using Controls
- •Data Binding Strategies
- •Data Binding Sources
- •Simple Binding
- •Simple Binding to a DataSet
- •Complex Binding of Controls to Data
- •Binding Controls to Databases Using ADO.NET
- •Creating a Database Viewer with Visual Studio and ADO.NET
- •Resources in .NET
- •Localization Nuts and Bolts
- •.NET Resource Management Classes
- •Creating Text Resources
- •Using Visual Studio.NET for Internationalization
- •Image Resources
- •Using Image Lists
- •Programmatic Access to Resources
- •Reading and Writing RESX XML Files
- •The Basic Principles of GDI+
- •The Graphics Object
- •Graphics Coordinates
- •Drawing Lines and Simple Shapes
- •Using Gradient Pens and Brushes
- •Textured Pens and Brushes
- •Tidying up Your Lines with Endcaps
- •Curves and Paths
- •The GraphicsPath Object
- •Clipping with Paths and Regions
- •Transformations
- •Alpha Blending
- •Alpha Blending of Images
- •Other Color Space Manipulations
- •Using the Properties and Property Attributes
- •Demonstration Application: FormPaint.exe
- •Why Use Web Services?
- •Implementing Your First Web Service
- •Testing the Web Service
- •Implementing the Web Service Client
- •Understanding How Web Services Work
- •Summary
- •Workshop
- •How Do Web References Work?
- •What Is UDDI?
- •Summary
- •Workshop
- •Passing Parameters and Web Services
- •Accessing Data with Web Services
- •Summary
- •Workshop
- •Managing State in Web Services
- •Dealing with Slow Services
- •Workshop
- •Creating New Threads
- •Synchronization
- •Summary
- •The String Class
- •The StringBuilder Class
- •String Formatting
- •Regular Expressions
- •Summary
- •Discovering Program Information
- •Dynamically Activating Code
- •Reflection.Emit
- •Summary
- •Simple Debugging
- •Conditional Debugging
- •Runtime Tracing
- •Making Assertions
- •Summary
Consuming a Web Service |
361 |
Trying Out the Client
To simulate two different users making changes to the same record, start two Web |
15 |
browsers. In the first Web browser, use the URL http://localhost/TaskClient/ TaskUI.aspx. In the second browser, use the URL http://127.0.0.1/TaskClient/ TaskUI.aspx. Using these different URLs has the effect of creating two different ASP.NET sessions, so you can simulate two different users.
Try adding and modifying records using the two different clients. Figure 15.2 shows an example in which one user tries to modify a record changed by another user.
FIGURE 15.2
Two shared task clients that modify the same record.
Summary
Today’s lesson showed how to create Web Services that use and return custom data structures and complex parameters. The first section detailed each kind of data type that you can use with HTTP and SOAP. You saw how HTTP restricts many of your options for a Web Service and how you can use SOAP to transmit almost any kind of data.
The second half of today’s lesson gave an advanced example of a Web Service that handles conflicting data modification requests from multiple clients. You learned the difference between optimistic and pessimistic locking and saw one strategy for overcoming some of the limitations of the optimistic locking scheme. The example used a record versioning strategy, which added a timestamp to each record. You can use the techniques presented in this sample in your own Web Service applications. These record-locking techniques aren’t unique to Web Services and can be applied in many kinds of applications.
362 |
Day 15 |
Q&A
QBecause the HTTP protocol is limited to call by reference, what practical effect does this have for my Web Service clients?
AWeb Service clients that use HTTP instead of SOAP won’t be able to use any Web Service methods that use call by reference parameters. ASP.NET generates the WSDL interface file automatically, and it won’t generate method definitions for methods that use reference parameters for the HTTP protocol. All methods will contain definitions for the SOAP protocol.
QI’m not sure if I should use an optimistic or pessimistic locking scheme for my Web Service. What are the critical issues?
AOptimistic locking works well when a large number of client programs don’t modify the same records frequently. This situation can be described as the “low contention” scenario because client programs don’t contend to modify the same records too often. However, if multiple client programs update the same records frequently, optimistic locking doesn’t work very well. Use pessimistic locking for this “high contention” scenario, when many users want to modify the same data record.
Workshop
The Workshop provides quiz questions to help you solidify your understanding of the material covered today as well as exercises to give you experience using what you’ve learned. Try to understand the quiz and exercises before continuing to tomorrow’s lesson. Answers are provided in Appendix A, “Answers to Quizzes and Exercises.”
Quiz
1.Can the HTTP protocol support methods that use the ADO.NET dataset as a parameter? Why or why not?
2.What is an “out” parameter?
3.What is a “ref” parameter?
4.What is optimistic locking?
5.Why is optimistic locking used in Web applications and services?
6.What method can you use to check the validity of a record modification request when using optimistic locking?
Consuming a Web Service |
363 |
Exercises
1. The Task Server contains a major defect if used by many clients simultaneously. |
15 |
The problem is that the Update method might change the contents of the source XML file while the AddTask method is being called. Use the .NET Monitor class to synchronize access to the XML file in the Web Service.
2.Change the Task Server to use a SQL Server database or a database server of your choice. How much of the code needs to be changed?
WEEK 3
DAY 16
Putting It All Together with Web Services
By reading the last three days’ lessons, you’ve gained a good knowledge base to use when creating your own Web Services. Today’s lesson will build on the last three and will teach you some more advanced techniques that you can use in your own Web Services.
Today’s lesson will begin by explaining how to manage state in your Web Services. This issue arises when you need to store information about particular Web Service clients between the calls that they make to your service. You will learn how to use the built-in Application and Session objects within your Web Services. As you will see, managing state can get complicated depending on the capabilities of the client accessing your service. This lesson will give solutions to many of these complex issues.
Today’s lesson will continue by introducing a new technique called asynchronous calling. Web Service clients can use this method to call Web Service methods that may take a long time to complete. Clients can create a request to execute a Web method, perform other work, and then be signaled when the method is complete. You will see two examples that use this technique, including a Web form–based client.