前端之家收集整理的这篇文章主要介绍了
[VB.NET]remoting客户和服务器共享成员或接口的示例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- 在.NET下的remoting使用里面,很多的书上都是使用了客户和服务器端都是使用一样共享成员或接口的示例来做说明。而且在实际的使用中有不小的问题。
- [共享代码]
- share.vb
- ImportsSystem.windows.forms
- PublicInterfaceIconnect
- FunctiongetName()AsString
- EndInterface
- PublicClassapp
- PublicSharedReadOnlyPropertyappPath()AsString
- ReturnApplication.StartupPath
- EndGet
- EndProperty
- PublicSharedReadOnlyPropertywinPath()AsString
- Get
- ReturnSystem.Environment.GetEnvironmentVariable("windir")
- EndGet
- EndProperty
- EndClass
- [服务器端]
- service1.vb
- ImportsSystem.ServiceProcess
- ImportsSystem.Runtime
- ImportsSystem.Runtime.Remoting
- ImportsSystem.Runtime.Remoting.Channels
- PublicClassService1
- InheritsSystem.ServiceProcess.ServiceBase
- #Region"组件设计器生成的代码"
- PublicSubNew()
- MyBase.New()
- InitializeComponent()
- EndSub
- ProtectedOverloadsOverridesSubDispose(ByValdisposingAsBoolean)
- IfdisposingThen
- IfNot(componentsIsNothing)Then
- components.Dispose()
- EndIf
- EndIf
- MyBase.Dispose(disposing)
- EndSub
- <MTAThread()>_
- SharedSubMain()
- DimServicesToRun()AsSystem.ServiceProcess.ServiceBase
- ServicesToRun=NewSystem.ServiceProcess.ServiceBase(){NewService1}
- System.ServiceProcess.ServiceBase.Run(ServicesToRun)
- EndSub
- PrivatecomponentsAsSystem.ComponentModel.IContainer
- <System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent()
- Me.ServiceName="Server"
- EndSub
- #EndRegion
- ProtectedOverridesSubOnStart(ByValargs()AsString)
- Try
- DimchAsNewTcp.TcpChannel(8212)
- ChannelServices.RegisterChannel(ch)
- Remoting.RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("serviceShare.serviceShare,serviceShare",True,True),"server",WellKnownObjectMode.Singleton)
- CatchexAsException
- EventLog.WriteEntry("日志"&ex.Message)
- EndTry
- EndSub
- ProtectedOverridesSubOnStop()
- Try
- DimchAsNewTcp.TcpChannel(8212)
- ChannelServices.UnregisterChannel(ch)
- CatchexAsException
- EventLog.WriteEntry("日志"&ex.Message)
- EndTry
- EndSub
- EndClass
- serviceShare.vb
- PublicClassserviceShare
- InheritsMarshalByRefObject
- Implementsshare.Iconnect
- PrivatesharediAsInt32=0
- PublicFunctiongetName()AsStringImplementsshare.Iconnect.getName
- i=i+1
- Return"fromServer"&i
- EndFunction
- EndClass
- [客户端]
- form1.vb
- ImportsSystem
- ImportsSystem.Runtime
- ImportsSystem.Runtime.Remoting
- ImportsSystem.Runtime.Remoting.Channels
- PublicClassForm1
- InheritsSystem.Windows.Forms.Form
- PrivatechAsTcp.TcpChannel
- #Region"Windows窗体设计器生成的代码"
- PublicSubNew()
- MyBase.New()
- InitializeComponent()
- EndSub
- ProtectedOverloadsOverridesSubDispose(ByValdisposingAsBoolean)
- IfdisposingThen
- IfNot(componentsIsNothing)Then
- components.Dispose()
- EndIf
- EndIf
- MyBase.Dispose(disposing)
- EndSub
- PrivatecomponentsAsSystem.ComponentModel.IContainer
- FriendWithEventsLabel1AsSystem.Windows.Forms.Label
- FriendWithEventsButton1AsSystem.Windows.Forms.Button
- <System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent()
- Me.Label1=NewSystem.Windows.Forms.Label
- Me.Button1=NewSystem.Windows.Forms.Button
- Me.SuspendLayout()
- Me.Label1.Location=NewSystem.Drawing.Point(80,50)
- Me.Label1.Name="Label1"
- Me.Label1.Size=NewSystem.Drawing.Size(125,25)
- Me.Label1.TabIndex=0
- Me.Label1.Text="Label1"
- Me.Button1.Location=NewSystem.Drawing.Point(105,195)
- Me.Button1.Name="Button1"
- Me.Button1.Size=NewSystem.Drawing.Size(75,25)
- Me.Button1.TabIndex=1
- Me.Button1.Text="Button1"
- Me.AutoScaleBaseSize=NewSystem.Drawing.Size(6,14)
- Me.ClientSize=NewSystem.Drawing.Size(292,273)
- Me.Controls.Add(Me.Button1)
- Me.Controls.Add(Me.Label1)
- Me.Name="Form1"
- Me.Text="Form1"
- Me.ResumeLayout(False)
- EndSub
- #EndRegion
- PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
- DimserverNameAsString
- DimaaAsshare.Iconnect
- serverName="tcp://127.0.0.1:8212/server"
- aa=CType(Activator.GetObject(Type.GetType("share.Iconnect,share",serverName),share.Iconnect)
- Label1.Text=aa.getName()
- EndSub
- PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
- ch=NewTcp.TcpChannel
- ChannelServices.RegisterChannel(ch)
- EndSub
- EndClass
- [服务安装]
- Strart.vb
- ModuleStrart
- SubMain(ByValarg()AsString)
- OnErrorResumeNext
- #IfDEBUGThen
- IfIO.File.Exists("setup.bat")Then
- Shell("setup.bat",True)
- EndIf
- #EndIf
- 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)
- DimSc2AsNewSystem.ServiceProcess.ServiceController("server")
- IfSc2.Status=ServiceProcess.ServiceControllerStatus.StoppedThen
- Sc2.Start()
- EndIf
- EndIf
- EndSub
- EndModule
- [服务卸载]
- UnSetup.vb
- Modulestrart
- SubMain()
- OnErrorResumeNext
- If(IO.File.Exists("testservice.exe"))Then
- DimSc1AsNewSystem.ServiceProcess.ServiceController("server")
- IfSc1.Status=ServiceProcess.ServiceControllerStatus.RunningThen
- Sc1.Stop()
- EndIf
- Shell(share.app.winPath&"/Microsoft.NET/Framework/v1.1.4322/InstallUtil.exe/u"_
- &share.app.appPath&"/testservice.exe/LogFile",True)
- EndIf
- EndSub
- EndModule
- [批处理]
- 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
- 这样能方便的扩展自己的功能,因为很多书上的代码,使用都是抄微软的,如果程序是分开独立制作,只公布接口的话,安微软的做法就很难成功。
原文链接:https://www.f2er.com/vb/264401.html