26
1 Web Services, Web Forms And .Net Useful cocktail terms essential to your project

Web Services, Web Forms And .Net

  • Upload
    duman

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

Web Services, Web Forms And .Net. Useful cocktail terms essential to your project. Outline. Overview of .Net Web Applications Web Forms (slides 5—11) Data access Security Session and Application State Web Services(slides 12—20) Namespaces Services Service Discovery and Clients. Apps. - PowerPoint PPT Presentation

Citation preview

Page 1: Web Services, Web Forms And .Net

1

Web Services, Web FormsAnd .Net

Useful cocktail terms essential to your project

Page 2: Web Services, Web Forms And .Net

2

Outline

• Overview of .Net• Web Applications

– Web Forms (slides 5—11)• Data access• Security• Session and Application State

– Web Services(slides 12—20)• Namespaces• Services• Service Discovery and Clients

Page 3: Web Services, Web Forms And .Net

3

What is .Net

• Very General Overview

– CLR: common type system; deep multilanguage integration

– Easier deployment model and versioning

System Services

CLR

Services Framework

Apps

Page 4: Web Services, Web Forms And .Net

4

Web Applications

• A web application is a set of URLs rooted at the same base URL: ASP+ programming model

• Based on assemblies, which makes deployment and live updates easy

• Two programming models based on ASP+– Web Forms– Web Services

Page 5: Web Services, Web Forms And .Net

5

Web Forms

• VB forms in web development; ASP & HTML syntax. WYSIWYG page layout

• I.e. ASP .Net Pages with Server Controls– Web Form Controls:

• HTML analogs: listboxes, text boxes, buttons

• Additional: Calendar, Ad Rotator– These controls are client sensitive and serve whatever the

client browser can handle

Page 6: Web Services, Web Forms And .Net

6

Working with Relational Data

• The Command Object

• DataReader

• DataSet

• Updating data

• Transactions

Page 7: Web Services, Web Forms And .Net

7

SqlConnection mysqlConnection =new SqlConnection(conString);// declare sql command by using a object named mycommandSqlCommand mysqlCommand = new SqlCommand();// using sql transaction classSqlTransaction myTrans;

// Open the connection.mysqlConnection.Open();// Assign the connection property.mysqlCommand.Connection = mysqlConnection;// Begin the transaction.myTrans = myConnection.BeginTransaction();// Assign transaction object for a pending local transactionmysqlCommand.Transaction = myTrans;try{ 

// Insert the user record.mysqlCommand.CommandText = "Insert into useraccount VALUES ('"+uname+"','"+ufamily+"')";mysqlCommand.ExecuteNonQuery();// pass the data .transaction complete myTrans.Commit();return "transaction completed";

}catch(Exception e){// transaction cancelmyTrans.Rollback();return e.ToString()+"********** transaction abort*******";}

Page 8: Web Services, Web Forms And .Net

8

Web Applications—other useful details

• Web Applications are stateless:– Application State– Session State

• Global.asax: a global file per application– Directive, Code Declaration, Application/Session

Scoped variables– Processing of Application Events (e.g

Application_OnStart)– Sample uses: adding a footer to every page,

personalization of pages based on the identity of the user, catching unhandled exceptions

• Web.Config

Page 9: Web Services, Web Forms And .Net

9

Authentication and Security• Forms Authentication-Cookies and HTML

forms

• Passport Authentication

• Windows Authentication

• Change the mode attribute of the authentication element in the configuration file: web.config– Windows,Forms,Passport, None

Page 10: Web Services, Web Forms And .Net

10

• Once you set the mode to Forms, the <authentication> will have a <forms> child

• use it to set various attributes, such as a login page, the name of the cookie to be checked, how long a cookie is good

• http://www.15seconds.com/issue/020220.htm

Page 11: Web Services, Web Forms And .Net

11

<configuration>  <system.web>

<authentication mode="Forms"> //set the cookies name, path and protection

<forms name=“cookie-name" path="/" loginUrl="login.aspx" protection="All" timeout="30“></forms>

</authentication> </system.web>

//deny unauthenticated users<location path=“MyApp/RestricedPage.aspx">

 <system.web> <authorization> <deny users="?" /> </authorization> </system.web>

</location></configuration>

Sample web.config file

Page 12: Web Services, Web Forms And .Net

12

Cookies

• A text file that gets stored on the client machine.

• http://www.eggheadcafe.com/articles/20010925.asp

• Upon request the ASP .Net application checks for a cookie; if it’s there it serves the requested information

Page 13: Web Services, Web Forms And .Net

13

using System.Web.Security

//check credentials against web.config

DataReader dr=cmdCommand.ExecuteReader(“select * from users where pwd=…”);

//if such a user was found

if(dr.Read()){//Grant the user access to the page they requested

//paramaters:username and whether the cookie persists between sessions

FormsAuthentication.RedirectFromLoginPage(txtLogin.Text,true);}else{lblError.Text = "Please check your credentials";}

//don’t forget to close the data reader, command,etc.

You can create a log-out button with the following event handler:

//scrap that cookie

FormsAuthentication.SignOut();

Page 14: Web Services, Web Forms And .Net

14

Web Services• What is it:

– An application delivered over Internet Standards; I.e. a URL that returns information to clients

– Model independent, unlike other component technologies like IIOP, RMI and DCOM

• A contract with the web service is specified through the messages it understands and returns; the only thing it relies on is Web standards

Page 15: Web Services, Web Forms And .Net

15

XML and SOAP

• Standard way to represent data, commands and typed data

• SOAP (Simple Object Access Protocol)– Industry standard for data and command

representation in XML

Page 16: Web Services, Web Forms And .Net

16

How do I create a Web Service with .Net

• Functionality is implemented in a code behind file—asmx.vb or asmx.cs extension. This is what you see when you view the .asmx file– Directives: <%@WebService Language=“C#” Codebehind=“….” Class=“..”%>

• WebService Base Class– Access for common ASP Objects, such as the request, application and session objects

• WebMethod Attribute (method needs to be Public)– Description: analogous to a comment– Name: the name of the class implementing the service– Namespace: unique names for your XML elements

• How do client applications differentiate between different services that use the same method name?

– A set of other properties:BufferResponse, CacheDuration, read yourself

Page 17: Web Services, Web Forms And .Net

17

[WebService (Namespace="http://cs.washington.edu/cse444au02/webservices/")]

public class FeedbackService : System.Web.Services.WebService {

/* WEBMETHOD: * Shows Statistics for each homework */

[WebMethod (Description="A method that gives the minimum, average and maximum time to complete a homework")]

public Stats ShowFeedbackStats(int nHomeworkNum) { }

Page 18: Web Services, Web Forms And .Net

18

Service Help Page• Default page when service is invoked

without a query string

• Through it you can invoke a method using HTTP Post (I.e. you can’t invoke methods that use complex data types as parameters)

• WSDL(Web Service Description)– Contract definition--XML

Page 19: Web Services, Web Forms And .Net

19

The WSDL file• Description of the service• Has the following elements:

– Types: XML schema for the message– Message: 1 for request, 1 for response

- <message name="ShowAllFeedbackStatsSoapIn">  <part name="parameters" element="s0:ShowAllFeedbackStats" />   </message>

– PortType: describe the messages- <portType name="FeedbackServiceSoap">-

- <operation name="ShowAllFeedbackStats">  <documentation>A method that gives the minimum, average and maximum

time to complete each homework</documentation>   <input message="s0:ShowAllFeedbackStatsSoapIn" />   <output message="s0:ShowAllFeedbackStatsSoapOut" />

  </operation>

– Binding– Service

Page 20: Web Services, Web Forms And .Net

20

Web Service Discovery

• How do I know what services are out there?• If you decide to make your service publicly

available (which we won’t) you need to register it with an XML Services directory—UDDI (Universal Description, Discovery and Integration)

• DISCO Static and dynamic discovery– .Disco– .VDISCO

Page 21: Web Services, Web Forms And .Net

21

Web Service Clients

• A program that is the end user

• A Web Form

• Another Web Service

• Communicate with the web service via proxy: not easy but VS does this for you…

Page 22: Web Services, Web Forms And .Net

22

• Enter the URL for the Web Service's discovery file in the text box and click on the arrow to the left of the box.

• Web Reference (Add Web Reference dialog box)—you can chose between browsing the UDDI, the local server directory, etc

Page 23: Web Services, Web Forms And .Net

23

Access and Discovery with VS .Net

• Once you add a web reference, the wsdl file is downloaded and a proxy class is generated

• You can’t see the proxy file but you can add it to your project manually

Page 24: Web Services, Web Forms And .Net

24

Using the Proxy Class

• There is a lot more to be said about the proxy class…but not here

• Calling a method: create an instance of the proxy class and invoke the method with the specified parameters…Done

Page 25: Web Services, Web Forms And .Net

25

using CSE444Feedback.edu.washington.cs.iinetsrv;

if (hwkNum.Text.Length==0) return;

//create the object FeedbackService objFeedback=new FeedbackService();

//invoke the method Stats st=objFeedback.ShowFeedbackStats(Convert.ToInt32(hwkNum.Text));

Page 26: Web Services, Web Forms And .Net

26

try{ _con = new SqlConnection (strConnection ); cmdSql= new SqlCommand(); cmdSql.Connection=_con ; }catch (Exception e){ Console.WriteLine("Failure "+e.ToString()); }

/* create the query you want to issue * and associate it with the command object*/ string query="SELECT * FROM Movies"; cmdSql.CommandText=query;

/* Open the connection and issue the query */ _con.Open(); /*=====================================================*///example of a DataReaderdrResultSet=cmdSql.ExecuteReader(); while(drResultSet.Read()){ …..

} /*====================================================*/sqlDataAdapter=new SqlDataAdapter(_sql,con); sqlDataAdapter.Fill(myDataSet, "Feedback_public");

/* hwkStats is the name of the DataGrid on * the ASP page: connect the grid to the DataSet */ hwkStats.DataSource =myDataSet.Tables[0].DefaultView; hwkStats.DataBind();