260 likes | 390 Views
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.
E N D
Web Services, Web FormsAnd .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 Services Framework CLR System Services What is .Net • Very General Overview • CLR: common type system; deep multilanguage integration • Easier deployment model and versioning
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
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
Working with Relational Data • The Command Object • DataReader • DataSet • Updating data • Transactions
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*******";}
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
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
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
<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
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
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();
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
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
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
[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) { }
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
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"><partname="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> <inputmessage="s0:ShowAllFeedbackStatsSoapIn" /> <outputmessage="s0:ShowAllFeedbackStatsSoapOut" /> </operation> • Binding • Service
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
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…
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
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 Access and Discovery with VS .Net
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
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));
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 DataReader drResultSet=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();