(Original) One step-by-step learning Remoting: starting with simplicity

Source: Internet
Author: User

One step to learn Remoting: start with simple

I. What are the advantages and disadvantages of Remoting?
Advantages:
1. allows us to conduct distributed development
2. Fast Tcp channel Remoting
3. Although it is remote, It is very close to the local call object.
4. The object state can be maintained.
5. There are no application restrictions. You can use the console, winform, iis, and windows Services to host remote objects.
Disadvantages:
1. Non-standard applications have platform restrictions
2. If you leave iis, you need to have your own security mechanism.

Ii. What is the difference between Remoting and Web Services?
The basic structure of ASP. NET Web Services provides simple APIs for Web Services by ing SOAP messages to method calls. This mechanism is implemented by providing a very simple programming model (based on ing SOAP message exchange to method calls. Clients of ASP. NET Web services do not need to understand the platform, object model, or programming language used to create them. Services do not need to know the clients that send messages to them. The only requirement is that both parties should recognize the SOAP message format being created and used, which is composed of the WSDL and XML architecture (XSD) web Service Contract definition.
. NET Remoting provides a basic structure for distributed objects. It uses flexible and scalable pipelines to provide full object semantics of. NET to remote processes. ASP. NET Web Service provides a very simple programming model based on message transmission. NET Remoting provides complex functions, including passing objects, callbacks, and multi-object activation and lifecycle management policies through values or references. To use. NET Remoting, the client needs to understand all the details. In short, you need to use. NET to create a client .. The NET Remoting pipeline also supports SOAP messages, but note that this does not change the client requirements. If the Remoting endpoint provides. NET-specific object semantics, the client must understand them whether or not they are using SOAP.

Iii. Simplest Remoting example
1. Remote Object:
Create a class library project: RemoteObject

Using System;

Namespace RemoteObject
{
Public class MyObject: MarshalByRefObject
{
Public int Add (int a, int B)
{
Return a + B;
}
}
}

2. Server
Create a console project: RemoteServer

Using System;
Using System. Runtime. Remoting;

Namespace RemoteServer
{
Class MyServer
{
[STAThread]
Static void Main (string [] args)
{
RemotingConfiguration. Configure ("RemoteServer.exe. config ");
Console. ReadLine ();
}
}
}

Create a configuration file: app. config <configuration>
<System. runtime. remoting>
<Application name = "RemoteServer">
<Service>
<Wellknown type = "RemoteObject. MyObject, RemoteObject" objectUri = "RemoteObject. MyObject"
Mode = "Singleton"/>
</Service>
<Channels>
<Channel ref = "tcp" port = "9999"/>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>

3. Client:
Create a console project: RemoteClient

Using System;

Namespace RemoteClient
{
Class MyClient
{
[STAThread]
Static void Main (string [] args)
{
RemoteObject. MyObject app = (RemoteObject. MyObject) Activator. GetObject (typeof (RemoteObject. MyObject), System. Configuration. ConfigurationSettings. deleettings ["ServiceURL"]);
Console. WriteLine (app. Add (1, 2 ));
Console. ReadLine ();
}
}
}


Create a configuration file: app. config <configuration>
<Deleetask>
<Add key = "ServiceURL" value = "tcp: // localhost: 9999/RemoteObject. MyObject"/>
</AppSettings>
</Configuration>

4. Test
During the final compilation, the following error occurs:
1. app. Add () not found ()
2. RemoteObject not found
This is because the RemoteClient of the client does not add a reference to the RemoteObject, And the compiler does not know which members of the remote object exist. Therefore, an error is reported. After adding a reference, vs.net will save a dll on the client, it may be difficult to modify remote objects? In fact, it is not troublesome. Once the project is compiled, vs.net will re-copy the dll.
Then, running the client directly will cause an exception "the target host is denied", indicating that the channel is not opened.
After running the server and then running the client, "the Assembly RemoteObject is not found" is displayed "! Looking back, we can see that we have added a reference to RemoteObject on the server. during compilation, this is because remote objects are not used at this time. Do you probably not understand that remote objects are also used when running the server? This is because the remote object is not activated at this time. Of course, we need to add remote object references to the server. After all, our objects are carried remotely.
Now run the server program and client program successively. The client program shows 3. The test is successful.

Iv. Conclusion
We use a simple example to implement the simplest remoting, without any introduction to its actual quality. It is the simplest to get started with an example.

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.