Step by step remoting 4: bearer mode (1)-Windows Service

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.
Relevant information can be found in msdn. Article :

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.

Create a remote object firstUsingSystem;
UsingSystem. Data;
UsingSystem. Data. sqlclient;

Namespace Remoteobject
{
Public   Class Myobject: marshalbyrefobject
{
Public Dataset getdata ()
{
Sqlconnection Conn = New Sqlconnection (system. configuration. configurationsettings. receivettings [ " Strconn " ]);
Sqldataadapter da = New Sqldataadapter ( " Select * From ubi_provincemaster " , Conn );
Dataset DS = New Dataset ();
Da. Fill (DS );
Return DS;
}
}
}

The client is still a console for testing: Remoteobject. myobject app = (Remoteobject. myobject) activator. GetObject ( Typeof (Remoteobject. myobject), system. configuration. configurationsettings. deleetask [ " 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 >
< Appsettings >
< 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 >

RunProgram, 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. Enable service1CodeView, locate the onstart part, and add the code

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

(do not omit the appdomain. currentdomain. basedirectory +)
the config method is the same as that in the console. we allow this Windows service to only 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 named remoteserversetup (we have created an installation project for the service just now)
7. Right-click "add"> "project output"> "primary output" and choose "remoteservice1"> "OK".
8. Right-click "View"> "Custom operation" and choose "Custom operation"> "add custom operation ". define operation-open the application folder-select the primary output just now-OK
9. regenerate the Security Installation Project-right-click-install
10. In the Service Manager (my computer-right-click-Manage-services and applications-services), find the remoteserver1 service, start the service
now you can open the client to test it!

some FAQs:
1. when starting the service, the system said something like "nothing is done, and the service has been stopped?
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 the client is running, "the server has no response "?
first, check whether the activation mode and object are correctly set in the Windows service configuration file, and whether the client server port number is consistent?
3. When the client is running, "cannot find the assembly" is displayed "?
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. system. configuration. configurationsettings. configurettings ["strconn"] is used in the remote object class, but the remote object does not have a configuration file. Where does it read The Config?
because remote objects do not exist independently and are hosted by Windows Services, it reads some configuration information from the configuration file of Windows Services, remote Object does not need configuration files.
5. Do you want to uninstall the service during installation?
NO, the installer stops the server-> uninstall the service-> install 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 burden on the Web server. All logic code is distributed to different service servers.

the last tip of the test:
if it is very difficult for us to remotely call the object for testing, we need to do this
modify the remote object-> recompile the installer-> reinstall the service on your machine-> start the service-> View the result
actually, you can do this.:
1. Modify the Connection database 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 it:
sqlconnection conn = new sqlconnection ("Server = (local); uid = sa; Pwd =; database = Ubisoft");
2. Modify the client code, 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

Related Article

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.