Remoting用法(2)

 

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)前来与之通讯。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值