How to Use remoting to implement duplex self-artech)

Source: Internet
Author: User
Tags call back

Remoting is a mature and Efficient Distributed Technology on the net platform. We are used to using remoting in the traditional Remote Call method. In the application domain where the client is located, we remotely call a method across the application domain through proxy (transparent proxy. When a call request from the client arrives at the application domain of the server through the proxy, remoting infrastructure activates the corresponding remote object on the server (an inherited sub-system. externalbyrefobject Class Object) -- here, only the server activated object (SAO) is activated on the server, and then the server executes the corresponding operation, passes the result to the proxy, and finally reaches the client. This is a typical request/response call method.

I have always admired remoting on the. NET platform rather than XML Web Service because I think. Net remoting is a mature distributed technology. It provides many features that XML Web Services do not possess. The support for bidirectional communication is a good embodiment.

Compared with the typical request/response message exchange pattern -- MEP, bidirectional communication is essentially duplex MEP. That is to say, when the server executes an operation, it can call back the client operation (this operation is then executed in the client application domain ).

Now let's look at how to implement bidirectional communication in the remoting environment step by step. In the following sample, our logic is: to call a remote call of a mathematical computation, in addition to passing the corresponding operands, we also pass an object, this object can be called back on the server to display the operation result on the client.
Download from the URL belowSource code:
Http://www.cnblogs.com/files/artech/Artech.DuplexRemoting.zip

Step 1: Build the overall architecture of the entire solution.

    • Artech. duplexremoting. Contract: Class Library Project, which defines the contract (Interface) of the remote object and callback object ). In fact, from the server perspective, the callback operation is performed in the application domain of the client, so in essence, the callback object is a remote object of the server.

The purpose of defining such a contract project is as follows:

1. If the remote object interface is not configured, a client that needs to call the remote object must reference the remote object itself. From the security perspective, the server exposes too many operation implementation logic to the client. If we extract the contract of the remote operation, the client only needs to reference this interface.

2. In general, the contract of the remote object is static, while the implementation of the business logic is constantly changing. The client only needs to understand the contract of the remote object, regardless of the changes made to the implementation of the remote object on the server, which has any impact on the client.

    • Artech. duplexremoting. remoting: Class Library Project, which defines the remote object itself. Because the remote object must implement the contract defined above. Therefore, artech. duplexremoting. contract must be referenced.

    • Artech. duplexremoting. Hosting: console application project, host remoting in self-host mode. Reference artech. duplexremoting. remoting.

    • Artech. duplexremoting. Client: console application project, referencing artech. duplexremoting. Contract.

Step 2 define contract in artech. duplexremoting. Contract

Iduplexcalculator. CS

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace artech. duplexremoting. Contract
{
Public interface iduplexcalculator
{
Void add (Double X, Double Y, icalculatorcallback callback );
}
}

Icalculatorcallback. CS

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace artech. duplexremoting. Contract
{
Public interface icalculatorcallback
{
Void showresult (Double X, Double Y, double result );
}
}

Step 3 define remote objects in artech. duplexremoting. remoting

Duplexcalculatorremoting. CS

Using system;
Using system. Collections. Generic;
Using system. text;

Using artech. duplexremoting. Contract;

Namespace artech. duplexremoting. remoting
{
Public class duplexcalculatorremoting: marshalbyrefobject, iduplexcalculator
{
Iduplexcalculator members # region iduplexcalculator members

Public void add (Double X, Double Y, icalculatorcallback callback)
{
Console. writeline ("invoke the method add ({0}, {1}).", x, y );
Double result = x + y;
Callback. showresult (X, Y, result );
}

# Endregion
}
}

Step 4 remote object in artech. duplexremoting. Hosting host

App. config

<Configuration>
<System. runtime. remoting>
<Application name = "Calculator">
<Service>
<Wellknown mode = "singlecall"
Type = "artech. duplexremoting. remoting. duplexcalculatorremoting, artech. duplexremoting. remoting"
Objecturi = "duplexcalculator. Soap"/>
</Service>

<Channels>
<Channel ref = "HTTP" Port = "8080">
<Serverproviders>
<Provider ref = "WSDL"/>
<Formatter ref = "binary" typefilterlevel = "full"/>
</Serverproviders>
<Clientproviders>
<Formatter ref = "binary"/>
</Clientproviders>
</Channel>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>

Program. CS

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace artech. duplexremoting. Hosting
{
Class Program
{
Static void main (string [] ARGs)
{
System. runtime. remoting. remotingconfiguration. Configure ("artech.duplexremoting.hosting.exe. config", false );
Console. writeline ("Calculator Service has begun to listen ");
Console. Read ();
}
}
}

Note the following two points:

1. When defining a channel, you must specify a bi-directional channel ). The system defines a series of system-defined channels for calling remote objects. Some of them can only provide one-way communication-for example, only client-to-server communication, while others can provide two-way communication-such as TCP channel and HTTP channel.

2. In serverprovider section, we must set typefilterlevel to full. For security considerations, remoting provides two deserialization levels -- low & full. Low is the default value. If typefilterlevel is set to low, remoting will deserialize objects related to the basic functions of remoting. If it is set to full, remoting deserializes all types. If you want to know which types are restricted under low level, see http://msdn2.microsoft.com/en-us/library/5dxse167.aspx.

The reason why typefilterlevel is set to full is that our remote call contains a callback object, which is actually an inherited system. externalbyrefobject Class Object (this object will be stored in artech. duplexremoting. defined in the client ). This object will not be automatically deserialized at low level.

<Channels>
<Channel ref = "HTTP" Port = "8080">
<Serverproviders>
<Provider ref = "WSDL"/>
<Formatter ref = "binary" typefilterlevel = "full"/>
</Serverproviders>
<Clientproviders>
<Formatter ref = "binary"/>
</Clientproviders>
</Channel>
</Channels>

Public interface iduplexcalculator
{
Void add (Double X, Double Y, icalculatorcallback callback );
}

Step 4InArtech. duplexremoting. Client defines the callback object and the call Remote Object

Calculatorcallbackhandler. CS

Using system;
Using system. Collections. Generic;
Using system. text;
Using artech. duplexremoting. Contract;

Namespace artech. duplexremoting. Client
{
Public class calculatorcallbackhandler: marshalbyrefobject, icalculatorcallback
{

Icalculatorcallback members # region icalculatorcallback members

Public void showresult (Double X, Double Y, double result)
{
Console. writeline ("x + y = {2} where x = {0} and Y = {1}", X, Y, result );
}

# Endregion
}
}

App. config

<Configuration>
<System. runtime. remoting>
<Application>
<Channels>
<Channel ref = "HTTP" Port = "0">
<Clientproviders>
<Formatter ref = "binary"/>
</Clientproviders>
<Serverproviders>
<Formatter ref = "binary" typefilterlevel = "full"/>
</Serverproviders>
</Channel>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>

Program. CS

using system;
using system. collections. generic;
using system. text;
using artech. duplexremoting. contract;
namespace artech. duplexremoting. client
{< br> class Program
{< br> static void main (string [] ARGs)
{< br> system. runtime. remoting. remotingconfiguration. configure ("artech.duplexremoting.client.exe. config ", false);
invocateduplexcalculator (" http: // localhost: 8080/calculator/duplexcalculator. soap ");
}< br>
static void invocateduplexcalculator (string remoteaddress)
{< br> iduplexcalculator proxy = (iduplexcalculator) activator. getObject (typeof (iduplexcalculator), remoteaddress);
proxy. add (1, 2, new calculatorcallbackhandler ();
console. read ();
}< BR >}< br>

Note the following two points:
1. because the server remotely calls the callback object in the client application domain during cross-application domain running (the callback is actually executed by the client rather than the server), the callback object should be an externalbyrefobject object.
2. As mentioned above, for the server side, the callback object is actually a remote object (in the callback process, the client side is converted to the server side, and the server side is converted to the client side ). The server needs to register some channels for the client to access remote objects hosted on the server. Similarly, the server needs to call back a callback object hosted on the client application domain. The client needs to register the corresponding channel
3. Like the server, we must set typefilterlevel to full.

So far, we have completed all the programs. Let's run them.

1. Run artech. duplexremoting. Hosting

2. Run artech. duplexremoting. Client

Set Remote ObjectHostToIISMedium

We know that remoting has two host Methods: Self host and iis host. In the preceding method, we put remoting host into a console application. Now we will try to host it to IIS. In fact, we have to do a very simple job.

1. Add a virtual directory corresponding to the artech. duplexremoting. remoting folder to IIS manager. Assume that the alias directory of this virtual directory is artech. duplexremoting.

2. in artech. duplexremoting. in the remoting root directory (http: // localhost/artech. add a web. config, and add a file similar to artech. duplexremoting. hosting/app. the remoting configuration in config.

<? XML version = "1.0"?>
<Configuration xmlns = "http://schemas.microsoft.com/.NetConfiguration/v2.0">

<System. runtime. remoting>
<Application>
<Service>
<Wellknown mode = "singlecall"
Type = "artech. duplexremoting. remoting. duplexcalculatorremoting, artech. duplexremoting. remoting"
Objecturi = "duplexcalculator. Soap"/>
</Service>

<Channels>
<Channel ref = "HTTP">
<Serverproviders>
<Provider ref = "WSDL"/>
<Formatter ref = "binary" typefilterlevel = "full"/>
</Serverproviders>
<Clientproviders>
<Formatter ref = "binary"/>
</Clientproviders>
</Channel>
</Channels>
</Application>
</System. runtime. remoting>

<System. Web>
<Compilation DEBUG = "true">
<Assemblies>
<Add Assembly = "system. Security, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"/>
<Add Assembly = "Microsoft. Transactions. Bridge, version = 3.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"/>
<Add Assembly = "smdiagnostics, version = 3.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089"/>
<Add Assembly = "system. identitymodel. selectors, version = 3.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089"/>
<Add Assembly = "system. directoryservices, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"/>
<Add Assembly = "system. Web. regularexpressions, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"/>
<Add Assembly = "system. Transactions, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089"/>
<Add Assembly = "system. messaging, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"/>
<Add Assembly = "system. serviceprocess, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"/>
</Assemblies>
</Compilation>
</System. Web>
</Configuration>

In this way, we can run the client without hosting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.