使用VB.Net Express版本创建服务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用VB.Net Express版本创建服务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7996字,纯文字阅读大概需要12分钟。
内容图文
![使用VB.Net Express版本创建服务](/upload/InfoBanner/zyjiaocheng/1121/88cb8913798c4d09acc9c1728c3684e0.jpg)
Services Part 1:> Creating Services
Visual Basic Express is a great, free tool from Microsoft. You don’t get all that Visual Studio 2005 offers though. One of the things VB Express does not have is templates to create a Windows Service.
We can still make Services with VB 2005 Express. All it takes is a little manual work. I will walk you thru a example on how to make a service in Visual Basic Express.
Choose Console Application, and give it the name of NewService1.
It will open in the designer with a module. Rename the module to NewService1.
Now we have to get some references for our Service. In the solution Explorer, double click on My Project and choose the References tab.
We need to Add the following references:
System.Configuration.Install
System.ServiceProcess
Now back to the NewService1 Module. We will be replacing all the text in the module, thus turning it into a class. Replace all the text with the text below:
Imports System.ServiceProcess Imports System.Configuration.Install Public Class NewService1 Inherits System.ServiceProcess.ServiceBase Friend WithEvents Timer1 As System.Timers.Timer PublicSubNew() MyBase.New() InitializeComponents() ‘ TODO: Add any further initialization codeEnd SubPrivateSub InitializeComponents() Me.ServiceName = "NewService1″"Me.AutoLog = TrueMe.CanStop = TrueMe.Timer1 = New System.Timers.Timer() Me.Timer1.Interval = 6000Me.Timer1.Enabled = TrueEnd Sub‘ This method starts the service. <MTAThread()> SharedSub Main() ‘ To run more than one service you have to add them to the array System.ServiceProcess.ServiceBase.Run(New System.ServiceProcess.ServiceBase() {New NewService1}) End Sub‘ Clean up any resources being used.ProtectedOverloadsOverridesSub Dispose(ByVal disposing AsBoolean) MyBase.Dispose(disposing) ‘ TODO: Add cleanup code here (if required)End SubProtectedOverridesSub OnStart(ByVal args() AsString) ‘ TODO: Add start code here (if required)‘ to start your service.Me.Timer1.Enabled = TrueEnd SubProtectedOverridesSub OnStop() ‘ TODO: Add tear-down code here (if required)‘ to stop your service.Me.Timer1.Enabled = FalseEnd SubPrivateSub InitializeComponent() Me.Timer1 = New System.Timers.Timer CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit() ‘ ‘Timer1‘ Me.Timer1.Enabled = TrueCType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit() End SubPrivateSub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed Dim MyLog AsNew EventLog() ‘ create a new event log‘ Check if the the Event Log ExistsIfNot MyLog.SourceExists(“NewService1″) Then MyLog.CreateEventSource(“NewService1″, “NewService1 Log”) ‘ Create LogEndIf MyLog.Source = "NewService1″" MyLog.WriteEntry("NewService1 Log", "It is running", EventLogEntryType.Information) ‘disable the timer so you dont fill up the log Timer1.Enabled = FalseEnd SubEnd Class
This will create the NewService class and insert a Timer into your
designer.
If you Double-Click on the NewService1 in the
SolutionExplorer, you will see the designer and the Timer1 object.
This code will run after 6 seconds has passed, and will insert a log entry for our service saying “ It is running”.
We need to set the Service Name in the Designer:
Double click on NewService1 to get the gray page. Then click somewhere in the grey to get the properties for this class.
Under ServiceName put NewService1.
Services Part 2:> Creating the needed Installers
This is not the Actual “Installer”. This is code needed for the Service to operate correctly.
We will get to Installing Later…
Add the Installer Code:
Create a new class file in your project and call it ProjectInstaller.
We wil replace the text in the ProjectInstaller class with the following code:
Imports System.ComponentModel Imports System.Configuration.Install <RunInstaller(True)> PublicClass ProjectInstaller Inherits System.Configuration.Install.Installer ‘Installer overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ ProtectedOverridesSub Dispose(ByVal disposing AsBoolean) TryIf disposing AndAlso components IsNotNothingThen components.Dispose() EndIfFinallyMyBase.Dispose(disposing) EndTryEnd Sub‘Required by the Component DesignerPrivate components As System.ComponentModel.IContainer ‘NOTE: The following procedure is required by the Component Designer‘It can be modified using the Component Designer.‘Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ PrivateSub InitializeComponent() Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller ‘ ‘ServiceProcessInstaller1‘ Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem Me.ServiceProcessInstaller1.Password = NothingMe.ServiceProcessInstaller1.Username = Nothing‘ ‘ServiceInstaller1‘ Me.ServiceInstaller1.ServiceName = "NewService1″"Me.ServiceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic ‘ ‘ProjectInstaller‘ Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1}) End SubFriendWithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller FriendWithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller PublicSubNew() MyBase.New() ‘This call is required by the Component Designer. InitializeComponent() ‘Add initialization code after the call to InitializeComponentEnd SubEnd Class
This will create the ProjectInstaller class and insert 2 Objects in the designer:
ServiceInstaller1 – Settings: The Service Name, Display Name, description, StartType
ServiceProcessInstaller – Settings: The Account the service will run under
In this example, the settings are set as follows:
ServiceName: NewService1
DisplayName: {blank}
Description: {blank}
StartType:
Automatic – (will auto start on reboot)
Account: LocalSystem – (do not
need user/password. This will run under System account)
Now we have to build the project.
Services Part 3:> Build and Install your Service
Build and Install service on your pc:
Go to build menu and choose Build NewService1 – This will create an EXE file in our project’s \bin\Release folder.
Next we will need to use the InstallUtil.exe to install the service into the PC.
InstallUtil.exe is part of the Dot Net distribution package.
It is
found in the c:\windows\Microsoft.net\Firmware\v2.0.50727\ folder.
Its command line usage is:
Install Service:
Installutil.exe PATH_TO_EXE.exe
Uninstall Service:
InstallUtil.exe PATH_TO_EXE.exe /u
For this example, I changed directory into the
c:\windows\Microsoft.net\Firmware\v2.0.50727\ folder.
Then I ran:
InstallUtil.exe “c:\vb2005\projects\NewService1\bin\Release\NewService1.exe”
The installer should give you lots of text, It should end by saying:
The Commit phase completed successfully.
The transacted install
has completed.
If there are any error messages in the install of the service, it will list them.
After successful installation, you should see the Service listed as
NewService1 in the Service Manager.
To get the service manager, Right
click on My Computer on the desktop, and choose Manage.
Then choose Services and Applications > Services. Right-Click on the NewService1 and choose Start.
Then go to System Tools > Event Viewer > Application, and you should see your log entries.
The service will make one entry saying “It is running” then disable the Timer so your log doesn’t fill up.
To install this service on another computer:
Prerequisites: You need to have .net distributable 2.0 on the target PC.
You need to copy over the files in the Release folder to the target computer. Then inside the c:\windows\Microsoft.net\Firmware\v2.0.50727\ folder run the InstallUtil.exe program as described previously.
That’s It.
原文:http://www.cnblogs.com/lbnnbs/p/3642927.html
内容总结
以上是互联网集市为您收集整理的使用VB.Net Express版本创建服务全部内容,希望文章能够帮你解决使用VB.Net Express版本创建服务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。