Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize message transmission between WCF and Remoting

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to achieve message transmission between WCF and Remoting". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve message transmission between WCF and Remoting".

I. introduction to NET Remoting

Unlike MSMQ, .NET Remoting does not support offline availability, and it is only suitable for programs on the .NET platform to communicate. It provides a framework that allows an object to interact with another object through an application domain. .net applications are executed in one main application domain, and code in one application domain cannot access the data of another application domain. However, in some cases, we need to communicate with other application domains across application domains, so we can use .NET Remoting technology to interact with objects in another application domain.

Basic principles

.net Remoting technology enables communication of objects between two applications through channels.

First of all, the client obtains the server-side object through the access channel of Remoting technology, and then parses it to the client-side object through the proxy, which is also called transparent proxy. At this time, the client-side object is only a reference of the server object. This not only ensures the loose coupling of related objects between the client and the server, but also optimizes the performance of communication. In this process, when the client invokes the method of the remote object through a transparent proxy, the call is encapsulated in a message object, which includes the remote object information, the method name and parameters to be called, and then the transparent agent calls the Invoke method delegated to the real proxy (RealProxy object) to generate an IMethodCallMessage

The message object is then serialized into a data stream and sent to the channel, which sends the data stream to the server. When the server receives the formatted data, it first restores the message object through deserialization, then activates the remote object on the server side and calls the corresponding method. The process of returning the result of the method is repeated in reverse according to the previous method. The specific implementation schematic diagram is as follows:

Remote object:

It is an object that runs on the server side and cannot be called directly by the client. Because the object passed by .NET Remoting is referenced, the remote object passed must inherit the MarshalByRefObject class, which enables remote objects to be used in .NET Remoting application communication and supports cross-domain boundary access of objects.

How remote objects are activated

Before accessing an object instance on the server side, it must be created and initialized through a process called Activation. This way in which a client creates a remote object through a channel is called object activation. In .NET Remoting, remote object activation is divided into two main categories: server-side activation and client-side activation.

1. Server side activation (WellKnow (well-known object) activation mode)

Why is it called well-known object activation mode? This is because the service application publishes this type on a well-known uniform resource identifier (URI) before activating the object instance, and then the server configures a WellKnow object for this type and publishes the object based on the specified port or address.

Net Remoting divides server-side activation into SingleTon mode and SingleCall mode.

SingleTon mode: this is stateful mode. The .NET Remoting will create the same object instance for all clients. The server creates the service object only when the object is called for the first time, and when the object is active, the SingleTon instance handles all subsequent client access requests, regardless of whether they are the same client or other clients. The SingleTon instance will maintain its state throughout the method call, similar to the concept of a static member. (equivalent to Application status)

SingleCall mode: is a stateless mode. When the client calls the method of the remote object, Remoting establishes a remote object instance for each client, and the destruction of the object instance is automatically managed by GC. Similar to the concept of instance members. (equivalent to Session status)

2. Client activation:

Different from Wellknow mode,. When NET Remoting activates each object instance, it assigns a URI to each client-activated type. Once the client activation mode receives a request from the client, an instance reference is established for each client. The differences between SingleCall mode and client activation mode are:

First, the object instance is created at a different time. The client activation method is instantiated once the client makes a call request, while the SingleCall is not created until the object method is called.

Second, SingleCall mode-activated objects are stateless, the object declaration cycle is managed by GC, while client-activated objects are stateful and their lifecycles can be customized.

Third, the two activation modes are implemented differently on the server side and the client side, especially on the client side, where the SingleCall mode is activated by GetObject (), which calls the object's default constructor, while the client activation mode is activated through CreateInstance (), which can pass parameters, so you can call a custom constructor to create an instance.

Channel:

The communication of objects between two application domains is achieved through channels in the .NET Remoting. The .NET Remoting includes 4 channel types:

The TcpChannel:Tcp channel uses the Tcp protocol to transport serialized message flows across .net Remoting boundaries, and TcpChannel serializes message objects in binary format by default, resulting in higher transport performance but does not provide any built-in security features.

The HttpChannel:Http channel uses the Http protocol to send messages between the client and the server, which traverses the firewall on the Internet to transmit serialized message flows. By default, HttpChannel serializes message objects in Soap format, so it is more interoperable and can use the encryption mechanism in the Http protocol to encrypt messages to ensure security. Therefore, usually in the local area network, we use TcpChannel more, and if we want to pass through the firewall, we use HttpChannel.

IpcChannel: Inter-process communication, using only communication between processes of the same system, without the need for hostname and port number. The hostname and port number are specified for both the Http channel and the Tcp channel.

Custom channels: custom transport channels can communicate using any basic transport protocol, such as UDP protocol, SMTP protocol, etc.

Second, the server activation method: SAO1, create a dll with a shared interface, which should be referenced by both the server and the client. / / define the interface class ITax / / compile to generate ITaxTemoting.dll / / both the server side and the client side add a reference to this class dll public interface ITax {double GetTax (int salary);}

Create a remote object that must inherit the MarshalByRefObject object. The remote object class Tax inherits the base class MarshalByRefObject and interface ITax.

/ / define a remote object, which must be compiled from MarshalByRefObject// to generate TaxRemoting.dll. The server must add a reference to the dll: public class Tax: MarshalByRefObject, ITax {private int _ callOCunt = 0; public Tax () {Console.WriteLine ("Remoting object Tax activated");} / / public double GetTax (int salary) {_ callOCunt++; return (double) tax;} 2, server

Need to add System.Runtime.Remoting.dll reference

Define channels and listen, register remote objects. The channel is used for communication between .NET client and server.

In the .NET Remoting, multiple channels are allowed to be created at the same time, but the .NET Remoting requires that the name of the channel be different because the name is the unique identifier used to identify the channel.

TcpChannel channel = new TcpChannel (8085); / / define the channel, and ChannelServices.RegisterChannel (channel, false) such as HttpChannel\ IPCChannel; / / register the channel RemotingConfiguration.RegisterWellKnownServiceType (typeof (Tax), "Tax1", WellKnownObjectMode.SingleCall); / / register a well-known remote object Tax,ObjectURI as Tax on the server, and the well-known object mode is a single call. 3. Client

Register the channel, get the remote object proxy according to URL, and use the proxy to call the remote object on the server side.

Void Main () {TcpChannel channel = new TcpChannel (); ChannelServices.RegisterChannel (channel, false); ITax obj = (ITax) Activator.GetObject (typeof (ITax), "tcp://localhost:8085/Tax1"); / / get a remote object agent according to URL, which is used to invoke the remote object on the server side. If (obj = = null) {Console.WriteLine ("Could not locate TCP server");} Console.WriteLine (obj.GetTax (1). ToString ()); / / call the method of the remote object to get the result, which calls the default constructor of the server-side class to instantiate. Console.WriteLine (obj.GetCallCount (). ToString ()); Console.WriteLine (obj.GetTax (1). ToString ()); Console.WriteLine (obj.GetCallCount (). ToString ());} / / you can register the type for the client first, and then instantiate the object (not recommended) RemotingConfiguration.RegisterWellKnownClientType (typeof (ITax), "tcp://localhost:8085/Tax1"); Tax obj = new Tax (); obj.GetTax (); III. Client activation method: CAO

You can call the server's non-default constructor with parameters, and the server saves a different state for each client.

1. Declare and define a public class public class Tax: MarshalByRefObject, ITax {/ / code with server SAO} 2, server, application Tax assembly TcpChannel channel = new TcpChannel (8085); ChannelServices.RegisterChannel (channel, false); RemotingConfiguration.ApplicationName = "Tax1"; RemotingConfiguration.RegisterActivatedServiceType (typeof (Tax)); 3. Client

You can use Soapsuds.exe to separate Tax assemblies for common client calls (that is, without specific implementation content)

E: > soapsuds-url: http://127.0.0.1:8502/TaxTax1?wsdl-oa:ClientProxy.dll

This will generate the ClientProxy.dll file for us in the root directory of the E disk, which will be used for the client to generate the agent.

Void Main () {TcpChannel channel = new TcpChannel (); ChannelServices.RegisterChannel (channel, false); Tax obj = (Tax) Activator.CreateInstance (typeof (ITax), null, new [] {new UrlAttribute ("tcp://localhost:8085/Tax1")}); / / get a remote object agent according to URL, which is used to invoke the remote object on the server side. Console.WriteLine (obj.GetTax (1). ToString ()); Console.WriteLine (obj.GetCallCount (). ToString ()); Console.WriteLine (obj.GetTax (1). ToString ()); Console.WriteLine (obj.GetCallCount (). ToString ());} / / you can register the type for the client before instantiating the object (not recommended) RemotingConfiguration.RegisterActivatedClientType (typeof (ITax), "tcp://localhost:8085/Tax1"); / / Activator.GetObjectTax obj = new Tax () Obj.GetTax (); 4. Close the logout channel channel.StartListening (null); ChannelServices.UnregisterChannel (channel); 4. Use the configuration file to rewrite the above distributed program server: RemotingConfiguration.Configure ("RemotingServerHostByConfig.exe.config", false)

The content of the server-side configuration file app.config is

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report