Web services

28
WRITING AND WORKING WITH SERVICES

description

 

Transcript of Web services

Page 1: Web services

WRITING AND WORKING WITH

SERVICES

Page 2: Web services

Web Service There are things applications need very often. So why

make these over and over again?

Web services can offer application-components like: currency conversion, weather reports, or even language translation as services.

Unlike traditional client/server models, such as a Web server/Web page system, Web services do not provide the user with a GUI.

Web services instead share business logic, data and processes through a programmatic interface across a network.

Developers can then add the Web service to a GUI (such as a Web page or an executable program) to offer specific functionality to users.

Page 3: Web services

ASP.NET model of Web Services

There are two primary ways to create services in ASP.NET.

Web services based on the ASP.NET (.asmx) model.

Microsoft Windows Communication Foundation (WCF) model

Page 4: Web services

XML Web Service (.asmx) Model

This is a familiar ASP.NET programming experience for services that are meant to be exclusively bound to Hypertext Transfer Protocol (HTTP) and hosted by Microsoft Internet Information Services (IIS) and ASP.NET.

Page 5: Web services

Web Service

The term Web services describes a standardized way of integrating Web-based applications using the XML, SOAP, WSDL and UDDI open standards over an Internet protocol backbone.

XML is used to tag the data, SOAP is used to transfer the data, WSDL is used for describing the services available and UDDI is used for listing what services are available.

Used primarily as a means for businesses to communicate with each other and with clients, Web services allow organizations to communicate data without intimate knowledge of each other's IT systems

Page 6: Web services

SOAP

SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on Extensible Markup Language (XML) for its message format, and usually relies on other Application Layer protocols, most notably Hypertext Transfer Protocol (HTTP) and Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.

Page 7: Web services

WSDL

WSDL stands for Web Services Description Language

WSDL is an XML-based language for locating and describing Web services.

An XML-formatted language used to describe a Web service's capabilities as collections of communication endpoints capable of exchanging messages.

WSDL is an integral part of UDDI, an XML-based worldwide business registry.

Page 8: Web services

XML Web Services ModelASP.NET wrap the web service code as a proxy object. This object will know how to expose your Web service. This includes deserializing SOAP requests, executing your .NET Framework code, and serializing your response to be sent back to the requesting client as a SOAP message.

Page 9: Web services

Creating an ASP.NET Web Service

An ASP.NET XML Web service is a class you write that inherits from the class System.Web .Services.WebService.

Create a Web service project through the Add New Project ->ASP.NET Web Service application.

This generates a separate project for your Web service application that has a structure similar to a Web site. This includes a folder for App_Data, a Web.config file, and related elements.

Page 10: Web services

Creating an ASP.NET Web Service

Like a Web page, Web services are exposed through Uniform Resource Locators (URLs).

This means your domain name followed by a page name, as in http://MyDomain/MyService.asmx.

Web service like a class that only exposes methods. Each Web service can expose multiple methods.

The page for an XML Web service is defined by the .asmx file. This file is nothing more than a simple text file that is used as a pointer to the code of your Web service.

@ WebService directive points to the actual code for the Web service.

Page 11: Web services

The WebService Class This class can be used to provide information about

your Web service. This information is used by clients that wish to reference the Web service.

You can provide both a namespace and a description of your Web service by applying the WebService attribute and parameters to your class.

The description parameter is simply text you write to identify the high-level intent of your Web service.

The namespace parameter sets the namespace of your Web service. This should be a domain name under your control. Visual Studio uses the tempuri.org namespace as filler until you define your actual namespace.

Page 12: Web services

The WebMethod Class You apply this attribute to any public method in

your Web service class you wish to expose as part of your service.

The WebMethod attribute class has a number of constructors used for various groups of parameter values.1. enableSessionState2. transactionOption3. cacheDuration4. bufferResponse

You can also use the Serializable attribute class to tag class outside your Web service. This ensures any public members of the class can be serialized by ASP.NET.

Page 13: Web services

Consuming an ASP.NET Web Service

The first step is setting a Web reference from your Web site to the given service. You do this by right-clicking your project file and choosing Set Web Reference.

This opens the Add Web Reference dialog box. Here, you define the URL of your service, select the given service (.asmx file), and set a name for the reference. This name will be used by the generated proxy class to define the namespace for accessing your service.

Page 14: Web services

Windows Communication Foundation (WCF) Model

This model allows developers to write services that can be configured to work with a variety of hosts, protocols, and clients. For example, you might want to write a service that is accessed over Transmission Control Protocol (TCP) instead of HTTP.

If that same service code needed to be called over both HTTP and TCP, you had to write and host it twice. This is one of the many problems WCF is meant to solve.

Page 15: Web services

WCF Model WCF is a unifying programming model.

It is meant to define a singular way for writing services and thereby unify things like Web services (.asmx), .NET Remoting, Message Queue (MSMQ), Enterprise Services (COM+), and Web Services Enhancements (WSE).

It does not replace these technologies on an individual basis. Instead, it provides a single programming model that you can use to take advantage of all of these items at once.

With WCF, you can create a single service that can be exposed as HTTP, TCP, named pipes, and so on.

You also have multiple hosting options.

Page 16: Web services

Difference between WCF and Web service

Page 17: Web services

WCF EndPoints WCF Service is a program that exposes a

collection of Endpoints. Each Endpoint is a portal for

communicating with the world. All the WCF communications are take

place through end point. End point consists of three components.

Address Binding Contract

Page 18: Web services

Address

Basically URL, specifies where this WCF service is hosted .

Client will use this url to connect to the service.

http://localhost:8090/MyService/SimpleCalculator.svc

Page 19: Web services

Binding Binding will describes how client will communicate with

service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.

The following table gives some list of protocols supported by WCF binding.

Page 20: Web services

Contract Collection of operation that specifies what the

endpoint will communicate with outside world.

Usually name of the Interface will be mentioned in the Contract, so the client application will be aware of the operations which are exposed to the client.

There are multiple contract types in WCF, including service contract, operation contract, message contract, fault contract (for error handling), and data contract.

These contracts work together to indicate to the client code consuming the WCF service how it should define communication messages

Page 21: Web services
Page 22: Web services

The Layers of the WCF Architecture

Page 23: Web services

Creating a WCF Service with ASP.NET

Creating and consuming WCF services follow a standard set of programming tasks. You follow these steps every time you wish to create and consume a new WCF service:

1. Define the service contract.2. Implement (or write) the service contract.3. Configure a service endpoint(s).4. Host the service in an application.5. Reference and call the service from a client

application.

Page 24: Web services

Creating a WCF Service with ASP.NET

In WCF programming, you first define an interface and decorate that interface with a number of attributes.

These WCF attribute classes are found in the System.ServiceModel namespace.

These attribute classes are used to define the details of the contract that your service will have with calling clients.

Page 25: Web services

The key WCF attribute classes

ServiceContract : The ServiceContract attribute class has parameters for setting things like whether the service requires a session (SessionMode), the namespace, the name of the contract, the return contract on a two-way contract (CallbackContract), and more.

OperationContract : attribute class to set things like whether the contract does not return a reply (IsOneWay), the message-level security (ProtectionLevel), or whether the method supports asynchronous calls (AsyncPattern).

DataContract : The DataContract attribute class is used to mark types you write (classes, enumerations, structures)

DataMembers : The DataMember attribute class is used to mark individual fields and properties that you want to serialize. You use this class in conjunction with the Data-Contract class.

Page 26: Web services

The WCF Service Application

Visual Studio and ASP.NET define the WCF Service Application project template. This template defines a Web project that serves to host the WCF service. This project contains a reference to System.ServiceModel.dll, which contains the WCF classes. Creating a new instance of this project template will also generate a default service (Service1.svc) and a related contract file IService1.vb or .cs).

Page 27: Web services

The WCF Service ApplicationFinally, a WCF Service application is automatically configured to be hosted in IIS and expose a standard HTTP endpoint. This information can be found inside the <system.servicemodel> section of the Web.config file.

<system.serviceModel><services>

<service name="NorthwindServices.Service1"behaviorConfiguration="NorthwindServices.Service1Behavior"><endpoint address="" binding="wsHttpBinding"contract="NorthwindServices.IService1"><identity><dns value="localhost"/></identity></endpoint><endpoint address="mex" binding="mexHttpBinding"

contract="IMetadataExchange"/></service></services>

Page 28: Web services

The WCF Service Application<behaviors>

<serviceBehaviors><behavior name="NorthwindServices.Service1Behavior"><!-- to avoid disclosing metadata information, set the

value below to falseand remove the metadata endpoint above before

deployment --><serviceMetadata httpGetEnabled="true"/><!-- To receive exception details in faults for debugging

purposes, set thevalue below to true. Set to false before deployment to

avoid disclosingexception information --><serviceDebug includeExceptionDetailInFaults="false"/></behavior>

</serviceBehaviors>

</behaviors></system.serviceModel>