在.NET下的remoting使用里面,很多的书上都是使用了客户和服务器端都是使用一样共享成员或接口的示例来做说明。而且在实际的使用中有不小的问题。
[共享代码]
share.vb
Imports System.windows.forms
Public Interface Iconnect '客户端和服务器端同时共享使用一个接口
Function getName() As String
End Interface
Public Class app
Public Shared ReadOnly Property appPath() As String '提供一些应用程序常用路径将会在服务安装的 Get '过程中被使用
Return Application.StartupPath
End Get
End Property
Public Shared ReadOnly Property winPath() As String
Get
Return System.Environment.GetEnvironmentVariable("windir")
End Get
End Property
End Class
[服务器端] '共俩个文件,第一个是服务文件,第二个是服务调用的功能实现文件
service1.vb '引用了System.Runtime.Remoting.dll文件,这是服务
Imports System.ServiceProcess
Imports System.Runtime
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
#Region " 组件设计器生成的代码 "
Public Sub New()
MyBase.New()
' 该调用是组件设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化
End Sub
'UserService 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' 进程的主入口点
1<mtathread()> _
2Shared Sub Main()
3Dim ServicesToRun() As System.ServiceProcess.ServiceBase
4
5' 在同一进程中可以运行不止一个 NT 服务。若要将
6' 另一个服务添加到此进程,请更改下行以
7' 创建另一个服务对象。例如,
8'
9' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
10'
11ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1}
12
13System.ServiceProcess.ServiceBase.Run(ServicesToRun)
14End Sub
15
16'组件设计器所必需的
17Private components As System.ComponentModel.IContainer
18
19'注意: 以下过程是组件设计器所必需的
20' 可以使用组件设计器修改此过程。
21' 不要使用代码编辑器修改它。
22<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
23'
24'Service1
25'
26Me.ServiceName = "Server"
27
28End Sub
29
30#End Region
31
32Protected Overrides Sub OnStart(ByVal args() As String)
33' 在此处添加启动服务的代码。此方法应设置具体的操作
34' 以便服务可以执行它的工作。
35Try
36Dim ch As New Tcp.TcpChannel(8212) '监听端口是在8212,你可以修改该端口
37ChannelServices.RegisterChannel(ch) '注册端口
38Remoting.RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("serviceShare.serviceShare,serviceShare", True, True), "server", WellKnownObjectMode.Singleton)
39
40'Type.GetType中的String是需要引用的服务所在的位置, "serviceShare.serviceShare,serviceShare" 中前面俩个serviceShare是指服务所在的程序集中的要做服务的类。逗号后面的serviceShare是指该程序集位于的文件。后面的第三个参数:True就是表示要搜索该文件时不区分大小写。"server"表示服务的名称。
41
42Catch ex As Exception
43EventLog.WriteEntry("日志 " & ex.Message)
44End Try
45
46End Sub
47
48Protected Overrides Sub OnStop()
49' 在此处添加代码以执行停止服务所需的关闭操作。
50Try
51Dim ch As New Tcp.TcpChannel(8212)
52ChannelServices.UnregisterChannel(ch)
53Catch ex As Exception
54EventLog.WriteEntry("日志 " & ex.Message)
55End Try
56
57End Sub
58
59End Class
60
61serviceShare.vb '该文件为接口的实现文件,可以修改这个文件获得自己需要的服务,可以在这里引用 '其他DLL中的方法
62Public Class serviceShare
63Inherits MarshalByRefObject
64Implements share.Iconnect
65Private shared i As Int32 = 0
66
67Public Function getName() As String Implements share.Iconnect.getName
68i = i + 1
69Return "from Server " & i
70End Function
71End Class
72
73[客户端]
74form1.vb
75Imports System
76Imports System.Runtime
77Imports System.Runtime.Remoting
78Imports System.Runtime.Remoting.Channels
79
80Public Class Form1
81Inherits System.Windows.Forms.Form
82Private ch As Tcp.TcpChannel
83
84#Region " Windows 窗体设计器生成的代码 "
85
86Public Sub New()
87MyBase.New()
88
89'该调用是 Windows 窗体设计器所必需的。
90InitializeComponent()
91
92'在 InitializeComponent() 调用之后添加任何初始化
93
94End Sub
95
96'窗体重写 dispose 以清理组件列表。
97Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
98If disposing Then
99If Not (components Is Nothing) Then
100components.Dispose()
101End If
102End If
103MyBase.Dispose(disposing)
104End Sub
105
106'Windows 窗体设计器所必需的
107Private components As System.ComponentModel.IContainer
108
109'注意: 以下过程是 Windows 窗体设计器所必需的
110'可以使用 Windows 窗体设计器修改此过程。
111'不要使用代码编辑器修改它。
112Friend WithEvents Label1 As System.Windows.Forms.Label
113Friend WithEvents Button1 As System.Windows.Forms.Button
114<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
115Me.Label1 = New System.Windows.Forms.Label
116Me.Button1 = New System.Windows.Forms.Button
117Me.SuspendLayout()
118'
119'Label1
120'
121Me.Label1.Location = New System.Drawing.Point(80, 50)
122Me.Label1.Name = "Label1"
123Me.Label1.Size = New System.Drawing.Size(125, 25)
124Me.Label1.TabIndex = 0
125Me.Label1.Text = "Label1"
126'
127'Button1
128'
129Me.Button1.Location = New System.Drawing.Point(105, 195)
130Me.Button1.Name = "Button1"
131Me.Button1.Size = New System.Drawing.Size(75, 25)
132Me.Button1.TabIndex = 1
133Me.Button1.Text = "Button1"
134'
135'Form1
136'
137Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
138Me.ClientSize = New System.Drawing.Size(292, 273)
139Me.Controls.Add(Me.Button1)
140Me.Controls.Add(Me.Label1)
141Me.Name = "Form1"
142Me.Text = "Form1"
143Me.ResumeLayout(False)
144
145End Sub
146
147#End Region
148
149Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
150Dim serverName As String
151Dim aa As share.Iconnect
152serverName = "tcp://127.0.0.1:8212/server"
153aa = CType(Activator.GetObject(Type.GetType(" share.Iconnect,share ", True, True), serverName), share.Iconnect)
154'注意这个地方
155Label1.Text = aa.getName()
156End Sub
157
158Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
159ch = New Tcp.TcpChannel '客户端可以不注册端口
160ChannelServices.RegisterChannel(ch)
161End Sub
162End Class
163
164[服务安装]
165Strart.vb '服务安装
166Module Strart
167Sub Main(ByVal arg() As String)
168On Error Resume Next
169#If DEBUG Then
170If IO.File.Exists("setup.bat") Then '批处理附后面
171Shell("setup.bat", , True)
172End If
173#End If
174If (IO.File.Exists("testService.exe")) Then
175Shell(share.app.winPath & "\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe " _
176& share.app.appPath & "\testService.exe /LogFile", AppWinStyle.Hide, True)
177Dim Sc2 As New System.ServiceProcess.ServiceController("server")
178If Sc2.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
179Sc2.Start()
180End If
181End If
182End Sub
183End Module
184
185[服务卸载]
186UnSetup.vb
187Module strart
188Sub Main()
189On Error Resume Next
190If (IO.File.Exists("testservice.exe")) Then
191Dim Sc1 As New System.ServiceProcess.ServiceController("server")
192If Sc1.Status = ServiceProcess.ServiceControllerStatus.Running Then
193Sc1.Stop()
194
195End If
196Shell(share.app.winPath & "\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe /u " _
197& share.app.appPath & "\testservice.exe /LogFile", AppWinStyle.Hide, True)
198End If
199End Sub
200End Module
201
202[批处理]
203copy ..\\..\serviceShare\bin\serviceShare.dll .\serviceShare.dll
204copy ..\\..\test\bin\test.exe .\test.exe
205copy ..\\..\shared\bin\share.dll .\share.dll
206copy ..\\..\UnSetup\bin\UnSetup.exe .\UnSetup.exe
207copy ..\\..\testService\bin\testService.exe .\testService.exe
208
209这样能方便的扩展自己的功能,因为很多书上的代码,使用都是抄微软的,如果程序是分开独立制作,只公布接口的话,安微软的做法就很难成功。
210
211
212示例虽然是在一个程序中实现的,但在俩个程序里面也不会有问题,大家可以自己试。
213
214
215点击下载所需代码</system.diagnostics.debuggerstepthrough()></system.diagnostics.debuggerstepthrough()></mtathread()>