+ All Categories
Home > Documents > Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and...

Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and...

Date post: 20-Dec-2015
Category:
View: 215 times
Download: 2 times
Share this document with a friend
Popular Tags:
27
Carnegie Mellon Universit y MSCF 1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”
Transcript
Page 1: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 1

C#/.NET

Basics 2

Some code is from “C# in a Nutshell” and “Programming C#”

Page 2: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 2

This week

• Event Handling and delegates

• ASP.NET Web Forms

• ASP.NET Web Services

Page 3: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 3

Event Handling Model

• Delegates listen for the events and call registered handlers

• Each component has a delegate for every event it can raise

• We register a method with the delegate

and the delegate will call the method

asynchronously

Page 4: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 4

Delegates (1)

• A Button, for example, needs to notify some object when it is pushed

• We don’t want to hardwire (in the button) which object to call

• A delegate is a reference type used to encapsulate a method with particular parameter types

Page 5: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 5

Delegate (2)using System;delegate String Foo(String x); // create a delegate class

class Test {

public static void Main() { Foo f = new Foo(ConvertToUpperCase); // create a delegate object String answer = f("abcd"); // call the method in the // object Console.WriteLine(answer); } public static String ConvertToUpperCase(String s) { return s.ToUpper(); }}

Page 6: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 6

Delegate (3)public class Form1 : System.Windows.Forms.Form {

private System.Windows.Forms.Button multiplyButton;

public void foo() { this.multiplyButton = new System.Windows.Forms.Button(); this.multiplyButton.Text = "Multiply";

this.multiplyButton.Click += new System.EventHandler(this.multiplyButton_Click); } private void multiplyButton_Click(object sender, System.EventArgs e)

{textBox3.Clear();string op1Str = op1.Text;string op2Str = op2.Text;

: }

Delegate reference

Delegate

Encapsulatedmethod

Page 7: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 7

Multicast Delegate

using System; // From C# In A Nutshelldelegate void MethodInvoker(); // define delegate class class Test { static void Main() { // create a Test object // and call its constructor new Test(); }

Page 8: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 8

Test() {

MethodInvoker m = null; m += new MethodInvoker(Foo); // overloaded += m += new MethodInvoker(Goo); // delegate holds m(); // pointers to two } // methods

m

MethodInvokervoid Foo()

void Goo()

Page 9: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 9

void Foo() { Console.WriteLine("Foo"); } void Goo() { Console.WriteLine("Goo"); }}

Output:FooGoo

Page 10: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 10

ASP.NET Web Forms (1)

• Web Forms bring rapid appplication development to the web

• Similar technology is available on J2EE platforms (struts, Java Server Faces)

• Drag and drop development for the web tier – write event handlers as in Windows

Forms• User interacts with the sever via a

standard browser

Page 11: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 11

ASP.NET Web Forms (2)

• Web pages are dynamically generated

• Standard HTML is sent to the browser

• Notepad would work but Visual Studio makes life easy

• The user interface code is in an .aspx file

• The logic (C# code) is stored in a separate file (containing event handling code)

Page 12: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 12

ASP.NET Web Forms (3)

• Postback events are handled on the server with an HTTP request. For example, the submit button is clicked.

• Non-postback events are not handled by the server immediately. For example, text is entered into a form or the mouse is moved.

• State is automatically added to an otherwise stateless protocol. .NET maintains the user’s session.

Page 13: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 13

Web Form Life Cycle

• Complicated series of activities similar to what is found in J2EE struts and JSF

• For this class let’s just say that a lot of pre- and post-processing goes on for each web request

Page 14: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 14

Creating A Web Form(1)

• Prerequisites: IIS and Front Page Server Extensions (use Internet Service Manager and right click on the web site/All Tasks/Configure Server Extensions)

• Start/Microsoft Visual Studio .NET/ New Project/Visual C#/ASP.NET Web Application/BinomialTreeWebApp

• Generated code goes into c:\Inetpub\wwwroot\BinomialTreeWebApp

Page 15: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 15

Creating A Web Form(2)

• Two files generated

- The .aspx file holds the HTML

- The aspx.cs file holds the C#

• To see the C# code right click the form and select view code

• Note that you can see the design view or the HTML view (tabs on bottom)

Page 16: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 16

Web Services

“The internet is evolving from a collection of isolated web sites and applications into a general communication bus for distributed applications.”

Pradeep Tapadiya, author of “.NET Programming”

Page 17: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 17

ASP.NET Web Services

0) Check if IIS is running by attempting to visit http://localhost

1) If it's not running click Start/Settings/Control

Panel/Add Remove Programs/

Add Remove Windows Components and

enable IIS.

2) If .NET was installed after IIS reconfigure IIS by running aspnet_regiis.exe /i from a command prompt.

Page 18: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 18

ASP.NET Web Services

Suppose we want to provide a student name

given a student ID.

Page 19: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 19

ASP.NET Server Code <%@ WebService Language="C#" Class="Student.QueryService" %>

// CoolService.asmx

using System.Web.Services;using System.Collections;

namespace Student {

[WebService(Namespace="http://localhost/ACoolQueryService/")]

public class QueryService : WebService {

private static Hashtable nameValuePairs;

Page 20: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 20

static QueryService() {

nameValuePairs = new Hashtable(); nameValuePairs.Add("12345","Moe"); nameValuePairs.Add("01234","Curly Joe"); nameValuePairs.Add("54321","Larry"); }

[WebMethod] public string GetName(string id) { return (string)nameValuePairs[id]; } }}

Page 21: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 21

Create a virtual directory under IIS

• Start• Settings • Control Panel• Administrative Tools• Select Internet Information Services• Expand and select the default web site• Click Action/New/Virtual Directory• Provide a name (ACoolQueryService in this case) and

browse to the directory holding the .asmx file• Select everything but write

Page 22: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 22

Checking the service

• Visit the service with your browser• http://localhost/ACoolQueryService/CoolService.asmx

• HTML is generated that allows you to test the service via standard HTTP

Page 23: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 23

Testing With HTTP Get

Request

GET /ACoolQueryService/CoolService.asmx/GetName?id=string HTTP/1.1 Host: localhost HTTP/1.1 200 OK

Content-Type: text/xml;

charset=utf-8

Content-Length: length

Response

<?xml version="1.0" encoding="utf-8"?>

<string xmlns="http://localhost/ACoolQueryService/">string</string>

Page 24: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 24

Testing with SOAP POST /ACoolQueryService/CoolService.asmx HTTP/1.1 Host:

localhost Content-Type: text/xml; charset=utf-8 Content-Length: length of document SOAPAction: "http://localhost/ACoolQueryService/GetName"

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-

instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

Page 25: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 25

<soap:Body> <GetName xmlns="http://localhost/ACoolQueryService/"> <id>string</id> </GetName> </soap:Body> </soap:Envelope>

Page 26: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 26

SOAP Response HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

Page 27: Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”

Carnegie Mellon University MSCF 27

<soap:Body> <GetNameResponse xmlns= "http://localhost/ACoolQueryService/"> <GetNameResult>string</GetNameResult> </GetNameResponse> </soap:Body></soap:Envelope>


Recommended