[VB.NET]remoting客户和服务器共享成员或接口的示例

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值