The remoting method is as follows:

Source: Internet
Author: User
In practical applications, we usually only use Windows Services and IIS to host remote objects. The reason for choosing a Windows service is that the service can be started automatically. After the server is restarted, you do not need to start the service again. The reason for selecting IIS is that we can use some IIS features such as integration verification.
You can find related articles in msdn:

Http://www.microsoft.com/china/msdn/library/architecture/architecture/architecturetopic/BuildSucApp/BSAAsecmodsecmod29.mspx

Http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cpguide/html/cpconremotingexamplehostinginiis. asp

You may think that this process will be a complicated process. Otherwise, we will discuss the implementation method below, with few steps.

First, create the remote object using system;
Using system. Data;
Using system. Data. sqlclient;

Namespace remoteobject
{
Public class myobject: marshalbyrefobject
{
Public dataset getdata ()
{
Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["strconn"]);
Sqldataadapter da = new sqldataadapter ("select * From ubi_provincemaster", Conn );
Dataset DS = new dataset ();
Da. Fill (DS );
Return Ds;
}
}
}

The client is still in the console for testing: remoteobject. myobject APP = (remoteobject. myobject) activator. getObject (typeof (remoteobject. myobject), system. configuration. configurationsettings. appsettings ["serviceurl"]);
Datatable dt = app. getdata (). Tables [0];
Foreach (datarow DR in DT. Rows)
{
Console. writeline (Dr ["iprmid"] + "" + Dr ["vprmname"]);
}
Console. Readline ();

Server Configuration File: <configuration>
<Deleetask>
<Add key = "strconn" value = "Server = (local); uid = sa; Pwd =; database = Ubisoft"/>
</Appsettings>
<System. runtime. remoting>
<Application name = "remoteserver">
<Service>
<Wellknown type = "remoteobject. myobject, remoteobject" objecturi = "remoteobject. myobject"
Mode = "singlecall"/>
</Service>
<Channels>
<Channel ref = "TCP" Port = "9999"/>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>

Run the program and we get a list of provinces and cities:

I. Windows Service bearer
The process of using vs.net to create a Windows service is basically no more than 10 steps, so we do not need to be afraid.
1. Create a new Windows Service Project remoteserver1
2. Open the service1 Code view, locate the onstart part, and add the code.

System. runtime. remoting. remotingconfiguration. Configure (appdomain. currentdomain. basedirectory + "remoteserver1.exe. config ");

(Do not omit appdomain. currentdomain. basedirectory +)
Config is the same as the config in the console. What we want this Windows service to do is read the configuration information from the config file for the configuration channel. Do not forget to add the configuration file.
3. Switch to the design view, right-click and choose add installer.
4. Switch to the newly generated project installer. in the CS design view, locate serviceprocessinstaller1, set the account attribute to LocalSystem, and set the servicename attribute of serviceinstaller1 to remoteserver1 (Service name ), starttype attribute is set to automatic (the service is automatically started when the system starts)
5. Do not forget to reference the added remoteobject.
6. Create a new installation project remoteserversetup (we have set up an installation project for the service just now)
7. Right-click and choose "add"> "project output"> "Master output"> "remoteservice1"> "OK ".
8. Right-click a view and choose Custom operations. Right-click a custom operation and choose add custom operation from the shortcut menu to open the application folder. Then, select the primary output and click OK.
9. regenerate the installation project. Right-click it and choose install.
10. In the Service Manager (my computer-right-click-Manage-services and applications-services), find the remoteserver1 service and start the service.
Now you can open the client and test it!

Some FAQs:
1. When starting the service, the system said something similar to "nothing is done, the service has been stopped". What does it mean?
It indicates that the Windows Service has an error. Generally, there is a problem with the service program. check what the Service has done? Only one line of code is added to our program. This error is not generally returned.
2. When I run the client, why is "the server does not respond "?
First, check whether the activation mode and object are correctly set in the Windows service configuration file. Is the port number of the client server consistent?
3. Why does "the Assembly cannot be found" appear when running the client "?
Check whether the type and Uri of the activation object are correctly configured in the Windows service configuration file? Does the service add remote object reference?
4. The remote object class uses system. configuration. configurationsettings. configurettings ["strconn"], but the remote object does not have a configuration file. Where does it read this Config?
Because remote objects do not exist independently and are hosted by Windows Services, they read some configuration information from the configuration file of Windows Services. remote objects do not need a configuration file.
5. Do I need to uninstall the service during installation?
No. The installer stops the server and uninstalls the service.
6. How to deploy our system during official use?
If the client is a program, you only need to upload the three files under the installation project to the server for installation, configure the config file (such as the connection string), and enable the service. If the client is a website, install the service on the server, configure the config file (such as the connection string), and enable the service, finally, upload the website to the Web server (which may not be the same server as the service ).
7. Do I need to upload the remote object DLL during deployment?
No. You can see that the dll exists automatically in the installation project.
8. What are the characteristics of such a system?
One web server, multiple service servers, and multiple sqlservice servers have a low load on the Web server. All logic code is distributed to different service servers.

Finally, let's talk about a test tip:
If it is very difficult for us to remotely call objects to test programs, we need to do so.
Modify remote object-re-compile installer-re-install the service on your machine-start the service-view the result
In fact, you can do this:
1. Modify the connection string in the remote object. Because it is not a remote object, we must read the connection string from the local device. For example, we can directly modify the above column:
Sqlconnection conn = new sqlconnection ("Server = (local); uid = sa; Pwd =; database = Ubisoft ");
2. Modify the client code to directly instantiate a remote object // remoteobject. myobject APP = (remoteobject. myobject) activator. getObject (typeof (remoteobject. myobject), system. configuration. configurationsettings. appsettings ["serviceurl"]);
Remoteobject. myobject APP = new remoteobject. myobject ();

At the time of official deployment, we will restore the database connection string to read from the config file, and restore the remote object to read from the remote.

If you are not clear about the Windows Service, read the following article:
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/vbcon/html/vbwlk1_throughcreatingwindowsserviceapplication. asp

Http://www.cnblogs.com/lovecherry/archive/2005/03/25/125527.html

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.