Groups | Blog | Home
all groups > dotnet remoting > september 2005 >

dotnet remoting : Problem Destroying and Recreating Remoting Object



raedwa01 NO[at]SPAM gmail.com
9/15/2005 12:09:06 PM
I have created an object that inherits MarshalByRefObject and
implements IObjectHandle for remoting purposes. I wrote a client that
connects using code like this:

Dim myRemObj as IObjectHandle
RemotingConfiguration.Configure(AppDomain.CurrentDomain.BaseDirectory +
AppDomain.CurrentDomain.FriendlyName + ".config"
remoteUrl= ConfigurationSettings.AppSettings("MyAppRemotingURL")
myRemObject = CType(RemotingServices.Connect(GetType(IObjectHandle,
remoteUrl), IObjectHandle)
Dim newObj as Byte() = myRemObject.Unwrap
.....
myRemObject = Nothing

This seems to work fine for now when connecting to the remote service
and getting data back. The problem is, occasionally in need to destroy
the remote object (it is hosted in a Windows Service) and recreate the
object. Apparently, if the client is running, it hangs up when
recreating the object.

In my Windows Service, i have code like this setup for managing the
objects.

Public Class RemoteManager

Private Channel as HttpServerChannel

Public Sub New()
Me.New(999)
End Sub

Public Sub New(ByVal channelNum as Integer)
If RemotingConfiguration.ApplicationName is Nothing Then
RemotingConfiguration.ApplicationName = "MyApp"
End If

RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemoteObj),
"remote", WellKnownObjectMode.Singleton)
Channel = New HttpServerChannel(channelNum)
End Sub

Protected Sub OnStart()
ChannelServices.RegisterChannel(Channel)
End Sub

Protected Sub OnStop()
ChannelServices.UnregisterChannel(Channel)
End Sub
End Class

In the process of destroying and recreating the object, i have in the
windows service dimmed an object of type RemoteManager. When the app
loads, i call a subroutine that sets the object to nothing and then
sets the object to a new instance of RemoteManager. When i need to
destroy and recreate the object, i first call the OnStop method of the
manager to unregister the channel, then recall the subroutine to set
the object to nothing and back to a new instance.

It appears however that the new instance is not really new. When i
step through i see that the RemotingConfiguration.ApplicationName is
already set (hence the check to see if it is nothing). The app hangs
at the Channel= new HttpServerChannel(channelNum) and just stops.
Apparently, the client is somehow keeping the channel open and stopping
the server from creating a new channel in the same port. I cannot
force the client to close the remote app and have no way of knowing
wether they are running or not.

Does anyone have a suggestion of something i can do to force the
channel closed and destroyed when stopping and setting the item to
nothing?

Thanks,

Rob Edwards
Robert Jordan
9/15/2005 10:09:04 PM
Hi Rob,

[quoted text, click to view]

Stopping a service with the SCM doesn't remove your service object.
It just calls OnStop().

Starting a stopped service just calls OnStart() of your service
object. The contructor of your class doesn't get called again.
That's why you see ApplicationName already beeing set.

Fix that first.

Now on the remoting part. If you really need to restart
the service while clients are still connected, then you
should consider using the *SingleCall* Wellknown type.
With SingleCall the clients don't need to reconnect,
and the server doesn't need to shutdown the channels.

AddThis Social Bookmark Button