异步调用远程对象(Remote Objects)

Asynchronous Calls on Remote Objects<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

The .NET Framework provides three possibilities to call methods on remote objects (no matter if they are Singleton, SingleCall, or published objects). You can execute their methods in a synchronous, asynchronous, or asynchronous oneway fashion.

 

1. 3种调用Remote Objects的方法

Synchronous calls (同步调用) are basic call methods on remote objects. The server's remote method is called like a common method, and the client blocks (waits) until the server has completed its processing. If an exception occurs during execution of the remote invocation, the exception is thrown at the line of code in which you called the server.

 

Asynchronous calls (异步调用)are executed in a two-step process. The first step triggers the execution but does not wait for the method's response value. The program flow continues on the client. When you are ready to collect the function's response, you have to call another function that checks if the server has already finished processing your request; if not, it blocks until finalization. Any exception thrown during the call of your method will be rethrown at the line of code where you collect the response. Even if the server has been offline, you won't be notified beforehand.

 

The last kind of function is a little different from the preceding ones. With asynchronous one-way methods (异步单向调用), you don't have the option of receiving return values or getting an exception if the server has been offline or otherwise unable to fulfill your request. The .NET Remoting Framework will just try to call the methods on the remote server and won't do anything else.

 

根据上面的介绍,可以了解到对Remote Objects3中调用方法,分别为Synchronous calls (同步调用)Asynchronous calls (异步调用) and asynchronous one-way methods (异步oneway调用)。其中Synchronous calls是经常采用的一个调用方法。

 

2. Asynchronous calls(异步调用)Remote Objects

这里要介绍的是Asynchronous calls(异步调用)远程对象的方法,Asynchronous calls的好处是不需要等待Remote method的返回结果,继续执行下面的代码,这样可以降低延迟。当Remote method是一个费时的操作,同时Client端也不需要同步获取Remote method的返回结果时,异步调用就是一个不错的选择。

 

The .NET Framework provides a feature, called asynchronous delegates (异步委托), that allows methods to be called in an asynchronous fashion with only three lines of additional code.

 

A delegate is, in its regular sense, just a kind of an object-oriented function pointer. You will initialize it and pass a function to be called when the delegate is invoked. In .NET Framework, a delegate is a subclass of System.MulticastDelegate, but C# provides an easier way to define a delegate instead of declaring a new Class.

 

Declaring a Delegate

The declaration of a delegate looks quite similar to the declaration of a method:

delegate <ReturnType> <name> ([parameters]);

指明方法的返回类型和参数列表。

 

As the delegate will call a method at some point in the future, you have to provide it with a declaration that matches the method's signature.

 

 

Remember that the delegate is in reality just another class, so you cannot define it within a method's body, only directly within a namespace or another class! (请注意:事实上delegate是一个类,因此你不能够在方法中定义一个delegate,需要在namespace中直接定义或者在class中定义。)

 

The method that will be invoked asynchronously is a member of the remote object’s class. The definition of the delegate must be available on the client. It is not needed on the server. Therefore, the delegate’s definition usually appears in the client application class.

 

Remote methods do not have to know that they’re going to be called asynchronously. No special coding on the server side is required for asynchronous calls.

也就是说,delegate只需要在Client定义(当然Client端需要知道Remote methods的原型),不需要在Remote class中出现,Remote methods也不需要知道自己是否被异步调用。总之,Remote classServer端不需要作任何变动。

 

However, the client must use the following steps to execute an asynchronous call on a remote method. Client端需要按照如下步骤完成对Remote Methods的异步调用:

(1) Define the delegate.

(2) Obtain a reference to the remote object.

(3) Allocate the delegate object and associate the remote method with the delegate object.

(4) Use the delegate’s BeginInvoke() method to call the remote method.

(5) Call the EndInvoke() method to perform the cleanup associated with the asynchronous call when the asynchronous method is done.

 

Client端部分代码示例:

定义一个Delegate

delegate string AsyncCallerDelegate(int myParam);

……

注册HttpChannel通道

HttpChannel channel = new HttpChannel();

ChannelServices.RegisterChannel(channel);

 

获取Remote Object对象引用

BaseRemoteObject obj = (BaseRemoteObject) Activator.GetObject(

    typeof(BaseRemoteObject),

    "http://localhost:1234/MyRemoteObject.soap");

 

创建Delegate对象,并与Remote method进行关联

AsyncCallerDelegate myDelegate = new AsyncCallerDelegate(obj.RemoteMethodName);

 

通过DelegateBeginInvoke()方法来调用Remote method,并输入参数(这里传入参数1

IAsyncResult myAsyncres = myDelegate.BeginInvoke(1,null,null);

…… 这里做其他的事情

 

调用EndInvoke()方法来完成异步调用过程,同时获取Remote method的返回结果

string resultValue = myDelegate.EndInvoke(myAsyncres);

 

3. Asynchronous One-Way Calls(异步单向调用)

One-way calls are a little different from asynchronous calls in the respect that the .NET Framework does not guarantee their execution. In addition, the methods used in this kind of call cannot have return values or out parameters. You also use delegates to call one-way methods, but the EndInvoke() function will exit immediately without checking if the server has finished processing yet. No exceptions are thrown, even if the remote server is down or the method call is malformed. Reasons for using these kinds of methods (which aren't guaranteed to be executed at all) can be found in uncritical logging or tracing facilities, where the nonexistence of the server should not slow down the application.

异步单向调用与异步调用的区别是: .Net Framework不保证Remote method的执行情况。此外,Remote method不允许有返回值或out参数。在一些不重要的记录日志或跟踪应用中,可以考虑采用异步One-Way调用,这样Remote method的执行情况就不会影响到其他业务代码的执行。

 

You define one-way methods using the [OneWay()] attribute. This happens in the defining metadata and doesn’t need a change in the server or client. The attribute [OneWay()] has to be specified in the interface definition of each method that will be called this way.

[OneWay()] attribute将方法标记为单向方法,无返回值和 out ref 参数,指示该方法具有 void 返回并只有 in 参数。

 

 

Reference:

(1) Ingo Rammer, Advanced .Net Remoting (C# Edition).

(2) David Conger, Remoting with C# and .Net.

 

Sample Categories Some samples are shown below in more than one category. The Dynamic Request, RODL and Named Pipe samples do not ship with the trial version of the RemObjects SDK. Category Samples Introduction First Sample Multi Channel Super TCP Channel Chat RODL (not shipped with the trial version) Async Auto Server Dynamic Request (not shipped with the trial version) Session Types Time Server Channels MegaDemo Multi Channel Super TCP Channel Chat HTTP Chat Named Pipes (not shipped with the trial version) Intermediate Arrays Broadcast Chat Extended File Transfer Named Pipes (not shipped with the trial version) Service Discovery Variants Architecture Session Types Class Factories FPC Server DataSnap Dispatch Notifier Auto Server Multi Channel Proxy Server Advanced Class Factories COM Dispatch Notifier Proxy Server Sample Descriptions Name Category Description Arrays Intermediate This sample shows how to use TROArray for presentating DB tables in a master/detail relationship. Async Introduction This sample shows how to call methods on a RemObjects SDK server asynchronously. There may be times where you want to submit a request for information and defer receiving the result until a bit later. A very simple calculation (Sum) is performed, but this has a built in ten second delay so that it is possible to query the server before the calculation is completed. Auto Server Introduction Architecture This sample shows how a client can control its server when they are both running locally. This is useful if you want to provide a simple standard alone solution which is easily upgraded to multi-tier (or you might want to provide both options). Broadcast Chat Intermediate This example shows how to use the TROBroadcastServer and TROBroadcastChannel channels to write an UDP broadcasting chat program. Class Factories Architecture Advanced This example shows how to use a Class Factory to generate three types of server: Singleton: all clients access a single server object. Single Call: server instances are created on demand and destroyed after processing the method call. Pooled: multiple server instances are accessible by clients. This works exactly the same as Singleton, unless the first server instance is busy. Note: to test this sample properly, you need to run at least two clients. COM Advanced This sample shows how to call an existing RemObjects SDK server using COM. DataSnap Architecture Standard example of the use of the TRODataSnapModule and TRODataSnapConnection components. Dispatch Notifier Architecture Advanced This example shows how to customize message dispatching. IRODispatchNotifier is a special interface that TROInvoker classes know and look for. If your server side object implements it, the IRODispatchNotifier.GetDispatchInfo method will be called before the target method is invoked. Dynamic Request (not shipped with the trial version) Introduction This example shows how to use the TRODynamicRequest component to execute server methods. Extended File Transfer Intermediate This example shows how to transfer files to and from a RemObjects SDK Server in chunks and how to monitor new files via server events. Note: needs at least two clients open. File(s) uploaded from one client are downloaded to the other(s). First Sample Introduction This sample provides an introduction to using the Delphi edition of the RemObjects SDK product. The example shows how to define/implement methods on the server and how to access them from the client. The data consists of name information and four simple methods are provided by the service: Nicknames, VerifyName, CheckName and FullNames. FPC Server Architecture HTTP Chat Channels This shows how to use polled events to create an HTTP based chat program. The clients poll every few seconds for new messages and the server distributes the messages to the appropriate client(s). MegaDemo Channels This comprehensive example illustrates many of the features of the RemObjects SDK by providing benchmark facilities for the various protocols and channels supported. Multi Channel Introduction Channels Architecture This example provides an introduction to using the Delphi edition of the RemObjects SDK product. It shows how to use different channels to connect to the server application. Named Pipes (not shipped with the trial version) Channels Intermediate This example shows the use of a named pipe connection. It creates a named pipe server as a Windows service. Proxy Server Architecture Advanced This example shows how to create a proxy server to redirect the calls to another server without having to recreate the RODL file, thus allowing the use of the same types of the original server. This provides total control. As every call will pass from the proxy class before going to the real server, you can even stop methods from being dispatched any further. RODL (not shipped with the trial version) Introduction <to follow> Service Discovery Intermediate This sample illustrates how to use the TRODiscoveryClient and TRODiscoveryServer components. Each instance of the application acts both as a server and as a client. For the server, you can modify the list of services supported on the right-hand side, while for the client you can enter a service name and get a list of the servers available on your LAN to support it. Session Types Architecture Introduction This example shows the use of sessions and illustrates several SessionManagers: TROEventSessionManager, TROInMemorySessionManager, TROMasterServerSessionManager and TRODBSessionManager. Important note: this sample needs a "Sessions" table to be created in one of your databases. By default, the sample looks for the Sessions table in MSSQL's Northwind Database, but you can easily change to connect to any other database. Super TCP Channel Chat Introduction Channels This sample shows how the Super TCP Channel can be used to create a chat server and clients. Unlike the HTTP Chat sample, this sample doesn't poll the server but sends events back to clients directly. Time Server Introduction This is an extremely basic sample illustrating how to use the TROBroadcastServer and TROBroadcastChannel components. Variants Intermediate This example shows how the RemObjects SDK can transfer variants and array of variants from the client and server using the TROBinMessage and TROSOAPMessage message types.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值