Additional .NET Concepts

30
http://www.epcc.ed.ac.uk/~ogsanet [email protected] February 24 th -25 th 2004 Daragh Byrne – EPCC Additional .NET Concepts

description

Additional .NET Concepts. Daragh Byrne – EPCC. Purpose. Web Services using ASP.NET Basis of MS.NETGrid software Assemblies Metadata and reflection Application configuration. ASP.NET and Web Services. ASP.NET Features. Unified Web Development Platform - PowerPoint PPT Presentation

Transcript of Additional .NET Concepts

Page 1: Additional .NET Concepts

http://www.epcc.ed.ac.uk/~ogsanet

[email protected]

February 24th-25th 2004

Daragh Byrne – EPCC

Additional .NET Concepts

Page 2: Additional .NET Concepts

2

Purpose

Web Services using ASP.NET– Basis of MS.NETGrid software

Assemblies Metadata and reflection Application configuration

Page 3: Additional .NET Concepts

3

ASP.NET and Web Services

Page 4: Additional .NET Concepts

4

ASP.NET Features

Unified Web Development Platform Runs under Microsoft Internet Information Services

(IIS):– Available on all Windows Platforms better than Win2K – Requires .NET runtime (CLR)

Combines traditional forms-based Web Application development and Web Services

Runs compiled applications so performs better than traditional ASP

Excellent support in Visual Studio .NET WYSIWYG-based design like designing Windows

Forms

Page 5: Additional .NET Concepts

5

Reference

Material from this lecture covered in great detail in your “Building Web Services” book:

– Chapter 6 in particular

Page 6: Additional .NET Concepts

6

Web Applications in ASP.NET

The files present in a virtual directory under IIS comprise a Web Application:– Isolated application domain for each Web application

Can contain both Web pages and Web Services Lives in own AppDomain:

– Memory protected region within a process, a .NET feature– Protects applications from one another

Requests for a page/service are mapped by IIS to the ASP.NET handler:– aspnet_isapi.dll, aspnet_wp.exe

• Listen for file extensions .aspx, .asmx etc

– Work carried out by a recycled process for efficiency and stability

Configuration is handled by Web.config in the root directory:– Web.config is standard ASP.NET application configuration file

Page 7: Additional .NET Concepts

7

Request Handling

IIS (aspnet_isapi.dll)

Request

asmx handler aspx handler

Response Response

Page 8: Additional .NET Concepts

8

Web Pages in ASP.NET

HTML lives in .aspx files:– Code can be placed here, or– Code in separate file

Presentation layer separated from logic:– A step away from the ugly embedded ASP model

Supports Web controls:– Reusable UI elements, e.g. menus, date-pickers– Can write custom controls

More details available in .NET framework documentation:– We concentrate on the Web Services aspect

Page 9: Additional .NET Concepts

9

ASP.NET Request Handling

HTTP request to foo.aspx

ASP.NET

foo.aspx

Compile and execute Output (html)

HTTP Response to client

Page 10: Additional .NET Concepts

10

Web Services in ASP.NET

Write a class that provides the service functionality:– Tag operations with WebMethod attribute– Tag class with WebService attribute to specify default namespace of

the service

Write a .asmx file:– References the implementing class

Can separate .asmx file and source code

Page 11: Additional .NET Concepts

11

Web Service Request Handling

SOAP request to foo.asmx

ASP.NET

Foo.asmx

Execute Output (SOAP)

SOAP Response to client

Page 12: Additional .NET Concepts

12

Example Web Service

// HelloService.asmx<% WebService Class=“HelloService” %>.. .. ..

// HelloService.cs[WebService( NameSpace=“http://myURL.com/HelloService” )]public class HelloService{

[WebMethod]public string SayHello(string name){

return “Hello there, “ + name);}

}

Compile and deploy under ASP.NET: – Easiest with Visual Studio .NET

Build a client

Page 13: Additional .NET Concepts

13

Useful Attributes

WebServiceAttribute– BufferResponse– Description– MessageName

Can use WebServiceAttribute to set properties of the service:

– [WebService( Namespace=“someURI”, Description=“Some text describing the service”)]

Page 14: Additional .NET Concepts

14

Automatic WSDL Generation

ASP.NET will automatically generate WSDL service descriptions

Send WSDL on the query string to see this:– e.g. service at http://localhost/myservice.asmx?WSDL

Reflection on service type to generate this document:– Uses e.g. WebMethod attributes to generate operation elements

Can control contents of WSDL document with attributes:– e.g. SoapDocumentMethodAttribute

Can suppress WSDL generation and do custom generation

Page 15: Additional .NET Concepts

15

Building Web Service Clients

Idea is that accessing a Web Service is as simple as making method call

Use the WSDL description to auto-generate client ‘proxy’ classes

.NET Framework provides wsdl.exe tool:– wsdl http://myhost.com/SomeService.asmx?wsdl /o:ServiceProxy.dll

– Examines a WSDL document– Outputs a DLL with a class that represents the service:

• Derived from SoapHttpClientProtocol class, which handles serialization, invocation, etc

– Highly integrated with Visual Studio.NET:• Does wsdl.exe behind the scenes, adds proxy stub to project automatically

Page 16: Additional .NET Concepts

16

Web Services Enhancements

SDK to support emerging W3C Web Services protocols that address:– Security (WS-Security)– Policy– Routing– Custom SOAP Attachments– Reliable messaging

Now at version 2.0:– http://msdn.microsoft.com/webservices/building/wse/

Page 17: Additional .NET Concepts

17

Assemblies

Page 18: Additional .NET Concepts

18

Assemblies

Assemblies are the unit of code distribution in .NET May be independently versioned May be digitally signed Scope the types within them Are the basis of Code Access Security:

– Code from an assembly can do certain things based on level of trust of assembly provider

Page 19: Additional .NET Concepts

19

Working with Assemblies

Logical assembly consists of a number of modules (files):– Primary module references the other modules

– Primary module identifies assembly

– Most cases only one module

Assemblies contain code, data and/or resources:– e.g icon byte streams.

Assemblies scope the types and data within them:– public, private, internal (package level in Java) modifiers apply to types

at assembly level

– Same type defined in different assemblies == different types

Assembly names are resolved by the runtime before loading:– Must reside in APPBASE or subdirectory:

• APPBASE is standard location off application directory, usually /bin• APPBASE can be changed using configuration files

Page 20: Additional .NET Concepts

20

Assembly Identification

Assemblies may be strongly-named or weakly-named:– Weakly-named just identified by the name of the primary module, minus

the .dll or .exe– Strongly-named have version, culture, public key as well

Concurrent versions of an assembly stored in the Global Assembly Cache:– Can only store strongly-named

Can specify dependence of application on particular assembly in configuration file

Page 21: Additional .NET Concepts

21

Dynamic Assembly Loading

Can work with Assemblies programmatically:– Members on the System.Reflection.Assembly class– Assembly.Load– Assembly.LoadFrom– Assembly.CreateInstance(string typeName)– Assembly.GetCustomAttributes()

Important to know about assemblies for our OGSI implementation

Page 22: Additional .NET Concepts

22

Using Metadata

Page 23: Additional .NET Concepts

23

Attributes

Attributes allow you to add metadata to your types:– Enables a declarative style of programming

Can use framework-supplied or custom attributes Information from attributes are stored in metadata for

that type Example:

public class MyClass{ [System.Obsolete(“Will be removed next release”)] public void SomeObsoleteMethod() { }}

Page 24: Additional .NET Concepts

24

Common Attribute Uses

Conditional compilation/calling:[Conditional(“DEBUG”)]public void myDebugMethod(string s) {}

XML serialisation:[XmlElement(“name”, someNameSpace)public string name_;

Web Service operations:[WebMethod]public string MyWebMethod(){ }

Page 25: Additional .NET Concepts

25

Custom Attributes

Can define own attribute:– Inherit from System.Attribute– Define usage using – you guessed it – an attribute!

Example:[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]public class SomeUsefulAttribute: System.Attribute{ public SomeUsefulAttribute(string s) { }}

// Use like[SomeUseful(“Hello”)]public class SomeClass{}

Page 26: Additional .NET Concepts

26

The Reflection API

Can get access to your attributes using this API:public void SomeCode(){

object [] customAttributes = someObject.GetType().GetCustomAttributes( typeof(SomeUsefulAttribute)); foreach(SomeUsefulAttribute attr in customAttributes) { //process }

}

Can get attributes on methods, fields, etc in a similar manner:– See the .NET documentation for System.Reflection namespace

Page 27: Additional .NET Concepts

27

Application Configuration

Page 28: Additional .NET Concepts

28

Application Configuration

Previously ad-hoc, comma-separated value files, language-specific etc:– Lots of different standards

.NET aims to provide consistent configuration for every application:– XML file-based– XML file in same directory as application usually

System.Configuration namespace provides API for doing this:– Extensible to use your own configuration schema

Web applications use the Web.config file as default

Page 29: Additional .NET Concepts

29

Example Configuration File

<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <sectionGroup name="gridContainer.config"> <section name="containerProperties“ type=“HandlerType"/> <section name="gridServiceDeployment“ type=“HandlerType"/> </sectionGroup> </configSections> <system.web>

<!- - Web Application Configuration - - > </system.web> <system.runtime.remoting /> <gridContainer.config>

<containerProperties> ... </containerProperties> <gridServiceDeployment> ... </gridServiceDeployment>

</gridContainer.config></configuration>

Page 30: Additional .NET Concepts

30

Other .NET Topics of Interest

Win32/COM interoperation:– Legacy integration

WinForms:– Smart clients, href-exes (executables over HTTP)

Asynchronous execution:– Threads, delegates

Support for event-driven programming in C# ADO.NET XML Libraries