Title: Developing Web Services and Web Applications with C
1Developing Web Services and Web Applications with
C and Visual Studio.Net
- Laboratório de Engenharia de Software
- João Pascoal Faria
- Outubro de 2006
2Contents
- Introduction to the .Net Framework ver
CSharp.ppt - Introduction to the C language - ver CSharp.ppt
- Introduction to Web Services
- Creating a Web Service in Visual Studio .Net
- Creating a Web Application that consumes a Web
Service in Visual Studio .Net - Data access with ADO.Net
3Introduction to Web Services
4Defining Web Services
- A web service is a chunk of functionality that
can be accessed using web technology - Across an intranet or the Internet
- Usually
- Whats accessed is a method in a language such as
Java or Visual Basic.NET - The web technology used is the Simple Object
Access Protocol (SOAP) carried over HTTP - Both the .NET and Java camps have endorsed web
services
5Key Web Services Technologies
- Web Services Description Language (WSDL)
- An IDL for web services
- Simple Object Access Protocol (SOAP)
- An XML-based protocol for web services
- Universal Description, Discovery, and Integration
(UDDI) - A directory service for web services
6Illustrating Web Services Technologies
UDDI Registry
Internet
UDDI Registry
Application
Application
Application
WSDL Interface
7XML Web Services Protocol Stack
Internet Protocols
Directory of services UDDI
Service discovery DISCO
Service descriptions WSDL
Service interactions SOAP
Universal type system XSD
Universal data format XML
Ubiquitous communication Internet
8Applying Web Services (1)
- Connecting clients to Internet applications
- One example is Microsofts .NET My Services
- B2B integration on the Internet
- The next generation of EDI
- Enterprise application integration (EAI) on
intranets - A standard approach to connecting all applications
9Applying Web Services (2)
Internet
10Describing Web Services WSDL
- Created by Microsoft and IBM
- Handed to the World Wide Web Consortium (W3C)
- Endorsed by everybody
- Microsoft, IBM, Sun, Oracle, BEA, more
11Accessing Web Services SOAP
- The Simple Object Access Protocol (SOAP) allows
invoking operations - SOAP defines an XML-based format for parameters
- SOAP requests/responses can ride on
- HTTP
- The most common case today
- SMTP or MSMQ
- For loosely-coupled interactions
- Other protocols
12What SOAP Provides
- Industry agreement
- Its endorsed by Microsoft, IBM, Sun, Oracle,
BEA, Iona, and others - Object model independence
- SOAP isnt tied to any operating system,
programming language, or object model - Support for diverse protocols
- Synchronous and asynchronous
13Illustrating A SOAP Message
Envelope
Headers
Body
Method
Parameters
14An Example SOAP Request (1)
- POST /AccountAccess HTTP/1.1
- Host www.qwickbank.com
- Content-Type text/xml charsetutf-8
- Content-Length 305
- SOAPAction
- ltSOAP-ENVEnvelope
- xmlnsSOAP-ENV
- "http//schemas.xmlsoap.org/soap/envelope/"
- SOAP-ENVencodingStyle
- "http//schemas.xmlsoap.org/soap/encoding/"gt
15An Example SOAP Request (2)
- ltSOAP-ENVBodygt
- ltmGetBalance
- xmlnsm"http//www.qwickbank.com/bank"gt
- ltAccountgt729-1269-4785lt/Accountgt
- lt/mGetBalancegt
- lt/SOAP-ENVBody gt
- lt/SOAP-ENVEnvelopegt
16An Example SOAP Response (1)
- HTTP/1.1 200 OK
- Content-Type text/xml charset"utf-8"
- Content-Length 304
- ltSOAP-ENVEnvelope
- xmlnsSOAP-ENV
- "http//schemas.xmlsoap.org/soap/envelope/"
- SOAP-ENVencodingStyle
- "http//schemas.xmlsoap.org/soap/encoding/"gt
17An Example SOAP Response (2)
- ltSOAP-ENVBodygt
- ltmGetBalanceResponse
- xmlnsm"http//www.qwickbank.com/bank"gt
- ltBalancegt3,822.55lt/Balancegt
- lt/mGetBalanceResponsegt
- lt/SOAP-ENVBody gt
- lt/SOAP-ENVEnvelopegt
18Discovering Web Services UDDI
- Universal Description, Discovery, and Integration
(UDDI) provides a specialized directory service
for discovering compatible web services - It uses XML
- Its endorsed by Microsoft, IBM, Ariba, Oracle,
Sun, BEA, and more - An organization can create an XML-defined
business registration - Which can contain information about various
available web services
19Illustrating A UDDI Business Registration
20Creating a Web Service in Visual Studio .Net
21ASP.NET
- Active Server Pages for the .Net Platform
- Allows creating two kinds of applications
- Browser applications
- Rely on .aspx pages
- Web services applications
- Rely on .asmx pages
22Illustrating a Web Services Application
IIS
class X WebMethod public int Method1()
SOAP
app.asmx
ADO.NET
Common Language Runtime
DBMS
23Exposing Web Services An Example .asmx Page
- lt_at_ WebService Language"c" Class"Compute" gt
- using System.Web.Services
- public class Compute
-
- WebMethod public int Factorial(int f)
-
- int i
- int result 1
- for (i2 iltf i)
- result result i
- return result
-
- WebMethod public double SquareRoot(double s)
-
- return System.Math.Sqrt(s)
-
-
24Controlling XML representation (1)Expose
public class OrderProcessor public void
SubmitOrder(PurchaseOrder order) ... public
class PurchaseOrder public string ShipTo
public string BillTo public string Comment
public Item Items public DateTime
OrderDate
public class OrderProcessor WebMethod
public void SubmitOrder(PurchaseOrder order)
... XmlRoot("Order", Namespace"urnacme.b2b
-schema.v1") public class PurchaseOrder
XmlElement("shipTo") public string ShipTo
XmlElement("billTo") public string BillTo
XmlElement("comment") public string Comment
XmlElement("items") public Item Items
XmlAttribute("date") public DateTime
OrderDate
25Controlling XML representation (2)Consume
Order order new Order() order.ShipTo "Manuel
Costa" order.BillTo "Bill Gates" order.OrderDa
te DateTime.Today OrderProcessor.SubmitOrder(
order)
26Controlling XML representation (3)SOAP request
message
lt?xml version"1.0" encoding"utf-8"?gt ltsoapEnvel
opegt ltsoapBodygt ltSubmitOrdergt ltOrder
date"20010703"gt ltshipTogtManuel
Costalt/shipTogt ltbillTogtBill
Gateslt/billTogt ltcommentgtOvernight
deliverylt/commentgt ltitemsgt
ltproductIdgt17748933lt/productIdgt
ltdescriptiongtDom Perignonlt/descriptiongt
lt/itemsgt lt/Ordergt lt/SubmitOrdergt
lt/soapBodygt lt/soapEnvelopegt
27Creating a ASP.NET Web Service Project
28Changing the Web Service Name
29Accessing the Code Behind
30Understanding the Code Behind
inheritance is optional (useful to create state
full web services)
31Editing the Code Behind
attribute that tells that this method should be
exposed as a web service method
32Understanding the .asmx page
In this case, everything is in the code behind
33Build
34Files Generated
35Testing with a Browser
36Viewing the dynamically generated WSDL (1)
37Viewing the dynamically generated WSDL (2)
38Creating a Web Application that consumes a Web
Service in Visual Studio .Net
39Creating a ASP. NET Web Application
40Adding a Reference to the Web Service
41Viewing the Proxy Class Created Behind the Scenes
42Designing the web page
43Calling the web service
44Running the client application
45Data Access with ADO.Net
46ADO.Net Architecture
SqlClient OleDb etc
47Populating a DataSet from a DataAdapter
OleDb
OleDbConnection nwindConn new
OleDbConnection("ProviderSQLOLEDBData
Sourcelocalhost" "Integrated
SecuritySSPIInitial Catalognorthwind") OleDbCo
mmand selectCMD new OleDbCommand("SELECT
CustomerID, CompanyName FROM Customers",
nwindConn) OleDbDataAdapter custDA new
OleDbDataAdapter() custDA.SelectCommand
selectCMD DataSet custDS new DataSet()
custDA.Fill(custDS, "Customers")
SqlClient
SqlConnection nwindConn new SqlConnection("Data
SourcelocalhostIntegrated SecuritySSPIInitial
Catalognorthwind") SqlCommand selectCMD new
SqlCommand("SELECT CustomerID, CompanyName FROM
Customers", nwindConn) SqlDataAdapter custDA
new SqlDataAdapter() custDA.SelectCommand
selectCMD nwindConn.Open() DataSet custDS
new DataSet() custDA.Fill(custDS, "Customers")
nwindConn.Close()
48Inserting, updating or deleting rows using a
DataTable and a SqlCommand with parameters
// Executa um dado comando com parâmetros
(insert, delete ou update) para todas as linhas
num // dataTable, preenchendo parâmetros com
valores lidos da mesma e devolve nº linhas
afectadas. private int ExecuteNonQuery(System.Data
.DataTable dt, System.Data.SqlClient.SqlCommand
sqlCommand) int rowsAffected 0 try
sqlCommand.Connection.Open() foreach(System
.Data.DataRow row in dt.Rows)
foreach(System.Data.SqlClient.SqlParameter
param in sqlCommand.Parameters) if
(row.IsNull(param.SourceColumn))
param.Value System.DBNull.Value
else param.Value rowparam.SourceColum
n rowsAffected sqlCommand.ExecuteNonQ
uery() sqlCommand.Connection.Close() c
atch(System.Exception e) ExceptionManager.Publ
ish(e) throw return rowsAffected
49Saving changes to a DataTable
// Propaga alterações (update, delete, insert)
num dataTable para a BD usando um dataAdapter //
já configurado public virtual void
SaveChanges(System.Data.SqlClient.SqlDataAdapter
sqlDataAdapter, System.Data.DataTable
dt) try sqlDataAdapter.Update(dt) cat
ch(System.Exception e) ExceptionManager.Publi
sh(e) throw
50Creating DataAdpaters with Visual Studio.Net
- Just drag and drop a table to the application
51Binding DataTables to Data Grids
- Just change a property in the data grid