Microsoft.NET Architecture and the C# Language A semester course for 4 th year students Comments to the presentations Prof. Vladimir O. Safonov, St. Petersburg University Email: v_o_safonov@mail.ru WWW: http://user.rol.ru/~vsafonov Lecture 10. Slide 2. This lecture is devoted to Web programming for .NET. Its central concept is .NET Web service. We consider basic concepts of ASP.NET and Web services, compare them to previous Web solutions, and give an overview of Web services definition, discovery and consuming. The goals of the lecture are as follows: - Introduction: Microsoft® .NET Framework and Microsoft ASP.NET - Overview of Web Services - Concepts and architecture - Design and development of Web services - Consuming Web services Slide 3. Here are the contents of the lecture: - Part 1: Overview - Part 2: Architecture: Microsoft .NET Framework and ASP.NET - Part 3: Developing Web services - Part 4: Consuming Web services; Invoking and applying Web services - Part 5: Advanced Web services: State control, security and transactions Slide 4. Let’s start with Part 1 whose contents is as follows: - History - What is a Web service - Distributed Web applications Slide 5. History of Web programming starts with traditional distributed programming that, in its turn, is based on client-server model. Since 1980s when OOP started to be used for commercial software development, several distributed object models based on component-oriented programming have been designed. The first was remote Method Invocation (RMI) whose characteristic features were: using proxy classes for remote access and object serialization. The most widely used object models up to late 1990s were COM, CORBA, Java RMI and Enterprise Java Beans (EJB). All of them are based on some srandard form of representing components. In late 1990s Microsoft developed Windows DNA (Distributed interNet Applications Architecture) to integrate the existing technologies - DHTML, COM, ASP, Message Queuing for developing distributed Internet applications. Slide 6. However, traditional approaches have a number of shortcomings. Distributed object models are not scalable for the Internet. Service and user are too tightly coupled. For distributed programming on the Internet their relation should be looser, based just on passing information via HTTP or other protocols and HTML or XML files. Also, unlike the Internet, such models have homogeneous infrastructure . Very serious shortcoming is versioning issues. It is especially uncomfortable for the Internet because appearance of any version of a DLL, say, from the network, different from that used on some host may completely crush a distributed application. Distributed models based on COM unfortunately are not quite portable because of limited support of COM on non-Windows platforms . As for CORBA-based models, they appear not to be portable either, because of non-portable implementation of service objects. At last, EJB (Enterprise JavaBeans) are available in Java language only, as everything in Java technology. Let’s emphasize that one of the main goals of Web programming is to achieve fully interactive Web. Slide 7. To solve the issues mentioned before, Microsoft developed its approach to Web programming and Web services for .NET. The goal of this approach is to provide services for other processes, available either on the Internet or on the Intranet of the user company. The main features of the .NET approach to Web services are as follows: - Black box concept: .NET Web services can be discovered on the Web, asked for their interface and consumed; - All .NET Web services are designed as reusable components; - They are based on .NET Framework, so they use CLR for their deployment and running as managed code; - They are supported by ASP.NET Web Services model, that, in its turn, is further developoment of ASP developed by Microsoft in 1990s; - They are based on common standards related to Web - HTTP, XML and SOAP. Slide 8. - .NET Web services enable scaled interaction between different kinds of applications, c lients and devices ; - They enable distribution and integration of application logic ; - They are loosely coupled ; - To summarize, .NET Web services, really enable programmable Web, not just fully interactive Web. Slide 9. This picture illustrates the architecture of distributed Web applications. On the client side, there are either browsers used by most of the Internet users to display Web pages or to get access to Web services. On the other hand, it is quite possible to also create just a Web client application of smaller size and consuming smaller resources than a browser that can also call Web services. On the server side, there are different kinds of Web services – portals, application-specific Web services (for example, implementing some specific business logic), and also a number of Web services that can be used as reusable building blocks for constructing more complicated Web services. During the interaction of Web clients and Web services, the Web services contracts should be followed, that should be explicitly specified in some way. Slide 10. The second part of the lecture is devoted to architectural concepts of .NET supporting Web services - NET Framework architecture, programming model and configuration . Slide 11. Features of .NET Framework architecture supporting the concept of a Web service are shown in this picture. Common Language Runtime running on top of system services provides a base framework for deploying and executing any kind of .NET applications, including Web services. CLR enables execution of a Web service as managed code. It is especially important because Web services should be based on enhanced requirements to safety and security. On top of CLR, runs ASP.NET that consists of two parts – Web Forms, supporting the GUI elements used in Web programming, and Web Services, supporting Web services theirselves. Slide 12. The programming model of .NET you already know about is shown on this scheme. Any .NET compiler translates a component written in any language, in particular, a Web service or its part, to an assembly consisting of intermediate CIL code and metadata. At runtime the intermediate code is just-in-time compiled into native code of the target platform. The code executed by the help of the CLR as managed code uses class libraries. Managed code execution enables full type checking and security checking that are so important for Web services. Slide 13. Configuration in .NET, in particular configuration of Web services, is based on XML configuration files. Configuration has hierarchical architecture: a configuration XML file influences not just on the current working directory but also on all its subdirectories. Specifically, configuring .NET Web services is based on the Web.Config XML configuration file. Slide 14. The Web.Config file b ased on XML syntax should be stored in the application directory. Configuration can be default and explicit. There is a standard (default) Web.Config file located in a .NET Framework directory. Any application’s Web.Config file settings override standard Web configuration file settings for this application. Any Web.Config file is customizable. The <webServices> section of the Web.Config file contains information on Web services. The WebServicesConfiguration class c ontains information on configuration. Slide 15. Now let’s proceed with Part 3 of the lecture – developing Web services. Here is our plan for this part: - The basics of Web service development - Web services infrastructure - Code and syntax of Web services - The Web Services Namespace - Publishing Web services - Searching Web services. Slide 16. The code of a Web service is stored in a file with .asmx extension, to distinguish between .NET Web services and ordinary ASP pages. The URI of the .asmx file can be considered to be a virtual path to an ASP.NET application. The .asmx file contains a separate Web solution or part of an existing solution. An example will be given a little later. In Microsoft Visual Studio.NET, Web services can be developed in C#, Microsoft Visual Basic.NET or Managed C++. The .asmx file contains a directive to indicate the implementation language of the Web service code. Slide 17. The infrastructure of a Web service and a high-level protocol to be performed in relation to a Web service, illustrated by this picture, is as follows. The first stage of working with Web service is discovery. This stage is based on Microsoft’s DISCO technology for discovering, or searching, Web services. In particular, this technology enables dynamic discovery of Web services using a special .vsdisco XML configuration file, with the dynamicDiscovery tag indicated. The server’s response to the .vsdisco request is another XML configuration file with the .disco extension. For the DISCO technology to be applicable to a Web service, the .disco file should be placed into the same directory as the Web service’s .asmx file. The .disco file contains a reference to the .wsdl XML file – a specification of the interface of the Web service. That information can be received by the Web client by the WSDL (Web Service Description Language) request that results in passing the WSDL description of the Web server’s interface to the client in the .wsdl file. Finally, the Web client can get the .asmx file of the Web service, using ASMX request. Slide 18. Code and syntax of a Web service is very simple. First, the WebService directive is used as a compiler setting to indicate to ASP.NET that the code being compiled is that of a Web service. The directive contains the name of the Web service implementation language and the name of the class to define the Web service. Second, the WebMethod attribute in the source code marks a method to be a Web service public method. In other words, this attribute is a kind of extension of public access to the whole Web. The implementation code of the Web service is provided in the language indicated in the WebService directive. Please note the using System,Web.Services statement. Slide 19. This slide demonstrates a simple example of a full .asmx file with implementation of a Web service to subtract two given integer numbers. Please note that the class contains two methods but one method of them only is marked as a Web method, so it is the Subtract method only that is accessible to the users of the Web service. Slide 20. The System,Web.Services namespace contains the classes supporting the development of Web services: - WebService is the base class for Web services. It provides base functionality for a Web service, e.g., WebService.Session; - WebServiceAttribute is an optional attribute class to contain some extra information for the Web service being defined; - WebMethodAttribute is the class for the WebMethod attribute that marks a method to be a publicly available method of a Web service. Slide 21. - WebServiceBindingAttribute contains the WSDL (Web Services Definition Language) interface of the Web service. It is actually a set of operations defined on the Web service; - WebServicesConfiguration contains configuration information on the Web service. It corresponds to the contents of the <webservices> section in the Web.config file; - WebServicesConfigurationSectionHandler - contains the definition of a Web services configuration section handler. Slide 22. Publishing a Web service is exposing the Web service and its methods. It contains the following steps: - Creating proxy and assembly for the Web service; proxy is generated by the WSDL tool; - Encouraging developers to use the Web service: publishing the WSDL contract and HTML description of the Web service. Web service clients can be Web applications and browsers. Slide 23. Discovering (or searching) the Web service can be performed either using the DISCO technology, or by a more general mechanism of using UDDI (Universal Discovery, Description, and Integration). DISCO is a technology developed and patented by Microsoft for publishing and discovering XML Web services. It is relied on XML-based .vsdisco files. A .vsdisco file contains references to resources for extracting WSDL. It is stored in the root directory of the Web service. Access to this file is enabled via URL and dynamically found document. Starting the discovery is performed by the Disco tool. The .vsdisco file for searching a Web service is automatically created by Visual Studio.NET . A more general way for searching Web services by using more advanced requests is using UDDI (Universal Discovery, Description, and Integration) - a kind of a a global repository for Web services. Slide 24. The Disco utility is used for discovering Web services. It is part of the .NET Framework. Example of its call from command line is given on the slide. In more detail, the utility creates the results.discomap XML file with a list of all Web services discovered at the specified location. Slide 25. Another .NET Framework utility – WSDL is a tool for Web services description. It creates a client proxy class to work with the Web service. An example of its command-line call is given on the slide. Its input arguments are the URL of the WSDL file, the proxy definition language and the protocol, and also the user name and password. The output is a source file in the given language containing the proxy class, and the code for networking call and marshalling. Slide 25. The fourth part of the section devoted to Web services has the following plan: - Application model - Calling Web services - Consuming Web services - Web Services Description Language (WSDL) . Slide 26. The application model illustrated by this scheme works as follows. The Web client application developer uses an .aspx file that contains information resulting from filling out a Web form to call the Web service application located on the Web Server 2 on the scheme. The Web service application on a Web Server 2 it its turn can consume another Web service application located on Web server 1. For calling it, a proxy class is used, referring to the .asmx source file of the other Web server application. Slide 27. Consuming Web services is based on the fact that Web services are addressable by URLs. So, to activate a Web service at the protocol layer, one should send an HTTP request. The Web protocols used for accessing a Web service are as follows: - HTTP-GET - indicated the name and the arguments of a method indicated in the URL; - HTTP-POST - the name and the arguments of the method are in the POST body; - HTTP-SOAP – a request to the Web service is sent as a SOAP envelope. The SOAP (Simple Object Access Protocol) provides de-facto standard for XML grammar to address Web services and to return their results. Examples of SOAP requests will be given later. Slide 28. The home task to this lecture is as follows. Please compare .NET Web services and Java functionality to support Web programming (servlets, Java Server Pages, etc.) . course for 4 th year students Comments to the presentations Prof. Vladimir O. Safonov, St. Petersburg University Email: v_o_safonov@mail.ru WWW: http://user.rol.ru/~vsafonov Lecture 10. Slide. services contracts should be followed, that should be explicitly specified in some way. Slide 10. The second part of the lecture is devoted to architectural concepts of .NET supporting Web services