Chapter 6 Services Part 1 Introduction to Services Advanced Programming Using Visual Basic 2008 First There Were Web Services A class that can be compiled and stored on the Web for an application to use Instantiate the class from Windows or Web applications A Web/Windows application consumes (uses) the service Instantiate means to create an run time instance of the class an object The object resides in volatile memory 2 Web Services as Components They are like components that have been created for multi-tier applications They are made available via the Web Can be publicly available Can be privately available on a intranet Can have a cost associated with them 3 1
Integral Technologies Web services employ additional services/technologies XML extensible markup language SOAP simple object access protocol WSDL web service definition language UDDI Universal description, discovery, and integration.net handles many of the details if you develop with Visual Studio 4 History of Web Services They are not a Microsoft technology See: From EDI To XML And UDDI: A Brief History Of Web Services at http://www.informationweek.com/news/so ftware/development/showarticle.jhtml?ar ticleid=6506480 5 XML Web Services Many Web services use XML Thus they are XML Web Services Not all Web Services use XML Web Services can be created using many languages/platforms 6 2
After Web Services At Microsoft, Inc. Windows Communication Foundation Microsoft s technology for communicating between applications Broader than Web Services Supported in VS 2008 Web services are also supported in VS 2008 A Word from Microsoft Windows Communication Foundation (WCF) is Microsoft s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments. http://msdn.microsoft.com/en-us/library/ms735119.aspx 3
Communication and Services - Concepts message SOAP response Application running Service running (might itself be a service) http://msdn.microsoft.com/en-us/library/ms731082.aspx Messaging and Endpoints - Messaging Model: message-based communication anything that can be modeled as a message (HTTP request, e.g.) can be represented in a uniform way in the programming model unified API across different transport mechanisms Clients: applications that initiate communication Services: applications that wait for clients to communicate with them and then respond to that communication single application can act as both a client and a service Messages are sent between endpoints Messaging and Endpoints - Endpoints Endpoints: places where messages are sent or received (or both) and they define all the information required for the message exchange client app must generate an endpoint compatible with target service's s endpoint endpoint describes in a standards-based way where messages should be sent, how they should be sent, and what the messages should look like services expose the requirements as metadata so that client apps can be built 4
More Acronyms Pertinent to Services Universal Resource Identifier (URI) uniquely identifies a resource on the Web More generic term than URL URI is preferred to URL for technical specifications i.e., URL is Old Hat now Each Web project must have a target namespace Default: http://tempuri.org Default OK for development but not for production URI of a site you control for production See http://www.w3.org/addressing/ 13 Alphabet Soup Technologies (continued) Services employ additional services/technologies XML extensible markup language SOAP simple object access protocol WSDL web service definition language UDDI Universal description, discovery, and integration.net handles many of the details if you develop with Visual Sudio 14 XML- Extensible Markup Language Tags mark up data according to what type of data it is e.g. <street address>4292 Eagle Ridge</street address> Facilitates data transfer across platforms Transmitted in text format with markup Text data can pass through firewalls, whereas binary data often is not allowed 15 5
SOAP - Simple Object Access Protocol Standard for sending data Protocol for handling requests and responses that includes class names, method names, and parameters Works with XML Does not include a specific protocol for transporting response/request packets Transport protocol is usually HTTP 16 WCF s WS Discovery Provides search mechanism for available services Directory service for services The Add Web Reference dialog in Visual Studio IDE allows you to search for an existing service Can access UDDI registries- Universal Description, Discovery, and Integration UDDI directory service for web services 17 WSDL- Web Services Description Language Controls format of calls to methods in Web Services Contains information (metadata) about names of the methods parameters that can be passed values that are returned from the functions 18 6
Create a WCF Service Adapted from Bradley & Millspaugh pp. 252-256 Create a WCF Service- 1 Open Visual Studio File New Project WCF Service Develop locally on usb or desktop or at home on file space When complete: publish to icnn web folder icnn is a subfolder of your class web folder, nn is the next number in the sequence 20 Create a WCF Service -2 Name the WCF service WCFService1 (the default name) 21 7
Create a WCF Service -3 Files created include.vb,.svc,.config types No user interface Why? You are designing a component for others to use and incorporate into their interface 22 Create a WCF Service -4 Rename the service (in Solution Explorer) Change Service1.svc to FirstSvc.svc Use the properties window Show all files in Solution Explorer Doubleclick to open FirstSvc.svc.vb 23 Create a WCF Service -5 With FirstSvc.svc.vb open Change the name of the Public Class to FirstSvc Change Implements to IFirstSvc Rename IService.vb to IFirstSvc.vb Open web.config Change references to Service1 and IService1 to FirstSvc and IFirstSvc 24 8
Create a WCF Service -6 Create Operation Contracts for two Public methods First one is a HelloWorld message function Open IFirstSvc.vb and add the HelloWorld contract where you see the comment TODO <OperationContract()> _ Function HelloWorld() As String 25 Create a WCF Service -7 First one is a contract for an ExtendedPrice calculator function Open IFirstSvc.vb and add the ExtendedPrice contract <OperationContract()> _ Function ExtendedPrice(ByVal Price As Decimal, ByVal Quantity As Integer) As Decimal Delete the other operation contracts that are provided as part of the default setup 26 Additional Info From Microsoft Getting Started Tutorial http://msdn.microsoft.com/enus/library/ms734712(v=vs.90).aspx When creating a basic WCF service first task is to define a contract Contract states what operations the service supports operation = Web service method 27 9
Additional Info From Microsoft (cont) Create a contract by defining a Visual Basic (VB) interface Each interface method is a specific service operation 28 Additional Info From Microsoft (cont) Each interface must have the ServiceContractAttribute applied to it <ServiceContract()> _ Public Interface IFirstSvc Each operation must have the OperationContractAttribute applied to it <OperationContract()> _ Function functionname(arguments) As type If method does not have the OperationContractAttribute, the method is not exposed 29 Create a WCF Service -8 Add contracts for calculator operations (as shown in Microsoft tutorial) <OperationContract()> _ Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double 30 10
Create a WCF Service -9 Code the service methods Open FirstSvc.svc.vb. Code as shown. Public Sub New End Sub Public Function HelloWorld() As String _ Implements IHWSvc.HelloWorld Return Hello World! Make your string a little bit different and unique to you End Function 31 Create a WCF Service -10 Continue. Code as shown. Public Function ExtendedPrice(ByVal Price As Decimal, ByVal Quantity As Integer) As Decimal _ Implements IFirstSvc.ExtendedPrice Return Price * Quantity End Function 32 Additional Info from Microsoft Creating a WCF service First you createf the contracts Contracts are defined using an interface The next step is to implement the interface Create a class FirstSvc that implements the user-defined IFirstSvc interface 33 11
Create a WCF Service -11 Continue. Code as shown. Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements IFirstSvc.Add Dim result As Double = n1 + n2 Return result End Function Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements IFirstSvc.Subtract Dim result As Double = n1 - n2 Return result End Function 34 Create a WCF Service -12 Continue. Code as shown. Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements IFirstSvc.Multiply Dim result As Double = n1 * n2 Return result End Function Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements IFirstSvc.Divide Dim result As Double = n1 / n2 Return result End Function 35 Create a WCF Service -13 Save all files Now the service contract is created and implemented. Build the solution to ensure there are no compilation errors Note and fix errors 36 12
Test the Service Run w/o debugging 37 Test the Service Read the message Click on the link Working! at some level 13
Create a WCF Service -14 Your turn add an operation Open IFirstSvc.vb and write the operation contract Then open FirstSvc.svc.vb and create the function 40 Create a WCF Service -15 Save and Build or Rebuild Run w/o debugging Click on the link Read the message Click on the link Scroll down to see references to your new function 41 Publish Publish your service to the icnn subweb of your subweb on the class server From the Build menu in Visual Studio, choose Publish enter the url http://cot-itec4339-02.cougarnet.uh.edu/fall2010/lastnamef/icnn 42 14
Call the Service to Check the Publish Operation Enter the url into a browser: http://cot-itec4339-02.cougarnet.uh.edu/spring10/lastnamef/icnn/firstsvc.svc End of Chapter 6 Services Part 1 Introduction to Services Advanced Programming Using Visual Basic 2008 15