Remoting用法(2)

本文介绍使用 VB.NET 2005 实现 Remoting 的具体步骤,包括配置文件设置、服务注册及客户端与服务器间的通信流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Remoting提供客户端与主机的高速通讯,实战:VB.Net 2005(二)

 

上面类库中三个类建好后,

 

在类库文件AssemblyInfo.vb中加入以下代码:

 

#Region " Helper Class to Get Information for the debug methods of each class. "

Public Class AssemblyInfo

    Private myType As Type

    Public Sub New()
        myType = GetType(AssemblyInfo)
    End Sub

    Public ReadOnly Property AsmName() As String
        Get
            Return myType.Assembly.GetName.Name.ToString()
        End Get
    End Property
    Public ReadOnly Property AsmFQName() As String
        Get
            Return myType.Assembly.GetName.FullName.ToString()
        End Get
    End Property
    Public ReadOnly Property CodeBase() As String
        Get
            Return myType.Assembly.CodeBase
        End Get
    End Property
    Public ReadOnly Property Copyright() As String
        Get
            Dim at As Type = GetType(AssemblyCopyrightAttribute)
            Dim r() As Object = myType.Assembly.GetCustomAttributes(at, False)
            Dim ct As AssemblyCopyrightAttribute = CType(r(0), AssemblyCopyrightAttribute)
            Return ct.Copyright
        End Get
    End Property
    Public ReadOnly Property Company() As String
        Get
            Dim at As Type = GetType(AssemblyCopyrightAttribute)
            Dim r() As Object = myType.Assembly.GetCustomAttributes(at, False)
            Dim ct As AssemblyCompanyAttribute = CType(r(0), AssemblyCompanyAttribute)
            Return ct.Company
        End Get
    End Property
    Public ReadOnly Property Description() As String
        Get
            Dim at As Type = GetType(AssemblyDescriptionAttribute)
            Dim r() As Object = myType.Assembly.GetCustomAttributes(at, False)
            Dim da As AssemblyDescriptionAttribute = CType(r(0), AssemblyDescriptionAttribute)
            Return da.Description
        End Get
    End Property
    Public ReadOnly Property Product() As String
        Get
            Dim at As Type = GetType(AssemblyProductAttribute)
            Dim r() As Object = myType.Assembly.GetCustomAttributes(at, False)
            Dim pt As AssemblyProductAttribute = CType(r(0), AssemblyProductAttribute)
            Return pt.Product
        End Get
    End Property
    Public ReadOnly Property Title() As String
        Get
            Dim at As Type = GetType(AssemblyTitleAttribute)
            Dim r() As Object = myType.Assembly.GetCustomAttributes(at, False)
            Dim ta As AssemblyTitleAttribute = CType(r(0), AssemblyTitleAttribute)
            Return ta.Title
        End Get
    End Property
    Public ReadOnly Property Version() As String
        Get
            Return myType.Assembly.GetName.Version.ToString()
        End Get
    End Property
End Class

#End Region

 

再在解决方案资源管理器中添加一个新项目,使之成为两个项目,

 

        项目类型:Windows

        模板:Windows应用程序

        名称为:Host

        位置就选成和上面建的类库共用的父目录,

       建立Window窗体,窗体文件名为:frmMain.vb

       在窗体上加上ListBox,取名为lstOutput

      frmMain.vb中加入下面代码:

 

Option Strict On

 

Imports System.IO
Imports System.Runtime.Remoting

Public Class frmMain
    Inherits System.Windows.Forms.Form

    ' This function iterates through all the ClientActivatedService types
    ' that were loaded via the RemotingConfiguration.Configure(Remoting.config)
    ' file.
    Private Sub ListClientActivatedServiceTypes()
        Dim entry As ActivatedServiceTypeEntry
        For Each entry In RemotingConfiguration.GetRegisteredActivatedServiceTypes()
            Me.lstOutput.Items.Add("  Registered ActivatedServiceType: " & entry.TypeName)
        Next
    End Sub

    ' This function iterates through all the WellKnownService types
    ' that were loaded via the RemotingConfiguration.Configure(Remoting.config)
    ' file.
    Private Sub ListWellKnownServiceTypes()
        Dim entry As WellKnownServiceTypeEntry
        For Each entry In RemotingConfiguration.GetRegisteredWellKnownServiceTypes()
            Me.lstOutput.Items.Add("  " & entry.TypeName & " is available at " & entry.ObjectUri)
        Next
    End Sub

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

        Try
            'Read in Host.exe.config file
            'The call to RemotingConfiguration.Configure loads in the xml configuration file
            'and lets the remoting architecture know what types to make available via remoting

            ' RemotingConfiguration.RegisterActivatedServiceType(GetType(RemotingSample.Customer))
            Me.lstOutput.Items.Add("装载启动配置文件:")

            RemotingConfiguration.Configure("Host.exe.config", False)

            'After loading the remoting.config file enumerate the list of ClientActivated
            'types and WellKnown types and list them in the list box on the form.
            Me.ListClientActivatedServiceTypes()
            Me.ListWellKnownServiceTypes()


        Catch exp As Exception
            ' Will catch any generic exception
            Dim txt As String
            txt = "I was unable to load the file remoting.config or it is not in the correct format." & vbCrLf & _
             "Please make sure it is located in the same directory as this exe " & _
             " and that it is in the correct format." & vbCrLf & _
             "Please see the Help, About box for the location of this exe." & vbCrLf & vbCrLf & _
             "Detailed Error Information below:" & vbCrLf & vbCrLf & _
             "  Message: " & exp.Message & vbCrLf & _
             "  Source: " & exp.Source & vbCrLf & vbCrLf & _
             "  Stack Trace:" & vbCrLf & _
             exp.StackTrace

            MessageBox.Show(txt, "Generic Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)

            Me.lstOutput.Items.Add("Unabled to load objects.")
        End Try

    End Sub

End Class

       

再在Host项目中添加一个新建项,模板为:应用程序配置文件 ,

名称为:app.config

app.config中代码如下:

 

<configuration>
<system.runtime.remoting>
  <application>
    <service>
      <activated type="RemotingSample.Customer,RemoteCustomer"/>
      <wellknown
         type="RemotingSample.SingleCallCustomer,RemoteCustomer"
         objectUri="SingleCallCustomer"
         mode="SingleCall"
            />
      <wellknown
         type="RemotingSample.SingletonCustomer,RemoteCustomer"
         objectUri="SingletonCustomer"
         mode="Singleton"
            />
    </service>
    <channels>
      <channel ref="tcp" port="8080">
        <serverProviders>
          <formatter ref="binary"/>
        </serverProviders>
      </channel>
    </channels>
    <!--
   Below is an example of using a different port.
          -->
    <!--
         <channels>
   <channel ref="tcp" port="9090">
    <serverProviders>
     <formatter ref="binary"/>
    </serverProviders>
   </channel>
         </channels>
  -->

  </application>
</system.runtime.remoting>
</configuration>

 

在Host项目的引用中添加引用:选项目:RemoteCustomer

 

将Host项目设为启动项,生成Host后,将Host.exe及其目录下的文件和

 

RemoteCustomer.dll等文件,拷贝到服务器中任一目录,运行Host后等待

客户机(client)前来与之通讯。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值