Web Services: building reusable Web Components with soap and asp.net (English)

zhaozj2021-02-08  257

Web Services: Building Reusable Web Components with SOAP and ASP.NETDavid S. PlattThis article assumes you're familiar with XML and Visual BasicLevel of Difficulty 1 2 3 Download the code for this article: WebComp.exe (93KB) Browse the code for this article at Code Center: TimeServiceDemo SUMMARY XML and HTTP are cross-platform technologies especially suited for building applications that can communicate with each other over the Internet, regardless of the platform they are running on Web Services in the Microsoft .NET Framework make it easy. to write components that communicate using HTTP GET, HTTP POST, and SOAP.An understanding of these concepts, along with knowledge of synchronous and asynchronous operations, security, state management, and the management of proxies by the .NET Framework is essential in building these Applications.This Article Has Been Adapted from David Platt's Upcoming Book Introducing The Microsoft .NET Platform To Be Published by Microsoft Press in Spri NG 2000.

seismic shift is just now beginning in Internet programming. Internet access will soon be built into nearly every program anyone ever writes. You will not use a generic browser, except when you feel like browsing generically. Instead, you will use dedicated programs that are optimized for accomplishing specific tasks. you will not be aware of the program's Internet access (except when it breaks) .An early example of this type of program is Napster, which allows you to search the shared hard drives of thousands of participating users for music files that meet specified criteria, and download the ones you like. The dedicated user interface of a multi-player game is another example of hidden Internet access. and the latest edition of Microsoft® Money does a good job of seamlessly blending Web content ( Current Stock Quotes, Latest Balances, And So ON) And Desktop Content (Financial Plans You Create Locally) .in Order to Develop Programs of this Type, There Needs to Be a Quick and Easy Way (Which, in turn, means a cheap way) to write code that communicates with other programs over the Internet The idea is not new; there are a number of existing technologies that enable this type of communication:. RPC, DCOM, and Microsoft Message Queue Services (MSMQ) Each of these techniques is cool in itself, but they all share one fatal failing:.. they only work from one similar system to another MSMQ talks only to MSMQ, a DCOM client only to a DCOM server, and so on. What's really needed is universal programmatic access to the Internet-some way for a program on one box to talk to any other program on any other box. It has to be independent not only of the operating system, but also of the programs'

INTERNAL IMPLEMENTATIONS (IS IT C or Visual Basic®? Which vendor is it from? Which Version is it?

Right now you can barely solve this problem on a single desktop.) And it has to be easy to program, or no one will be able to afford it.Solution ArchitectureThe only way to deal with the enormous numbers of heterogeneous entities on the Internet is to use the lowest common denominator. In other words, when bytes are transferred from one box to another, the process needs to use some standard that everyone on the Internet supports. The most common Internet transfer protocol is HTTP, which is used today by essentially all Web browsers to request the pages they display. The emerging cross-platform standard for encoding pure information transferred over HTTP is XML.Microsoft put these ideas together and developed the concept of a Web Service-a seamless way for objects on a server to accept InComing Requests from Clients Using HTTP and XML. To create a Web Service, you Simply Write a Microsoft .NET Server Object As if IT WERE Going to Be Accessed Directly by Local Clients, Mark IT with AT tribute that says that you want it to be available to Web clients, and let ASP.NET do the rest. It automatically hooks up a prefabricated infrastructure that accepts incoming requests through HTTP and maps them to calls on your object, as shown in Figure 1 . By rolling them into a Web Service, your objects can work with anyone on the Web who speaks HTTP and XML, which should be everybody. you do not have to write the infrastructure that deals with Web communication-the operating system provides it for you .

Figure 1 Handling HTTP RequestsOn the client side, .NET provides proxy classes that have easy, function-based access to the Web Services provided by any server that accepts HTTP requests, as shown in Figure 2. A developer tool reads the description of the Web Service and generates a proxy class containing functions in whatever language you're using to develop the client. When your client calls one of these functions, the proxy class generates an HTTP request and sends it to the server. When the response comes back from the server, the proxy class parses the results and returns them from the function. This allows your function-based client to seamlessly interact with any Web server that speaks HTTP and XML.Figure 2 Proxy ClassesWriting a Web ServiceAs I always do when explaining a new piece Of Technology, I Could To Demonstrate Web Services. This Sample Web Service Provides The Current Time of Day on Its Machine In The Form of A String, Either With OR WITHOU t the seconds digits. You can download the sample code for this service from the link at the top of this article to follow this description more closely. You will need to download and install the Microsoft .NET SDK if you want to run it as well .I wrote this Web Service in the form of an .asmx page to avoid distracting you with other software packages. I first installed the .NET SDK. Then I used the administrative tools in Microsoft Internet Information Services (IIS) to set up a virtual Directory Pointing to The Folder in Which I Would Do My Service File Development. The I Wrote My ASP.NET CODE IN A File Called TimeService.asmx on My Server.

Writing the Web Service was incredibly easy; I did it in Notepad (see Figure 3) While it's quite simple, there are a few constructs that are probably new to you, so let's go over it section by section (You should note that.. the code in this article is based on pre-Beta 1 bits and the calls may change somewhat before the final release.) The program starts with the standard ASP.NET salutation <% @ ...%>. in it, the WebService directive tells ASP.NET that the code on the page is to be exposed as a Web Service. The Language attribute tells ASP which language to compile the page's code in accordance with. I've chosen Visual Basic because it's familiar to the majority of developers. . ASP.NET will use Visual Basic.NET to compile the code You do not have to install Visual Studio®;. all the language compilers come with the .NET SDK The class attribute tells ASP.NET the name of the class of object to Activate for Incoming Requests Addressed to this service.the rest of the point hat implements the class. I first use the Imports directive, a new feature of Visual Basic.NET, which tells the compiler to import the namespaces. The term "namespace" is a fancy way to refer to the description of a set of prefabricated functionality . It is conceptually identical to a reference in your Visual Basic 6.0 project. Since ASP.NET will compile this example's code "just-in-time" when a request arrives from a client, I do not have a project in which to set ............................... ...

Web.Services contain the prefabricated features you need to write a Web Service.The next line defines the name of the class. This class in Visual Basic is similar to many you've written. You'll see a brand new keyword (Inherits) at the end of the line:.. Inherits WebService This represents one of the main enhancements in Visual Basic.NET-support for the object-oriented concept called inheritance When you say that your new TimeService class inherits from the WebService class, you are telling the compiler to take all the code from the system-provided class named WebService (known as the base class) and include it in your new TimeService class (known as the derived class). and in addition to reusing the code from the class you inherit from, you can add, alter, and override the functionalities of that class in your new clan. Think of inheritance as cutting and pasting without actually moving anything. in fact, seriously deranged C and Java-language geeks often refer to physically cutting and pasting code as editor inheritance.The WebService class is provided by the new .NET runtime library, just as many objects used in Visual Basic 6.0 were provided with the operating system. WebService contains all the prefabricated plumbing required to handle the incoming HTTP requests and route them to the proper methods on your object.I'm often asked what the difference is between importing the namespace and declaring inheritance. in other words, what's the difference between the Imports and Inherits keywords? Well, the Imports statements only bring in the Description of a set of functionality, it doesn't actually make use of it. as i said before, it's like setting a reference.

The Inherits keyword actually takes part of the code referred to in the description and uses it. It's like Dim on steroids.Now that I've defined my class, I need to define the methods and functions of the class. Again, this is very similar to the way it worked in Visual Basic 6.0, but with one new twist:. you have to add a new attribute, , to every method that you want exposed to Web clients This tells ASP.NET that the method in which it appears is to be exposed to clients as a Web Service. You've seen plenty of attributes in Visual Basic 6.0, such as public, private, and const. The .NET Framework and Visual Basic.NET use many more of them , which is why there's a new syntax for specifying them. Just as your class can contain public and private methods, so can it contain some methods that are exposed to Web clients and others that are not.Finally, let's get down to the internals of THE CLASS'S Method. The Handling of The Date and Time Variables in My Code May Look A Little strange to you; it was to me Do not worry about it, it's just how Visual Basic.NET deals with dates and times.Figure 4 TimeService.asmx in a BrowserNow let's access the Web Service from a client ASP.NET contains.. good prefabricated support for this as well. If you fire up Microsoft Internet Explorer 5.5 and request the ASP.NET page I just wrote, you will see the page shown in Figure 4. ASP.NET detects the access to the Web Service page itself ( as opposed to one of the methods forin the service), And Responds with a page of information about the service. this shows you all the methods That ASP.

NET found in the target class (in this case, only GetTime), and provides a test capability for each method. Enter True or False to tell it whether to show the seconds digits or not, click Invoke, and the test page will call the specified method and return the results (if you entered TRUE), as shown in Figure 5. Note that the parameters are passed in the URL, and the results are returned as XML via the GET protocol. Also note that you must open the page in a way that goes through IIS, typing in a qualified UTL such as http:. // localhost / your virtual directory name / timeservice.asmx If you simply double-click the page in the Explorer view of your hard disk, you'll bypass IIS and ASP.NET and simply receive the text of the .asmx page, which is not what you're looking for.Figure 5 Show SecondsThat's all I had to do to write my Web Service. It only took 13 lines, counting else And endif. How much easier does it get? Self-Description of Web Services: The SDL Filein Order to ALLOW Program WO D evelop client apps that use my Web Service, I need to provide the information that they need to have during their design and programming process. For example, a client of my Web Service would probably like to know the methods the Web Service exposes, the parameters required, and the protocols supported-something conceptually similar to the type library that a standard COM component would carry. The problem with type libraries, however, is that they are Microsoft COM-specific, and you may want non-Microsoft systems to be able To Consume your Web service as well.

You may also want to be able to write descriptions of non-Microsoft services running on non-Microsoft systems, so that your Microsoft-built client applications can use these services. What you need is a universal method of describing a service. And you need it to be machine-readable, so that intelligent development environments can make use of it.ASP.NET provides such a descriptive service. When it compiles a Web Service, ASP.NET can produce a file listing the protocols supported by the service and the methods provided and parameters required by that service. The file is encoded in XML and uses a vocabulary called Service Descriptor Language (SDL). The SDL file of a Web Service is sometimes known as its contract because it lists the things that the service is capable of doing and tells you how to ask for them.You obtain the SDL file from ASP.NET by requesting the .asmx file with the characters? SDL attached to the URL. for example, on my local machine, I obtained the SDL file for My SA mple Web Service by requesting the URL http: //localhost/AspxTimeDemo/TimeService.asmx SDL Since I'm not exposing the sample service on an actual running server, I've provided this file in the sample code.When you wrote?. COM components in Visual Basic 6.0 and Visual C ® 6.0, you sometimes wrote the component first and then wrote a type library to describe it. Other times you started with a type library describing an interface and wrote code that implemented it. SDL can work in ..

Alternatively, I could have written the SDL file first, describing what I'd like the service to do, and then use the utility program WebServiceUtil to generate a template file that implements the service that it describes (similar to the Implements keyword in Visual Basic 6.0) .The SDL file is somewhat complex, so I've extracted portions to discuss the principles (see Figure 6). The serviceDescription element is the root of the file. Everything inside it is part of the description of this Web Service. It contains an attribute giving the name of the Web Service, in this case, TimeService.The root contains two interesting types of subelements. The first is a protocol description, which tells an interested client developer which protocols the service supports and how to encode the request And Response Data For That Protocol. I've shown the one for the http get operation, Since That The Easiest for Me To Visualize. Any Person or Development Tool Looking At The SDL Will Know That The Servi ce named TimeService can be accessed via this protocol. The full SDL file also contains entries saying that it can be accessed via the HTTP POST operation and also via the Simple Object Access Protocol (SOAP) -an HTTP / XML hybrid. I discuss HTTP POST and SOAP in the next section of this article. Looking inside the protocol description, you can see that the service contains a method called GetTime, which lives at the specified URL. The request requires a single parameter called ShowSeconds. The fact that it is used In an HTTP Get Operation Implies That IT ITED IN The Form of A String. The Response Coms Back in XML, Again In The Form of a string.

The second interesting piece in the SDL file is the W3C-standard element. This contains the abstract definition of the service, without regard for how it's accessed in any particular protocol or even where it lives. Think of this as an interface definition in a type library;.. it describes a set of methods, but not an implementation of them You can see that my sample SDL file describes a function named GetTime, which requires a Boolean parameter called ShowSeconds You can see that it also contains a separate description of the result of the GetTime method, which comes back in a string.Writing Web Service ClientsNow let's look at what you have to do to write a client that accesses this service. I found writing the clients as easy as writing the service itself. The ASP.NET listener accepts three different ways of packaging the incoming request into HTTP:. HTTP GET, HTTP POST, and SOAP This is conceptually similar to an aircraft control tower speaking three dialects of English- s ay, American, British, and Strine (Australian) I'll look at all of these to examine how you would write a client that uses them.Case 1:. HTTP GETThe Web Service will accept a request through a simple HTTP GET request with the ShowSeconds parameter in the URL string. You saw the SDL file describing my Web Services support for this protocol in the previous section. A sample Web page providing access to this request is shown in Figure 7, and is provided as the file GetTimeThroughHttpGet.htm .

Figure 7 Sample Web PageCase 2:. HTTP POSTThe Web Service will accept a request through an HTTP POST request with the ShowSeconds parameter in an input control The portion of the SDL file describing my Web Service's support for the HTTP POST operation is shown in Figure 8 . The separate and elements indicate that making the request of the service, that is, posting the form, is a separate operation from receiving the response. This is how an HTTP POST operation normally works, so do not worry about it. The

element indicates that the POST operation requires a form containing an input control named ShowSeconds, which will carry this parameter. I used Microsoft FrontPage® 2000 to write a form that does this. A screen shot is shown in Figure 9, and the HTML code in Figure 10. The file is provided as GetTimeThroughHttpPost.htm. When the request reaches the server, ASP.NET creates the TimeService object, pulls the parameters from the form's controls, and calls the Ge tTime method.Figure 9 Sending a Request via HTTP POSTCase 3: SOAP The Web Service will accept an incoming call request via an HTTP POST request that has all of its information encoded in a SOAP packet SOAP is an XML vocabulary that describes function calls and. their parameters Newcomers to SOAP will probably benefit from reading. "Develop a Web Service: Up and Running with the Soap Toolkit for Visual Studio" in the August 2000 issue of MSDN Magazine, and A Young Person's Guide to the Simple Object Access Protocol in the March 2000 issue.

The portion of the SDL file describing my Web Service's support for the HTTP POST operation is shown in Figure 11. I've written a sample program, shown in Figure 12, that uses SOAP to call the GetTime method of the Web Service. It uses the Microsoft Internet transfer control to do the actual HTTP communication. Note that, unlike the previous two examples where the URL pointed at the method within the service, SOAP encoding is directed to the .asmx page containing all the methods of the Web Service. The SOAP packet sent to the server contains the name of the function and its parameters, encoded in XML according to an agreed-upon schema, as you can see in the top edit control. When the SOAP packet reaches the server, ASP.NET recognizes it , parses the method name and its parameters out of the packet, creates the object, and makes the call.Figure 12 Soap ClientThe XML encoding of the request packet varies somewhat from that used by the SOAP Toolkit Rob Caron discussed in his August 2000 ar . Ticle and the examples that come with it SOAP and .NET are currently very much under development, so this type of divergence is only to be expected.Case 4: Intelligent SOAP Proxy, Synchronous OperationThe SOAP example from the previous section is obviously quite tedious to write. It reminds me somewhat of manually writing an IDispatch client in COM, in the sense that there's an awful lot of boilerplate packaging that's critical to get correct (one character off and you're hosed), but which varies little from one method To the next.

Just as C and Visual Basic-provided wrapper classes that took the pain out of accessing automation objects (many programmers using Visual Basic never even knew it was painful for the C geeks, and the rest either did not care or actively approved), so does the .NET SDK provide the capability of generating wrapper classes that make writing a SOAP client for your Web Service a trivial operation.The tool that does this is a command-line utility called WebServiceUtil that comes with the .NET SDK. It's also available from Visual Studio, as described in the September 2000 issue of MSDN Magazine (see "Visual Studio.NET: Build Web Apps Faster and Easier Using Web Services and XML") This program reads the description of the Web Service from an SDL file and. generates a proxy for accessing them from the language you specify It currently supports Visual Basic.NET, C #, and JavaScript, but not C I copied the SDL file to a local directory and ran WebServiceUtil with the command line:.. webservice util / command: proxy / language: vb /path:timeservice.sdlThe Visual Basic-based proxy code is shown in Figure 13. It contains both synchronous and asynchronous versions of functions for getting the time from the Web Service I'll discuss the. Synchronous Version Here, And The Asynchronous Version in The next section The proxy class inherits from the base class system.Web.Services.protocols.soapclientProtocol (you '

re going to have these long names, so get used to them), which contains the actual code. The proxy class contains a property called Path, which the proxy inherits from the base class. This property specifies the URL of the server to which the call is directed. It contains a default value that it got from the original SDL file, but the sample program demonstrates how you can change it at runtime if you want. The client calls the named method on the proxy by calling the method Invoke, which , again, it has inherited from the base class. This method then creates a SOAP packet containing the method name and parameters (as shown in the previous section) and sends it to the server over HTTP. When the SOAP response packet comes back from the server, the base class parses out the return value and returns it to the proxy, which then returns it to the client.Note that the attributes block in the function name (the characters between the angle brackets) contains information that tells the base cla ss how to package the call, such as the names of methods and parameters. Visual Studio.NET makes extensive use of attributes as a way of passing information to the prefabricated functionality of system code. In earlier days, this would probably have been done through member variables of the base class where it would have been difficult to differentiate immutable runtime attributes from those that can change during program execution. The new arrangement is harder to mess up, which is generally a good thing.The actual Visual Basic-based code for The Client Is Shown in Figure 14, And The Sample Client App I Shown in Figure 15. You Can See That IT '

s pretty trivial The heavy lifting is done for you by the proxy class.Figure 15 Client AppCase 5:. Intelligent SOAP Proxy, Asynchronous OperationThe Internet is much more amorphous and nondeterministic than a single desktop Like Disneyland, it can be a lot of fun. , but it's almost always so crowded that you have no hope of enjoying it without a clever strategy for managing its chronic overload. Even the simplest call to a Web Service can easily take 5 or 10 seconds to complete, even on a good day with the Wind at Your Back, and Can Up to Anything from There. You Can't Leave a User With A Second Or Two, if That.you Can't make Blocking Calls to a Web Service from the Thread That Handles Your Program's User Interface. An Industrial-Strength App Can't Just Call The Synchronous Proxy Method from The Form Button '

s response function, as I did in the sample. You have to somehow make the call from another thread which can wait for the response without hanging the user interface, and at some later time retrieve the results from that thread and present them to the user .Until now, most programmers have had to write their own infrastructure code to handle this situation. This type of code is notoriously tricky to get right in all cases, and the time that it took represented a dead loss in monetary terms. now, however , since this situation is nearly universal, the intelligent proxy classes generated by WebServiceUtil is equipped with prefabricated code to handle it. Instead of making a call and blocking until it completes, your program can call one method that transmits the request data to the server and THEN RETURNS IMMEDITELY. LATER, AT Some Convenient Time, Your Program Can Call Another Method to Harvest The Results Returned from The Server. This Makes Your Life Much, Much Easier Because You Don '

t have to write the scheduling code. And it works with any server, not just those that are written to support asynchronous access.Figure 13 showed both the synchronous and asynchronous versions of the time service call. The proxy contains a method with the name Beginmethodname , in this case, BeginGetTime. Its parameter list begins with the parameters of the synchronous method, in this case, the ShowSeconds Boolean variable. It has two more parameters used in a callback case that is beyond the scope of this article. in the client code in Figure 14, I pass Nothing (the equivalent of null in Visual Basic) for both of them. This starts the communication chain, sending out the request, and returning immediately. The return value is an object of type IAsyncResult, which I will Usess Getting The Result Later. i can go off and do what in the lab, i inserted a long ity, just counting from one to a billion to Simulate a long Operatio n. You'll Have to Do That yourself if you want to test this.at Some Later Point, if you want to harvest the results of the Operation, Find Out What Time Actually Was When The Server Sent It Back to you. You Do That by calling the endmethodname method on the proxy, in this case, endgettime, passing the IasyncResult That I got from the beginning

s how the infrastructure code knows which result to give back to you, as the client can have several outstanding requests at the same time. The end method harvests the result and returns it.But how do you know when the operation is complete and that the results are ready for you to harvest? The IAsyncResult object contains a method used for just this purpose, called IsCompleted, which returns True or False. Your user interface thread can poll periodically to see if it's done. If you call EndGetTime before the operation is finished, it will block until the operation does in fact complete. While you probably do not want to do this from your main user interface thread, it may make sense to call it from a worker thread that has reached a point in its processing from which it can not proceed without the results of the Web Service call.Web Service Support in Visual Studio.NETThe Web Service example I showed earlier demonstrated that writing Web Services does not depend on fancy programming envi ronments. But since developer time is the second greatest constraint in software development, it makes sense to write your Web Services using tools that will help you crank them out faster. Since Notepad lacks such useful features as an integrated debugger, Visual Studio.NET is a better choice for Writing Web Services. I '

ll show you how to write a service using the July 2000 PDC release of Visual Studio.NET.When you select File | New | Project from the Visual Studio.NET main menu, it offers you the dialog box shown in Figure 16.Figure 16 New Web ServiceI selected Visual Basic Projects in the left pane, the Web Service icon in the right pane, entered a project name in the edit control, and clicked OK. Visual Studio.NET generated a new solution containing the files shown in Figure 17. By the way, the "solution" seems to hold one or more projects, so I really think project group would be a far more descriptive term.Figure 17 Solution ExplorerThe interesting programming takes place in the file WebService1.vb, where the code that implements your Web Service actually lives. The code for my sample Web Service is shown in Figure 18. I've written a bit more code in this one to demonstrate interesting features of the Web Service environment. It was much easier with Visual Studio.NET than With notepad. if you loo K at The Downloaded Sample, You'll Find That The Figure OMIMITS A LOT OF Administrative Code Placed in The Class for the Internal Use of Visual Basic.Net (Users of Visual J

® 6.0 will find it familiar). Comments in the code promise that it will be hidden in the final release, which will be helpful since it can be very confusing to read.The wizard generates several other files, as you saw in Figure 17. The file config.Web is an XML data file that contains various configuration options that tell ASP.NET how to handle your Web Service at runtime. For example, it contains the ASP.NET session timeout interval.The file .disco is an XML data file that is used for controlling dynamic discovery of the Web Service by clients by collecting SDLs and schemas together. The default configuration, which you can see in the downloaded code, simply contains entries that tell ASP.NET to exclude certain subdirectories (generated in the publishing process) from the SDL file that it generates.The .asmx file is the target for client requests. Unlike the previous example in which the code lived in this file, this example contains a callout to code that lives elsewhere, as shown h ERE: <% @ Webservice Language = "VB" CodeBehind = "WebService1.vb"

Class = "vs7demotimeservice.webservice1"%>

The Codebehind attribute tells it where to find the code that backs up the service.The file globals.vb is where event handler functions live that handle project-wide events, such ApplicationStart and ApplicationEnd. You put your own code in these functions, and ASP .NET will call it when the specified event takes place. The file globals.asax is the connection between these files and ASP.NET.When you build your project, Visual Studio.NET automatically publishes it to the Web server you specify (in this case, the system default). to test the Web Service, simply start the debugger from the Visual Studio.NET main menu. It opens a testing page similar to the one you saw in Figure 4, which is a very smooth way to handle things . You can set breakpoints in your Web Service and debug anything. The only snag I've found is that the Web Service does not handle bad user input well. For example, if the user puts in a bad value, say, he spells False IncorRectly, ASP.NET REJECTS The Incoming Request BEF ore it hits your Web Service and you get the error shown in Figure 19.Figure 19 Error ReportIntrinsic Objects The original version of ASP contained a set of objects called the intrinsic objects, which represented the connection between an executing ASP script and the server on which .. it was running This set included the Server, Application, Session, Request, and Response objects An ASP script could access them directly; a COM object used by an ASP script had to jump through a few hoops to get them, but could also Use the theout too much trouble.asp.net contains Similar Objects, General Updated and enhanced. They '

ve also been repackaged to make them easier to use. The intrinsic objects are now part of the System.Web.Services.WebService base class. When you derive your service from that class, you inherit them for free (I told you that you were going to like inheritance) .The Server, Application, and Session objects are part of your Web Service class. They have been updated and enhanced somewhat. For example, the Server object now has an HtmlEncode method, to match its previously existing HtmlDecode. There is a new object called User, which you will use when performing security authorization (see the section on Security). The Request and Response objects have been moved inside a new intrinsic object called the Context (not to be confused with the ObjectContext used for committing and aborting transactions-its an unfortunate overloading of the nomenclature). You generally will not use these much, as their functions (accessing properties in the posted form) have been abstracted away by the Web Service infrastructure. I'll show how to use some of them in the next section.State ManagementMany, many trees have been senselessly murdered in the ongoing debate about stateful versus stateless objects. I won '

t even attempt to settle the question here. What I will do is to show you the options that are available for state management in a Web Service, which you can then use to implement whatever state behavior you decide you want.Web Service objects in their natural state (groan) are stateless. that means that ASP.NET constructs a new instance of that object for each incoming call and destroys it at the end of the call. The result of one call is not available to the next call unless you go Out of Your Way To Make it you want; sometimes it isn't. for my time service show in this article, it probably is. What aclient port . what the next client wants or should get Each function call is sufficient unto itself; there's no reason to keep any state from one call to the next In the classic sense of an object as a combination of data and the code that operates on the. Data, you might argue That there is no object here Client Is Simply Making A Call That Has No Relation To Anything Else, And That's Not An Object, That's A Function. Well, Tough. I'm Going to Call It An Object Because It Uses .Net Object Services, and You Can't stop me.When this behavior is not what you want, an object can maintain state between one call and another. The object is still created and destroyed on demand, but it can use ASP.NET to hold data that it was working on in .. its previous life, as you might leave instructions in your will for your descendants ASP.NET can maintain two types of state in a Web Service: application state and session state The former is available to all parts of your Internet application;

that is to say every session with every user in that object and all the others built into your solution. (See what a lame word solution is? Try substituting project group. Better, no?) The latter is available only to the session of the individual user currently connected to it. A session is a time-limited conceptual connection maintained by ASP.NET to make it easier for you to program behavior on a per-conversation basis. you use session state for remembering things like the items in a particular User's shopping cart, so you '

ll probably use it much more often than application-level state.The ASP.NET system on which the Web Service is based provides a mechanism for maintaining and tracking state variables. The base class WebService from which your Web Service derives contains two collections for holding state, one called Application and one called Session. You can put data into them and take data out by accessing them through string names of items. The sample service shown in Figure 18 contains code that uses both types of state. I keep the total number of requests in the Application, and the number of requests for this particular user in the Session-level state.Security in Web ServicesSecurity is vital to any type of distributed programming, and discussions of it are often highly charged with emotion. I will attempt to Outline The Problems That Arise In this area too much trouble. But a full-scale discussion of security would take two or three full articles.All Web Services care about security, whether you think they do or not. You might think that my sample time service does not, because I don '

t care who sees the timer. I still want to make sure that no one deletes it or hacks the code to make it subtract ten minutes from the current time and make all the callers late for their appointments. So I want read permission for anyone, and write permission only for a select few. IIS provides that basic level of security.The security requirements of a Web Service are somewhat like those of a city hall. You have large numbers of people coming and going anonymously, accessing areas such as the tourism office that dispenses maps one at a time. But other areas of the same city hall, such as the floor containing the mayor's office, do not allow anyone in who can not prove his identity (authentication) and who does not have business on the Floor (Authorization) .The First Problem in Security Is Authentication-Who Are you, and how do i know you real it That Person? You can '

t figure out if this person or that one is allowed to access some resource until you know who they are. ASP.NET gets its authentication service from the underlying IIS layer. You use the existing IIS tools to mark particular pages as requiring authentication, as shown in Figure 20, clearing the Anonymous access box as shown. IIS will then require a user ID / password handshake before allowing the user to view the specified page. This requires that the user have an account accessible to the server. If the authentication succeeds , the ASP.NET script is run using the identity of the authenticated user.Figure 20 Authentication MethodsWhen writing a .NET proxy-based client application (as shown earlier) the proxy base class contains properties called Username, Password, and Domain. Setting these Properties, Both Strings, Allows A Client To Easily Pass The M. Obviously, Authentication Requires Extra Network Traffic, SO you Should Only Use it when you all tourist office would not be very useful if it required IDs and strip searches to obtain a map. My sample does not require authentication just to read the page, only to write it. In this case, the administrator leaves the Anonymous access box checked and specifies the identity of the account that any anonymous client will use for its access checks on the server. Generally, this account has a low level of privilege.Once you've decided who the user is, you need to check before performing any SENSITIVE ACTION WHETER THAT PERSON IS OR ISN '

t allowed to do it. Sometimes this is handled by the back end operating system. For example, NTFS allows an administrator to set access permissions on files, and you hope that administrator will not allow anonymous users access to system configuration files. Other programs , such as Microsoft SQL Server ™, do similar things.It's usually best to perform authorization checks as early in the access process as possible, so that if they fail, you've wasted the minimum amount of processing time. Your Web Service object can perform its own authorization via the intrinsic object named User. This contains a method called IsCallerInRole, which will tell you whether the user is a member of an administrative group you've set up elsewhere. It is conceptually identical to the similar method seen in COM ................. .י.......................... me. You can write your own code that uses that information to decide if the user is allowed to do this or that.Web Services will, at some future time, support Passport authentication and cookie-based authentication, which would make many lives easier. That Is Beyond The Scope of this Article.conclusionAll Program Would Like to Write Program That Talk to Each Other over the Internet, No Matter What Type of System THEY '

re running on. You can accomplish this by settling on the lowest common denominator of XML and HTTP. The Web Services portion of the Microsoft .NET technology suite makes it easy for you to write your business logic in the form of .NET components and expose them to all Web clients that speak HTTP GET, HTTP POST, or SOAP. that's just about everyone in the world. An SDL file, also generated by the Web Services infrastructure, provides a machine-readable description of the functionality available through the Web Service . A proxy generator provides function-based access to a Web Service, making it easier to write client programs. Visual Studio.NET provides a productive environment for developing Web Services. The Web Service infrastructure provides several options for setting up the security required by a Web app. These Tools allow you to write better Internet Applications Much More Quickly-a Good Combination.for Related Articles See: Visual Studio.net: Build Web Applications Faster and Eas Ir useing web services and xmlp2p: Writing peer-to-peer networked apps with the microsoft .NET Framework

David S. Platt is president and founder of Rolling Thunder Computing Inc. He teaches COM and COM at Harvard University and at companies all over the world He publishes a free e-mail newsletter on COM , available at http:. //Www.rollthunder .com. David is The Author of understanding COM (Microsoft Press) and an upcoming book Introducing The Microsoft .NET Platform, Due In Spring Press.

From The February 2001 Issue of Msdn Magazine.get It at Your Local NewsStand, or Better Yet, Subscribe.

Back to TOP

转载请注明原文地址:https://www.9cbs.com/read-1794.html

New Post(0)