register remote object

Richnl

Well-known member
Joined
Mar 20, 2007
Messages
93
Programming Experience
Beginner
syntax for remoting on ipcchannel

I have a beginners question


Sofar I have added two windowsform applications(server and client) and a libraryclass project to the solution. I have added System.Runtime.Remoting references to both projects and refferences to the remoteobject class.
Now I need some help to fill in the blanks.
I kept the client and server code in a separate class apart from the form class
Goal is to add "Hello world" to the listbox on the clientform if a button is pressed and a counter

I have trouble retaining the last value of the counter getID?





VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] knpConnect_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] knpConnect.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] testObj [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SampleObject[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] str [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = testObj.HelloWorld()[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListBox1.Items.Add(str)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label3.Text = [/SIZE][SIZE=2][COLOR=#a31515]"Counter: "[/COLOR][/SIZE][SIZE=2] & (Convert.ToString(testObj.getID))[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] SampleRemotingClient[/SIZE]
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] main()[/SIZE]
[SIZE=2][COLOR=#008000]'// Create a channel for communicating w/ the remote object[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] clientChannel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IpcClientChannel = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IpcClientChannel[/SIZE]
[SIZE=2]ChannelServices.RegisterChannel(clientChannel, [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#008000]'// Register and create an instance of the remote object[/COLOR][/SIZE]
[SIZE=2]RemotingConfiguration.RegisterWellKnownClientType( _[/SIZE]
[SIZE=2]GetType(SampleObject), _[/SIZE]
[SIZE=2][COLOR=#a31515]"ipc://hello/helloworld"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
VB.NET:
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] SampleObject[/SIZE]
[SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] MarshalByRefObject[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] lastID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] HelloWorld() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]"Hello world"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] getID() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2]lastID = lastID + 1[/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] (lastID)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]


VB.NET:
[SIZE=2][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System[/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Runtime.Remoting[/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Runtime.Remoting.Channels[/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Runtime.Remoting.Channels.Ipc[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] SampleRemotingServer[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] main()[/SIZE]
[SIZE=2][COLOR=#008000]' // Create an instance of a channel[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] serverChannel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IChannel = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IpcChannel([/SIZE][SIZE=2][COLOR=#a31515]"hello"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ChannelServices.RegisterChannel(serverChannel, [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000]'// Register as an available service with the name HelloWorld[/COLOR][/SIZE]
[SIZE=2]RemotingConfiguration.RegisterWellKnownServiceType( _[/SIZE]
[SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](SampleObject), _ [/SIZE]
[SIZE=2][COLOR=#a31515]"HelloWorld"[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2]WellKnownObjectMode.Singleton)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
[/SIZE]

TIA, Richard
 
Last edited:
The code is fine except the "separate class apart from the form class" - why? You should also unregister the channels. I have put this into a solution with the three projects (the two winforms app set to startup) that runs your remoting sample.
 

Attachments

  • vbnet20-ipcRemoting.zip
    32.2 KB · Views: 36
The code is fine except the "separate class apart from the form class" - why?
I agree, it's not neccesary, because it took only a couple lines of code
But that's not what causing it too loose state, is it

I will have a look at your sample and draw my conclusions, thank you
 
I'am not that technical yet, but would the problem
be solved with this
Dim clientChannel As New IpcClientChannel("clientchannel", Nothing) as opposed to what I did

Dim clientChannel As IpcClientChannel = New IpcClientChannel'

Could you explain, so I don't do it again next time


 
No, that was just a way of handling the unregistering with a given name, if you don't give a name one is just set, but to unregister it you have to either keep the reference or name for later. Your 'initial problem' was you didn't actually run any code to configure the remoting, you just defined a class you didn't use. Try the uploaded solution, it works just fine.
 
yes. thank you that worked fine

i'am trying to pinpoint the exact mistake I made

you just defined a class you didn't use.
hmm..Do you perhaps mean that I had to register and create an instance in the same procedure. If you do both things at once it maintains state..(thinking for myself)
 
You don't necessarily need to keep the instance, all needed is to configure (register) the remoting system by running the shared methods of ChannelServices and RemotingConfiguration, or using configuration files as the MSDN examples, .NET Framework Remoting Overview (Building step-through). Available also from local help documentation, just search for "remoting" in help index.
 
well, at least I got it working, thanks to you
Msdn, is not always that helpfull. and short of vb samplecode

I guess now I have to implement a timer to access the remoteobject
And make the "popupalerter" state visible/unvisible
See how that go's

thanks
 
I succesfully did the remoting between applications.
Now I let a windows service host the remote object.

I get a big access denied error??

Do I need some additional settings for it to work in a w-service?
 
I found that setting authorizedGroup to Users or Everyone worked with my service (LocalSystem, didn't try other service accounts). I used this code:
VB.NET:
Dim prop As IDictionary = New Hashtable
prop("name") = "myChannelNameS"
prop("portName") = "myChannelPort"
prop("authorizedGroup") = "Users" 'also Everyone worked
Dim serverChannel As New IpcServerChannel(prop, Nothing)
Look into other properties also, Channel and Formatter Configuration Properties
 
Back
Top