Link to home
Start Free TrialLog in
Avatar of Verboten
Verboten

asked on

How do you code a Vb.net Remoting Service to listen to more then one TCP channel.

I have a Vb.net Service that I created that I want to be able to listen on multiple ports and have the different ports register to different classes in the same dll.  So, for example --- I want port 1 to register to objA.Class1 and port 2 to register to objA.Class2.  

This is an example of the code I am using that is located in the onstart method of the service:

TcpNotificationChannel = New TcpServerChannel(1)
ChannelServices.RegisterChannel(TcpNotificationChannel)
RemotingConfiguration.RegisterWellKnownServiceType(GetType(objA.Class1), "Class1", WellKnownObjectMode.SingleCall)

TcpNotificationChannel = New TcpServerChannel(2)
ChannelServices.RegisterChannel(TcpNotificationChannel)
RemotingConfiguration.RegisterWellKnownServiceType(GetType(objA.Class2), "Class2", WellKnownObjectMode.SingleCall)

If I do it this way I receive an error when I try to use the object for port 2.  I receive the error "Requested Service not found".  But when I use the object using port1 it works just fine.  If I switch the order in which they are registered with port 2 being first, the object for port 2 works fine and the call on the object for port 1 fails.

Anyone have any ideas?

Also note -- I plan on moving the registration to a configuration file, but was having problems unregistering the channels when using that approach.
Avatar of armoghan
armoghan
Flag of Pakistan image

ASKER CERTIFIED SOLUTION
Avatar of ihenry
ihenry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Verboten
Verboten

ASKER

Thanks for the response from both of you...

I must appoligize, my sample code above is not the exact way I was doing it.  I had a bad case of cut and paste and forgot to change a few items.  Here is the way I am actually doing it:

dim TcpNotificationChannel1 as New TcpServerChannel(1)
ChannelServices.RegisterChannel(TcpNotificationChannel1)
RemotingConfiguration.RegisterWellKnownServiceType(GetType(objA.Class1), "Class1", WellKnownObjectMode.SingleCall)

dim TcpNotificationChannel2 as New TcpServerChannel(2)
ChannelServices.RegisterChannel(TcpNotificationChannel2)
RemotingConfiguration.RegisterWellKnownServiceType(GetType(objA.Class2), "Class2", WellKnownObjectMode.SingleCall)

So, I am currently using two different instances of the channel.  And I believe the Channel Names will be defaulted to empty string since I didn't set them.

Using the two channels works fine if both of them are registered to the same class, but the problem comes into play if two different classes are being used.  I realize that since my classes are both in the same object, I could used use an interface class that would implement both of them and that would work, but it doesn't seem like the correct or simpliest approach.

Another approach I thought of was using a seperate appdomain in the service, but that is an area I am very new to and currently don't know how to approach it.

Any other ideas?

Your help is appreciated!

If I can remember correctly, ChannelName property is default to "tcp" if it's not set or set it explicitly to empty string to avoid name collition. Please check in MSDN help for TcpServerChannel and let me know if I'm correct.

And how are you getting the object at client-side?
ihenry -- thanks for your help!

It looks like you are correct with the default name of a channel being "tcp".

While digging deeper, I found that the error "Only one usage of each socket address (protocol/network address/port) is normally permitted" was being generated at the service level and I didn't even realize it.  The error message I posted above was coming from the client side from the result of this problem.  I should have noticed that earlier :(

To fix the issue I changed to a different port for one of the channels and it is working fine now.  One thing I also did was to use the recommendation to uniquely name each of the channels during the declaration of the objects through the constructor.

Thanks again.

You're welcome, glad your problem solved.