您的位置:首页 > 编程语言 > VB

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

2008-11-30 16:48 387 查看
  <script type="text/javascript"><!--
google_ad_client = "pub-8333940862668978";
/* 728x90, 创建于 08-11-30 */
google_ad_slot = "4485230109";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

在.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

' 进程的主入口点

<MTAThread()> _

Shared Sub Main()

Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' 在同一进程中可以运行不止一个 NT 服务。若要将

' 另一个服务添加到此进程,请更改下行以

' 创建另一个服务对象。例如,

'

' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}

'

ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End Sub

'组件设计器所必需的

Private components As System.ComponentModel.IContainer

'注意: 以下过程是组件设计器所必需的

' 可以使用组件设计器修改此过程。

' 不要使用代码编辑器修改它。

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

'

'Service1

'

Me.ServiceName = "Server"

End Sub

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)

' 在此处添加启动服务的代码。此方法应设置具体的操作

' 以便服务可以执行它的工作。

Try

Dim ch As New Tcp.TcpChannel(8212) '监听端口是在8212,你可以修改该端口

ChannelServices.RegisterChannel(ch) '注册端口

Remoting.RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("serviceShare.serviceShare,serviceShare", True, True), "server", WellKnownObjectMode.Singleton)

'Type.GetType中的String是需要引用的服务所在的位置,"serviceShare.serviceShare,serviceShare" 中前面俩个serviceShare是指服务所在的程序集中的要做服务的类。逗号后面的serviceShare是指该程序集位于的文件。后面的第三个参数:True就是表示要搜索该文件时不区分大小写。"server"表示服务的名称。

Catch ex As Exception

EventLog.WriteEntry("日志 " & ex.Message)

End Try

End Sub

Protected Overrides Sub OnStop()

' 在此处添加代码以执行停止服务所需的关闭操作。

Try

Dim ch As New Tcp.TcpChannel(8212)

ChannelServices.UnregisterChannel(ch)

Catch ex As Exception

EventLog.WriteEntry("日志 " & ex.Message)

End Try

End Sub

End Class

serviceShare.vb '该文件为接口的实现文件,可以修改这个文件获得自己需要的服务,可以在这里引用  '其他DLL中的方法

Public Class serviceShare

Inherits MarshalByRefObject

Implements share.Iconnect

Private shared i As Int32 = 0

Public Function getName() As String Implements share.Iconnect.getName

i = i + 1

Return "from Server " & i

End Function

End Class

[客户端]

form1.vb 

Imports System

Imports System.Runtime

Imports System.Runtime.Remoting

Imports System.Runtime.Remoting.Channels

Public Class Form1

Inherits System.Windows.Forms.Form

Private ch As Tcp.TcpChannel

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()

MyBase.New()

'该调用是 Windows 窗体设计器所必需的。

InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 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

'Windows 窗体设计器所必需的

Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的

'可以使用 Windows 窗体设计器修改此过程。

'不要使用代码编辑器修改它。

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(80, 50)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(125, 25)

Me.Label1.TabIndex = 0

Me.Label1.Text = "Label1"

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(105, 195)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(75, 25)

Me.Button1.TabIndex = 1

Me.Button1.Text = "Button1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Button1)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim serverName As String

Dim aa As share.Iconnect

serverName = "tcp://127.0.0.1:8212/server"

aa = CType(Activator.GetObject(Type.GetType("share.Iconnect,share", True, True), serverName), share.Iconnect)

'注意这个地方

Label1.Text = aa.getName()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

ch = New Tcp.TcpChannel '客户端可以不注册端口

ChannelServices.RegisterChannel(ch)

End Sub

End Class

[服务安装]

Strart.vb '服务安装

Module Strart

Sub Main(ByVal arg() As String)

On Error Resume Next

#If DEBUG Then

If IO.File.Exists("setup.bat") Then '批处理附后面

Shell("setup.bat", , True)

End If

#End If

If (IO.File.Exists("testService.exe")) Then

Shell(share.app.winPath & "/Microsoft.NET/Framework/v1.1.4322/InstallUtil.exe " _

& share.app.appPath & "/testService.exe /LogFile", AppWinStyle.Hide, True)

Dim Sc2 As New System.ServiceProcess.ServiceController("server")

If Sc2.Status = ServiceProcess.ServiceControllerStatus.Stopped Then

Sc2.Start()

End If

End If

End Sub

End Module

[服务卸载]

UnSetup.vb

Module strart

Sub Main()

On Error Resume Next

If (IO.File.Exists("testservice.exe")) Then

Dim Sc1 As New System.ServiceProcess.ServiceController("server")

If Sc1.Status = ServiceProcess.ServiceControllerStatus.Running Then

Sc1.Stop()

End If

Shell(share.app.winPath & "/Microsoft.NET/Framework/v1.1.4322/InstallUtil.exe /u " _

& share.app.appPath & "/testservice.exe /LogFile", AppWinStyle.Hide, True)

End If

End Sub

End Module

[批处理]

copy ../../serviceShare/bin/serviceShare.dll ./serviceShare.dll

copy ../../test/bin/test.exe ./test.exe

copy ../../shared/bin/share.dll ./share.dll

copy ../../UnSetup/bin/UnSetup.exe ./UnSetup.exe

copy ../../testService/bin/testService.exe ./testService.exe

这样能方便的扩展自己的功能,因为很多书上的代码,使用都是抄微软的,如果程序是分开独立制作,只公布接口的话,安微软的做法就很难成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息