Other .NET Topics

Download Report

Transcript Other .NET Topics

Additional .NET Concepts
Daragh Byrne – EPCC
http://www.epcc.ed.ac.uk/~ogsanet
[email protected]
February 24th-25th 2004
Purpose
Web Services using ASP.NET
– Basis of MS.NETGrid software
Assemblies
Metadata and reflection
Application configuration
2
ASP.NET and Web Services
3
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
4
Reference
Material from this lecture covered in great detail in
your “Building Web Services” book:
– Chapter 6 in particular
5
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
6
Request Handling
Request
IIS (aspnet_isapi.dll)
Response
Response
asmx handler
aspx handler
7
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
8
ASP.NET Request Handling
HTTP request to foo.aspx
HTTP Response to client
ASP.NET
Compile and execute
Output (html)
foo.aspx
9
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
10
Web Service Request Handling
SOAP request to foo.asmx
SOAP Response to client
ASP.NET
Execute
Output (SOAP)
Foo.asmx
11
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
12
Useful Attributes
WebServiceAttribute
– BufferResponse
– Description
– MessageName
Can use WebServiceAttribute to set properties
of the service:
– [WebService( Namespace=“someURI”,
Description=“Some text describing the
service”)]
13
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
14
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
15
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/
16
Assemblies
17
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
18
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
19
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
20
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
21
Using Metadata
22
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()
{
}
}
23
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()
{
}
24
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
{
}
25
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
26
Application Configuration
27
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
28
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>
29
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
30