Address: http://hi.baidu.com/grayworm/blog/item/1d2ccdd6cc23b12506088b6b.html
PreviousArticleThrough the ConsoleProgramOr the winform program to provide the WCF Service for the local machine or remote machine. We call this method the self-host service release method, this article describes how to compile a Windows service to publish the WCF Service.
Step 1: Create the Windows Service Program windowsservicehost
Figure 1
Figure 2
Step 2: Add two references to the consolebookservicehost program: one is the reference of the WCF Service Library Services project, and the other is the reference of system. servicemodel.
Step 3: Modify service1 attributes
In the design interface of service1, right-click and select "attribute" to change both (name) and servicename to bookservicehost.
Figure 3
Step 4: Write in service1CodeAs follows:
Figure 4
Step 5: Configure app. config.
Here we will not describe how to configure it in detail. We copied the app. config In the last selfhostbookservice project, but we can
Figure 5
Here we have made a Windows service host program that can publish the bookservice service.
Next, let's take a look at how to run this Windows service.
Step 6: add the installer for the service.
On the service1 design page, right-click and select "add installer"
Figure 6
Generate the installer component. The following page is displayed:
Figure 7
Serviceprocessinstaller1: service installation information
Figure 8
Serviceinstaller1: Service Information
Figure 9
Compiled successfully
Step 7: Install the services compiled above
Open the vs2008 command window, enter the bin/debug folder of the current project, and execute the command:Installutil windowsservicehost.exe
Figure 10
Figure 11
Step 8: Start the bookservicehost Service
Open the Service Manager and we can see that the service we just registered already exists, but it hasn't been started yet. Right-click Start"
Figure 12
Step 9: test the service:
Open IE and enter:Http: // localhost: 8081/bookserviceThe following interface is displayed:
Figure 13
In the vs2008 command window, enter:WcftestclientHttp: // localhost: 8081/bookserviceThe following interface is displayed:
Figure 14
Call the method of the corresponding endpoint to add books. The operation is normal.
========================== ========================================================== ============
The following describes how to package and deploy Windows Services.
Windows Services are called NT Services in earlier versions of Visual Studio, and new names are enabled in vs.net. Using Visual C # To create a Windows service is not difficult. This article will guide you step by step to create a Windows service and use it. When the service starts and stops, it writes some text information to a text file.
Step 1: create a service framework
To create a new Windows service, you can select the Windows Service option from the Visual C # project, give the project a new file name, and click OK.
You can see that the wizard adds the webservice1.cs class to the project file:
The meaning of each attribute is:
Ü autolog: whether to automatically write data to System Log Files
Ücanhandlepowerevent receives power events during the service
Ü whether the canpauseandcontinue Service accepts requests for suspending or resuming running the service
Ü whether the canshutdown Service receives a notification when the computer that runs it shuts down, so that it can call the onshutdown Process
Ü whether the canstop Service accepts the stop operation request
Ü servicename service name
Step 2: add features to the service
In the. CS code file, we can see that there are two ignored functions onstart and onstop.
The onstart function is executed when the service is started, and the onstop function is executed when the service is stopped. Here, when the service is started and stopped, some text information is written to a text file. The Code is as follows:
Protected override void onstart (string [] ARGs)
{
Filestream FS = new filestream (@ "D: \ mcwindowsservice.txt", filemode. openorcreate, fileaccess. Write );
Streamwriter m_streamwriter = new streamwriter (FS );
M_streamwriter.basestream.seek (0, seekorigin. End );
M_streamwriter.writeline ("mcwindowsservice: Service started" + datetime. Now. tostring () + "\ n ");
M_streamwriter.flush ();
M_streamwriter.close ();
FS. Close ();
}
Protected override void onstop ()
{
Filestream FS = new filestream (@ "D: \ mcwindowsservice.txt", filemode. openorcreate, fileaccess. Write );
Streamwriter m_streamwriter = new streamwriter (FS );
M_streamwriter.basestream.seek (0, seekorigin. End );
M_streamwriter.writeline ("mcwindowsservice: Service stopped" + datetime. Now. tostring () + "\ n ");
M_streamwriter.flush ();
M_streamwriter.close ();
FS. Close ();
}
Step 3: add the installer to the Service Application
Visual Studio. NET comes with the installation component that can be used to install resources associated with service applications. The installation component registers a single service on the system to which it is being installed, and notifies the Service Control Manager of the existence of the service.
To correctly install the service, you do not need to perform any special encoding in the installer. However, to add special features to the installation process, you may occasionally need to modify the content of the installation program.
To add an installer to a service application, follow these steps:
1: In the solution, access the design view of the service to which you want to add and install components.
2: In the Properties window, click Add installer Link
In this case, a new projectinstaller class and two installation components serviceprocessinstaller and serviceinstaller are added to the project, and the service attribute values are copied to the component.
3: To determine how to start the service, click the serviceinstaller component and set the starttype attribute to an appropriate value.
Ümanual must be started manually after the service is installed.
Ü automatic the service is automatically started every time the computer restarts.
Ü the disabled service cannot be started.
4: Change the account attribute of the serviceprocessinstaller class to LocalSystem.
In this way, no matter which user is logged on to the system, the service will always start.
Step 4: generate a service program
You can select generate from the generate menu to generate a project.
Do not run the project by pressing F5-you cannot run the service project in this way.
Step 5: Install the service
Access the directory of compiled executable files in the project.
Use the project output as a parameter to run installutil.exe from the command line. Enter the following code in the command line:
Installutil yourproject.exe
Uninstall Service
Use the project output as a parameter to run installutil.exe from the command line.
Installutil/u yourproject.exe
Http://www.cnblogs.com/laiwen/archive/2005/08/21/219590.html
Packaging:
Create an installation project for the service
On the "file" menu, point to "add project" and select "new project ".
In the project type pane, select the installation and deployment projects folder.
Select "Installation Project" in the "template" pane ". Name the project myservicesetup.
The installation project is immediately added to the solution.
Then, add the output mynewservice.exe of the Windows Service Project to the installation project.
Add mynewservice.exe to the installation project
In Solution Explorer, right-click "myservicesetup", point to "add", and select "project output ".
The "add project output group" dialog box appears.
Select "mynewservice" in the "project" box ".
In the list box, select "primary output" and click "OK ".
The main output project of mynewservice is added to the installation project. Now add a custom operation to install the mynewservice.exe file.
Add custom operations to the installation project
In Solution Explorer, right-click the installation project, point to "View", and select "Custom operations ".
The custom operation editor is displayed.
In the "Custom operations" editor, right-click the "Custom operations" node and select "add custom operations ".
The "select items in project" dialog box appears.
Double-click "application folder" in the list box to open it, select "main output from mynewservice (activity)", and click "OK ".
The master output is added to all four nodes in the Custom operation, namely, "Install", "Submit", "rollback", and "Uninstall ".
In Solution Explorer, right-click the myservicesetup project and select generate ".
Install Windows Services
To install myeventlog.exe, right-click the installation project in Solution Explorer and select "Install ".
Start and Stop services
Open the Service Control Manager by performing one of the following operations:
In Windows 2000 Professional, right-click my computer on the desktop and Click Manage ". On the "Computer Management" console, expand the "services and applications" node.
-Or-
On Windows 2000 Server, click Start, point to programs, click Administrative Tools, and then click Services ".
Note: in Windows NT 4.0, you can open this dialog box from the control panel.
Now we can see that mynewservice is listed in the "service" Area of the window.
Select your service from the list, right-click the service, and click Start ".
Right-click the service and click "stop ".
Verify service event log output
Open the server resource manager and access the "event log" node. For more information, see process event logs in server resource manager.
Note that the server resource manager "server" node is unavailable in Visual Basic and Visual C #. Net Standard Edition. For more information, see Visual Basic Standard Edition or Visual C # Standard Edition.
Find the list of mynewlog and expand it. You should see the items that the Service performs operations.
Uninstall Service
On the Start Menu, open control panel, click add or delete programs, find your service, and click Delete ".
You can also right-click the program icon of the. MSI file and select "Uninstall" to uninstall the program.
Note: If the service is installed on Windows 2000, you must restart the system before you can reinstall the service. In Windows 2000, the service is not completely deleted before the system restarts.
==================
The above tests are normal.