WROX - C# Web Services - Building Web Services with ASP.NET and.NET Remoting.pdf

(459 KB) Pobierz
12493900 UNPDF
Programmer to Programmer TM
C#
Building Web Services with
.NET Remoting and ASP.NET
Ashish Banerjee, Aravind Corera, Zach Greenvoss, Andrew Krowczyk,
Christian Nagel, Chris Peiris, Thiru Thangarathinam, Brad Maiani
C#
Web Services
Web Services
12493900.103.png 12493900.114.png 12493900.125.png 12493900.136.png 12493900.001.png 12493900.012.png
Summary of Contents
Introduction
1
Section One – Getting Started
9
Chapter 1: What is a Web Service?
11
Chapter 2: Web Service Protocols
27
Chapter 3: Web Services and the .NET Framework
49
Section Two – ASP.NET Web Services
65
Chapter 4: Building an ASP.NET Web Service
67
Chapter 5: Consuming ASP.NET Web Services
105
Section Three – .NET Remoting
129
Chapter 6: .NET Remoting Architecture
131
Chapter 7: Web Services Anywhere
175
Chapter 8: Building a Web Service with .NET Remoting
201
Chapter 9: Building a .NET Remoting Client
231
Section Four – Advanced Topics
253
Chapter 10: Universal Description, Discovery and Integration (UDDI)
255
Chapter 11: .NET Security and Cryptography
275
Chapter 12: Web Services As Application Plug-Ins
333
Section Five – Case Studies
367
Case Study 1: ASP.NET
369
Case Study 2: P2P .NET Remoting
423
Section Six –Appendix
493
Appendix A: .NET Remoting Object Model
495
Index
581
.NET Remoting Architecture
6
In the last few chapters, we have seen how ASP.NET web services can be created and used. ASP.NET
web services require the ASP.NET runtime as hosting environment. Using .NET Remoting directly, we
can host a web service in any application we want. .NET Remoting allows much more flexibility
because different transport protocols may be used, we can get a performance increase with different
formatting options, and it's possible to host the server in different application types.
The next four chapters will deal with .NET Remoting. In this chapter, we will look at the architecture of
.NET Remoting, and go into detail in the following areas:
What is .NET Remoting?
.NET Remoting Fundamentals
Object Types
Activation
Marshaling
Asynchronous Remoting
Call Contexts
The next chapter will show different application types, transport protocols, and formatting options, and
Chapters 8 and 9 will show an example of using .NET Remoting. Let's begin the chapter with a look at
.NET Remoting.
As with the previous chapters, the code for the examples in this chapter can be downloaded from
http://www.wrox.com .
12493900.023.png 12493900.034.png 12493900.045.png 12493900.056.png 12493900.059.png 12493900.060.png 12493900.061.png 12493900.062.png 12493900.063.png 12493900.064.png 12493900.065.png 12493900.066.png 12493900.067.png 12493900.068.png 12493900.069.png 12493900.070.png 12493900.071.png 12493900.072.png 12493900.073.png 12493900.074.png 12493900.075.png 12493900.076.png 12493900.077.png 12493900.078.png 12493900.079.png 12493900.080.png 12493900.081.png 12493900.082.png 12493900.083.png 12493900.084.png 12493900.085.png 12493900.086.png 12493900.087.png 12493900.088.png 12493900.089.png 12493900.090.png 12493900.091.png 12493900.092.png 12493900.093.png 12493900.094.png 12493900.095.png 12493900.096.png 12493900.097.png 12493900.098.png 12493900.099.png 12493900.100.png 12493900.101.png 12493900.102.png 12493900.104.png 12493900.105.png 12493900.106.png 12493900.107.png 12493900.108.png 12493900.109.png 12493900.110.png 12493900.111.png 12493900.112.png 12493900.113.png 12493900.115.png 12493900.116.png 12493900.117.png 12493900.118.png 12493900.119.png 12493900.120.png 12493900.121.png 12493900.122.png 12493900.123.png 12493900.124.png 12493900.126.png 12493900.127.png 12493900.128.png 12493900.129.png 12493900.130.png 12493900.131.png 12493900.132.png 12493900.133.png 12493900.134.png 12493900.135.png 12493900.137.png 12493900.138.png 12493900.139.png 12493900.140.png 12493900.141.png 12493900.142.png 12493900.143.png 12493900.144.png 12493900.145.png 12493900.146.png 12493900.002.png 12493900.003.png 12493900.004.png 12493900.005.png 12493900.006.png 12493900.007.png 12493900.008.png 12493900.009.png 12493900.010.png 12493900.011.png 12493900.013.png 12493900.014.png 12493900.015.png 12493900.016.png 12493900.017.png 12493900.018.png 12493900.019.png 12493900.020.png 12493900.021.png 12493900.022.png 12493900.024.png 12493900.025.png 12493900.026.png 12493900.027.png 12493900.028.png 12493900.029.png 12493900.030.png 12493900.031.png 12493900.032.png 12493900.033.png 12493900.035.png 12493900.036.png 12493900.037.png 12493900.038.png 12493900.039.png 12493900.040.png 12493900.041.png 12493900.042.png 12493900.043.png 12493900.044.png 12493900.046.png 12493900.047.png 12493900.048.png 12493900.049.png 12493900.050.png 12493900.051.png 12493900.052.png 12493900.053.png
Chapter 6
What is .NET Remoting?
.NET Remoting is the replacement for DCOM. As we have seen in the last chapters, ASP.NET web services
are an easy-to use-technology to call services across a network. ASP.NET web services can be used as a
communication link with different technologies, for example to have a COM or a Java client talk to web
services developed with ASP.NET. As good as this technology is, however, it is not fast and flexible enough
for some business requirements in intranet solutions, and ASP.NET web services requires the ASP.NET
runtime. With .NET Remoting we get Web Services Anywhere that can run in every application type.
Web Services Anywhere
The term "Web Services Anywhere" means that web services can not only be used in any application, but
any application can offer web services. ASP.NET web services require the IIS to run; web services that make
use of .NET Remoting can run in any application type: console applications, Windows Forms applications,
Windows services, and so on. These web services can use any transport with any payload encoding .
In the next chapter we will talk about when and how to use .NET Remoting with different transports
(TCP and HTTP) and about different payload encoding mechanisms (SOAP and binary).
CLR Object Remoting
The next part of .NET Remoting that we need to be aware of is CLR Object Remoting. With CLR
Object Remoting we can call objects across the network, as if they were being called locally.
With CLR Object Remoting we have:
Distributed Identities – Remote objects can have a distributed identity. If we pass a reference
to a remote object, we will always access the same object using this reference.
Activation – Remote objects can be activated using the new operator. Of course, there are
other ways to activate remote objects, as we will see later.
Lease-Based Lifetime – How long should the object be activated on the server? At what time
can we assume that the client no longer needs the remote object? DCOM uses a ping
mechanism that is not scalable to internet solutions. .NET Remoting takes a different
approach with a lease-based lifetime that is scalable.
Call Context – With the SOAP header, additional information can be passed with every
method call that is not passed as an argument.
We will cover all of these CLR Object Remoting features in detail later on in this chapter.
.NET Remoting Fundamentals
The methods that will be called from the client are implemented in a remote object class. In the figure
opposite we can see an instance of this class as the Remote Object. Because this remote object runs inside a
process that is different from the client process – usually also on a different system – the client can't call it
directly. Instead the client uses a proxy . For the client, the proxy looks like the real object with the same
public methods. When the methods of the proxy are called, messages will be created. These are serialized
using a formatter class, and are sent into a client channel . The client channel communicates with the server
part of the channel to transfer the message across the network. The server channel uses a formatter to
deserialize the message, so that the methods can be dispatched to the remote object :
132
.NET Remoting Architecture
Client Process
Server Process
Proxy
Remote Object
Formatter
Formatter
Client Channel
Sever Channel
In the simplest case, we have to create a remote object class and instantiate a channel for a .NET
Remoting application. The formatter and the proxy will be supplied automatically. The architecture is
very flexible in that different formatters and channels can be used. We cover the use of the TCP and
HTTP channels, and the SOAP and binary formatters in the next chapter.
In the next section we'll start with the simplest case to develop a remoting object, server, and client. In
this section we will not go into the details of the remoting architecture, as we will cover this later after
we've finished a simple client and server.
Remote Object
A remote object is implemented in a class that derives from System.MarshalByRefObject .
MarshalByRefObject defines methods for lifetime services that will be described later when we use
the leasing features. A remote object is confined to the application domain where it is created. As we
already know, a client doesn't call the methods directly; instead a proxy object is used to invoke
methods on the remote object. Every public method that we define in the remote object class is
available to be called from clients.
What is an application domain ? Before .NET, processes were used as a security boundary so that one
process couldn't crash another process because it used private virtual memory. With the help of the
managed environment of .NET, the code of an application can be checked, and there's no way to crash the
process. To reduce the overhead with different processes, the concept of an application domain was
introduced with .NET. Multiple applications can run in the same process without influencing each other
if they are called within different application domains. If one of these applications throws an exception
that isn't handled, only the application domain is terminated, and not the complete process. To invoke a
method in an object running in a different application domain, .NET remoting must be used.
The following code sample shows a simple remote object class, and the code for this simple example can be
found in the SimpleTest folder of the code download for this chapter, which is available from
http://www.wrox.com . The method Hello() is declared public to make it available for a remoting client:
// MyRemoteObject.cs
using System;
namespace Wrox.Samples
{
133
12493900.054.png 12493900.055.png 12493900.057.png 12493900.058.png
Zgłoś jeśli naruszono regulamin