Receiving a 'Type is not registered for activation' error when trying to create a second remoting connection

37 views Asked by At

For over 12 years the first connect() function below has worked perfectly connecting to a single server side application. This year we needed to create a second connection to the same server side application hosted on different machine, so replicated the function and added a new Connection2 class as we understand that a single class can only be registered on the client side once. Please see the code below and perhaps offer some suggestions as to where we are going wrong. PS: Please be aware that this is inherited code that started life in .NET Framework 1.0, so although this might not be the best way, it is working when running connect(), but failing when running connect2().

Private Function connect(Optional ByVal forceConnect As Boolean = False) As Boolean
    Dim channel As TcpClientChannel

    Try
      If Not remoteServer Is Nothing And Not forceConnect Then Exit Function 'don't reconnect
      'unregister first in case we are connecting after a failure
      channel = ChannelServices.GetChannel("server")
      ChannelServices.UnregisterChannel(channel)
      Dim serverProv As BinaryServerFormatterSinkProvider = New BinaryServerFormatterSinkProvider
      serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full
      Dim clientProv As BinaryClientFormatterSinkProvider = New BinaryClientFormatterSinkProvider
      System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(New TcpClientChannel("server", clientProv), False)
      Try
        RemotingConfiguration.RegisterActivatedClientType(GetType(Connection), server)
      Catch ex As Exception
        'resume
      End Try
      remoteServer = New Connection
    Catch ex As System.Net.Sockets.SocketException
      MsgBox("The server is not started" & vbCrLf & vbCrLf & server, MsgBoxStyle.Critical, "Error")
      channel = ChannelServices.GetChannel("server")
      ChannelServices.UnregisterChannel(channel)
      Return False
    Catch ex As Exception
      MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!!")
      channel = ChannelServices.GetChannel("server")
      ChannelServices.UnregisterChannel(channel)
      Return False
    End Try
    Return True
  End Function
  Private Function connect2(Optional ByVal forceConnect As Boolean = False) As Boolean
    Dim channel2 As TcpClientChannel

    Try
      If Not remoteServer2 Is Nothing And Not forceConnect Then Exit Function 'don't reconnect
      'unregister first in case we are connecting after a failure
      channel2 = ChannelServices.GetChannel("server2")
      ChannelServices.UnregisterChannel(channel2)
      Dim serverProv2 As BinaryServerFormatterSinkProvider = New BinaryServerFormatterSinkProvider
      serverProv2.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full
      Dim clientProv2 As BinaryClientFormatterSinkProvider = New BinaryClientFormatterSinkProvider
      System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(New TcpClientChannel("server2", clientProv2), False)
      Try
        RemotingConfiguration.RegisterActivatedClientType(GetType(Connection2), server2)
      Catch ex As Exception
        'resume
      End Try
      remoteServer2 = New Connection2 <---- EXCEPTION OCCURS HERE
    Catch ex As System.Net.Sockets.SocketException
      MsgBox("The server2 is not started" & vbCrLf & vbCrLf & server, MsgBoxStyle.Critical, "Error")
      channel2 = ChannelServices.GetChannel("server2")
      ChannelServices.UnregisterChannel(channel2)
      Return False
    Catch ex As Exception
      MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!!")
      channel2 = ChannelServices.GetChannel("server2")
      ChannelServices.UnregisterChannel(channel2)
      Return False
    End Try
    Return True
  End Function

As mentioned we have tried with a second Collection class (just Inheritted from original class); We have tried creating a completely new dll with every method, function, class renamed; Obviously if we create remoteServer2 = Connection, then the client reads from the original DB on the first server.

Thank you in advance for any advice/suggestions as we are not 100% familiar with Remoting in .NET

UPDATE - @500-InternalServerError Hi All, we have gotten closer - The server side remoting services are what are sending this error back. We have now added a config file flag where we call the relevant connection in a switch statement (server side): Select Case connectionNo Case "1" RemotingConfiguration.RegisterActivatedServiceType(GetType(Connection)) Case "2" RemotingConfiguration.RegisterActivatedServiceType(GetType(Connection2)) Case Else Throw New Exception("No valid connection no found") End Select

So you are definately correct that that is where the initial error came from. Thank you.

0

There are 0 answers