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

Asynchronous Calls on Remote Objects

 

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.

 

原文地址:https://www.cnblogs.com/rickie/p/49717.html