Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Remoting / April 2006

Tip: Looking for answers? Try searching our database.

Object lost when remoting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonah Olsson - 17 Apr 2006 00:33 GMT
Hello,

I have a Windows Service running that manage all communication to and from a
serial COM interface. The service updates a SQL Server database whenever
something happens on the COM port.

To be able to send commands to the service, I added .NET Remoting. The
connection works fine and all that, but my trouble is how to pick up the
serial COM object that was initialized when the service started. When the
"SendCommand" function is called, a new instance of the SerialPort object is
being made and all settings are lost. Please see the code below.

Thanks for any kind of help.

Regards,
Jonah Olsson

----------
Public Class ComService
   Inherits System.ServiceProcess.ServiceBase

   Private WithEvents SerialPort As New SerialPort

   Protected Overrides Sub OnStart(ByVal args() As String)

       ' Initiate the COM interface
       ConnectToInterface()
'......
   End Sub

   Private Sub ConnectToInterface()

       ' Load the SerialPort with settings
       With SerialPort
           Try
               .CommPort =
Configuration.ConfigurationSettings.AppSettings("COMM_PORT")
           Catch ex As Exception
               m_eventLog.WriteEntry("Unable to assign serial port")
           End Try

           .BitRate =
Configuration.ConfigurationSettings.AppSettings("BIT_RATE")
           .RTSEnable = True
           .DTREnable = True
           .EnableOnComm = True

           Try
               ' Open the serial port for communication
               .PortOpen = True
           Catch ex As Exception
               m_eventLog.WriteEntry(ex.Message)
           End Try

           If .PortOpen = False Then
               m_eventLog.WriteEntry("Unable to open serial port")
           End If
       End With
   End Sub

   ' Send commands to the COM interface
   Public Sub SendCommand(ByVal cmd As String)
       If SerialPort.PortOpen Then
           SerialPort.Output(cmd & Environment.NewLine)
       End If
   End Sub

End Class

Public Class RemoteConnector
   Inherits MarshalByRefObject
   Implements ICom

   Public Sub SendCommand(ByVal cmd As String) Implements ICom.SendCommand
       Dim cs As new ComService
       cs.SendCommand(cmd)
   End Sub
End Class
Spam Catcher - 17 Apr 2006 07:53 GMT
> To be able to send commands to the service, I added .NET Remoting. The
> connection works fine and all that, but my trouble is how to pick up
> the serial COM object that was initialized when the service started.
> When the "SendCommand" function is called, a new instance of the
> SerialPort object is being made and all settings are lost. Please see
> the code below.

Since you need to maintain state, you need to marshal your object as a
singleton.

You'll also need to override the InitializeLifetimeService function:

http://www.thinktecture.com/Resources/RemotingFAQ/SINGLETON_IS_DYING.html
Jonah Olsson - 17 Apr 2006 10:28 GMT
Hi,

I'm using Singleton for the RemoteConnector class through the App.config
file, but do I need the ComService class to be Singleton as well? Here's my
.config:

 <system.runtime.remoting>
   <application name="RemoteConnector">
     <service>
       <wellknown mode="Singleton"
type="Scanmarket.ComTalker.RemoteConnector, ComTalker" objectUri="remcon" />
     </service>
     <channels>
       <channel ref="http" port="999">
         <clientProviders>
           <formatter ref="binary" />
         </clientProviders>
         <serverProviders>
           <formatter ref="binary" typeFilterLevel='Full' />
         </serverProviders>
       </channel>
     </channels>
   </application>
 </system.runtime.remoting>

/Jonah

>> To be able to send commands to the service, I added .NET Remoting. The
>> connection works fine and all that, but my trouble is how to pick up
[quoted text clipped - 9 lines]
>
> http://www.thinktecture.com/Resources/RemotingFAQ/SINGLETON_IS_DYING.html 
Jonah Olsson - 18 Apr 2006 18:39 GMT
Anyone?

/Jonah
Spam Catcher - 18 Apr 2006 23:58 GMT
"Jonah Olsson" <jonah@IHATESPAM.com> wrote in news:O$p7D8wYGHA.2208
@TK2MSFTNGP03.phx.gbl:

> Anyone?
>
> /Jonah

Did you read the article? You need to override the Lifetime of the object:

http://www.thinktecture.com/Resources/RemotingFAQ/SINGLETON_IS_DYING.html
Jonah Olsson - 19 Apr 2006 18:34 GMT
Thanks, but maybe I wasn't clear in my question. The
InitializeLifetimeService override is already in place. The problem was that
the object within the Windows Service class itself is lost. Maybe your
answer to Alberto could give me a clue.

Cheers,
Jonah

> "Jonah Olsson" <jonah@IHATESPAM.com> wrote in news:O$p7D8wYGHA.2208
> @TK2MSFTNGP03.phx.gbl:
[quoted text clipped - 6 lines]
>
> http://www.thinktecture.com/Resources/RemotingFAQ/SINGLETON_IS_DYING.html
Spam Catcher - 20 Apr 2006 00:49 GMT
> Thanks, but maybe I wasn't clear in my question. The
> InitializeLifetimeService override is already in place. The problem
> was that the object within the Windows Service class itself is lost.

Hmm I took a look at your code:

   Public Sub SendCommand(ByVal cmd As String) Implements ICom.SendCommand
       Dim cs As new ComService
       cs.SendCommand(cmd)
   End Sub

Declare CS as a class variable - don't reinstantiate it on each call. Won't
that fix your problem?

Also take a look at RemotingServices.Marshal command - that'll let you
marshal existing instances of objects.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.